Skip to content

Commit caed171

Browse files
authored
Merge pull request #8 from hustcat/devel
Move 'VF/MAC/VLAN' from NetConf to NetArgs by following the CNI spec
2 parents 7bd3ac8 + ca774bf commit caed171

File tree

7 files changed

+247
-38
lines changed

7 files changed

+247
-38
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ options ixgbe max_vfs=8,8
3030
* `name` (string, required): the name of the network
3131
* `type` (string, required): "sriov"
3232
* `master` (string, required): name of the PF
33+
* `ipam` (dictionary, required): IPAM configuration to be used for this network.
34+
35+
## Extra arguments
36+
3337
* `vf` (int, optional): VF index, default value is 0
3438
* `vlan` (int, optional): VLAN ID for VF device
3539
* `mac` (string, optional): mac address for VF device
36-
* `ipam` (dictionary, required): IPAM configuration to be used for this network.
3740

3841
## Usage
3942

@@ -45,10 +48,8 @@ Given the following network configuration:
4548
"name": "mynet",
4649
"type": "sriov",
4750
"master": "eth1",
48-
"vf": 1,
49-
"mac": "66:d8:02:77:aa:aa",
5051
"ipam": {
51-
"type": "host-local",
52+
"type": "fixipam",
5253
"subnet": "10.55.206.0/26",
5354
"routes": [
5455
{ "dst": "0.0.0.0/0" }
@@ -59,16 +60,22 @@ Given the following network configuration:
5960
EOF
6061
```
6162

62-
```
63-
# CNI_PATH=$CNI_PATH CNI_ARGS="IP=10.55.206.46" ./priv-net-run.sh ifconfig
63+
Add container to network:
64+
65+
```sh
66+
# CNI_PATH=`pwd`/bin
67+
# cd scripts
68+
# CNI_PATH=$CNI_PATH CNI_ARGS="IP=10.55.206.46;VF=1;MAC=66:d8:02:77:aa:aa" ./priv-net-run.sh ifconfig
69+
contid=148e21a85bcc7aaf
70+
netnspath=/var/run/netns/148e21a85bcc7aaf
6471
eth0 Link encap:Ethernet HWaddr 66:D8:02:77:AA:AA
6572
inet addr:10.55.206.46 Bcast:0.0.0.0 Mask:255.255.255.192
6673
inet6 addr: fe80::64d8:2ff:fe77:aaaa/64 Scope:Link
6774
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
68-
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
69-
TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
75+
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
76+
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
7077
collisions:0 txqueuelen:1000
71-
RX bytes:530 (530.0 b) TX bytes:988 (988.0 b)
78+
RX bytes:0 (0.0 b) TX bytes:558 (558.0 b)
7279

7380
lo Link encap:Local Loopback
7481
inet addr:127.0.0.1 Mask:255.0.0.0
@@ -80,4 +87,16 @@ lo Link encap:Local Loopback
8087
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
8188
```
8289

90+
Remove container from network:
91+
92+
```sh
93+
# CNI_PATH=$CNI_PATH ./exec-plugins.sh del $contid /var/run/netns/$contid
94+
```
95+
96+
For example:
97+
98+
```sh
99+
# CNI_PATH=$CNI_PATH ./exec-plugins.sh del 148e21a85bcc7aaf /var/run/netns/148e21a85bcc7aaf
100+
```
101+
83102
[More info](https://github.com/containernetworking/cni/pull/259).

config/args.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package config
2+
3+
import (
4+
"encoding"
5+
"fmt"
6+
"reflect"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
// UnmarshallableBool typedef for builtin bool
12+
// because builtin type's methods can't be declared
13+
type UnmarshallableBool bool
14+
15+
// UnmarshalText implements the encoding.TextUnmarshaler interface.
16+
// Returns boolean true if the string is "1" or "[Tt]rue"
17+
// Returns boolean false if the string is "0" or "[Ff]alse"
18+
func (b *UnmarshallableBool) UnmarshalText(data []byte) error {
19+
s := strings.ToLower(string(data))
20+
switch s {
21+
case "1", "true":
22+
*b = true
23+
case "0", "false":
24+
*b = false
25+
default:
26+
return fmt.Errorf("Boolean unmarshal error: invalid input %s", s)
27+
}
28+
return nil
29+
}
30+
31+
// UnmarshallableString typedef for builtin string
32+
type UnmarshallableString string
33+
34+
// UnmarshalText implements the encoding.TextUnmarshaler interface.
35+
// Returns the string
36+
func (s *UnmarshallableString) UnmarshalText(data []byte) error {
37+
*s = UnmarshallableString(data)
38+
return nil
39+
}
40+
41+
// CommonArgs contains the IgnoreUnknown argument
42+
// and must be embedded by all Arg structs
43+
type CommonArgs struct {
44+
IgnoreUnknown UnmarshallableBool `json:"ignoreunknown,omitempty"`
45+
}
46+
47+
// GetKeyField is a helper function to receive Values
48+
// Values that represent a pointer to a struct
49+
func GetKeyField(keyString string, v reflect.Value) reflect.Value {
50+
return v.Elem().FieldByName(keyString)
51+
}
52+
53+
// LoadSriovArgs parses args from a string in the form "K=V;K2=V2;..."
54+
func LoadSriovArgs(args string, container interface{}) error {
55+
if args == "" {
56+
return nil
57+
}
58+
59+
containerValue := reflect.ValueOf(container)
60+
61+
pairs := strings.Split(args, ";")
62+
unknownArgs := []string{}
63+
for _, pair := range pairs {
64+
kv := strings.Split(pair, "=")
65+
if len(kv) != 2 {
66+
return fmt.Errorf("ARGS: invalid pair %q", pair)
67+
}
68+
keyString := kv[0]
69+
valueString := kv[1]
70+
keyField := GetKeyField(keyString, containerValue)
71+
if !keyField.IsValid() {
72+
unknownArgs = append(unknownArgs, pair)
73+
continue
74+
}
75+
76+
switch keyField.Kind() {
77+
case reflect.Int:
78+
vi, err := strconv.Atoi(valueString)
79+
if err != nil {
80+
return fmt.Errorf("Convert value %s for field %s failed", valueString, keyString)
81+
}
82+
keyField.SetInt(int64(vi))
83+
case reflect.String:
84+
keyField.SetString(valueString)
85+
case reflect.Slice:
86+
u := keyField.Addr().Interface().(encoding.TextUnmarshaler)
87+
err := u.UnmarshalText([]byte(valueString))
88+
if err != nil {
89+
return fmt.Errorf("ARGS: error parsing value of pair %q: %v)", pair, err)
90+
}
91+
default:
92+
return fmt.Errorf("Unsupport type %s for %s", keyField.Kind().String(), keyString)
93+
}
94+
}
95+
96+
isIgnoreUnknown := GetKeyField("IgnoreUnknown", containerValue).Bool()
97+
if len(unknownArgs) > 0 && !isIgnoreUnknown {
98+
return fmt.Errorf("ARGS: unknown args %q", unknownArgs)
99+
}
100+
return nil
101+
}

config/args_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLoadArgs(t *testing.T) {
8+
s := "VF=1;VLAN=10;MAC=AA:BB:CC:DD:EE:FF;IP=192.168.1.2"
9+
args := &NetArgs{}
10+
err := LoadSriovArgs(s, args)
11+
if err != nil {
12+
t.Errorf("error %v", err)
13+
}
14+
if args.IP.String() != "192.168.1.2" {
15+
t.Errorf("failed to parse IP")
16+
}
17+
if args.VF != 1 {
18+
t.Errorf("failed to parse VF")
19+
}
20+
if args.MAC != "AA:BB:CC:DD:EE:FF" {
21+
t.Errorf("failed to parse MAC")
22+
}
23+
if args.VLAN != 10 {
24+
t.Errorf("failed to parse VLAN")
25+
}
26+
27+
//fmt.Printf("%#v\n%s\n", args, args.IP.String())
28+
}
29+
30+
// go test -run LoadArgs

config/config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package config
2+
3+
import (
4+
"net"
5+
6+
"github.com/containernetworking/cni/pkg/types"
7+
)
8+
9+
type NetConf struct {
10+
types.NetConf
11+
Master string `json:"master"`
12+
}
13+
14+
type NetArgs struct {
15+
types.CommonArgs
16+
VF int `json:"vf,omitempty"`
17+
VLAN int `json:"vlan,omitempty"`
18+
MAC string `json:"mac,omitempty"`
19+
IP net.IP `json:"ip,omitempty"`
20+
}
21+
22+
type SriovConf struct {
23+
Net *NetConf
24+
Args *NetArgs
25+
}

fixipam/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package main
22

33
import (
44
"fmt"
5+
"net"
6+
57
"github.com/containernetworking/cni/pkg/skel"
68
"github.com/containernetworking/cni/pkg/types"
7-
"net"
89
)
910

1011
func main() {

scripts/priv-net-run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ if [[ ${DEBUG} -gt 0 ]]; then set -x; fi
55
# Run a command in a private network namespace
66
# set up by CNI plugins
77
contid=$(printf '%x%x%x%x' $RANDOM $RANDOM $RANDOM $RANDOM)
8+
echo "contid=$contid"
9+
810
netnspath=/var/run/netns/$contid
11+
echo "netnspath=$netnspath"
912

1013
ip netns add $contid
1114
./exec-plugins.sh add $contid $netnspath

0 commit comments

Comments
 (0)