@@ -20,7 +20,7 @@ type NetConf struct {
2020 types.NetConf
2121 Master string `json:"master"`
2222 MAC string `json:"mac"`
23- VF int `json:"vf"`
23+ VF * int `json:"vf"`
2424 Vlan int `json:"vlan"`
2525}
2626
@@ -43,9 +43,12 @@ func loadConf(bytes []byte) (*NetConf, error) {
4343}
4444
4545func setupVF (conf * NetConf , ifName string , netns ns.NetNS ) error {
46-
46+ vfIdx := 0
4747 masterName := conf .Master
48- vfIdx := conf .VF
48+ if conf .VF != nil {
49+ vfIdx = * conf .VF
50+ }
51+ // TODO: if conf.VF == nil, alloc vf randomly
4952
5053 m , err := netlink .LinkByName (masterName )
5154 if err != nil {
@@ -63,7 +66,7 @@ func setupVF(conf *NetConf, ifName string, netns ns.NetNS) error {
6366 }
6467
6568 if len (infos ) != 1 {
66- return fmt .Errorf ("Mutiple network devices in directory %s" , vfDir )
69+ return fmt .Errorf ("no network devices in directory %s" , vfDir )
6770 }
6871
6972 // VF NIC name
@@ -79,36 +82,41 @@ func setupVF(conf *NetConf, ifName string, netns ns.NetNS) error {
7982 if err != nil {
8083 return err
8184 }
82- if err = netlink .LinkSetVfHardwareAddr (m , conf . VF , macAddr ); err != nil {
83- return fmt .Errorf ("failed to set vf %d macaddress: %v" , conf . VF , err )
85+ if err = netlink .LinkSetVfHardwareAddr (m , vfIdx , macAddr ); err != nil {
86+ return fmt .Errorf ("failed to set vf %d macaddress: %v" , vfIdx , err )
8487 }
8588 }
8689
8790 if conf .Vlan != 0 {
88- if err = netlink .LinkSetVfVlan (m , conf . VF , conf .Vlan ); err != nil {
89- return fmt .Errorf ("failed to set vf %d vlan: %v" , conf . VF , err )
91+ if err = netlink .LinkSetVfVlan (m , vfIdx , conf .Vlan ); err != nil {
92+ return fmt .Errorf ("failed to set vf %d vlan: %v" , vfIdx , err )
9093 }
9194 }
9295
9396 if err = netlink .LinkSetUp (vfDev ); err != nil {
94- return fmt .Errorf ("failed to setup vf %d device: %v" , conf . VF , err )
97+ return fmt .Errorf ("failed to setup vf %d device: %v" , vfIdx , err )
9598 }
9699
97100 // move VF device to ns
98101 if err = netlink .LinkSetNsFd (vfDev , int (netns .Fd ())); err != nil {
99- return fmt .Errorf ("failed to move vf %d to netns: %v" , conf . VF , err )
102+ return fmt .Errorf ("failed to move vf %d to netns: %v" , vfIdx , err )
100103 }
101104
102105 return netns .Do (func (_ ns.NetNS ) error {
103106 err := renameLink (vfDevName , ifName )
104107 if err != nil {
105- return fmt .Errorf ("failed to rename vf %d device %q to %q: %v" , conf . VF , vfDevName , ifName , err )
108+ return fmt .Errorf ("failed to rename vf %d device %q to %q: %v" , vfIdx , vfDevName , ifName , err )
106109 }
107110 return nil
108111 })
109112}
110113
111114func releaseVF (conf * NetConf , ifName string , netns ns.NetNS ) error {
115+ vfIdx := 0
116+ if conf .VF != nil {
117+ vfIdx = * conf .VF
118+ }
119+
112120 initns , err := ns .GetCurrentNS ()
113121 if err != nil {
114122 return fmt .Errorf ("failed to get init netns: %v" , err )
@@ -121,7 +129,7 @@ func releaseVF(conf *NetConf, ifName string, netns ns.NetNS) error {
121129 // get VF device
122130 vfDev , err := netlink .LinkByName (ifName )
123131 if err != nil {
124- return fmt .Errorf ("failed to lookup vf %d device %q: %v" , conf . VF , ifName , err )
132+ return fmt .Errorf ("failed to lookup vf %d device %q: %v" , vfIdx , ifName , err )
125133 }
126134
127135 // device name in init netns
@@ -130,18 +138,18 @@ func releaseVF(conf *NetConf, ifName string, netns ns.NetNS) error {
130138
131139 // shutdown VF device
132140 if err = netlink .LinkSetDown (vfDev ); err != nil {
133- return fmt .Errorf ("failed to down vf %d device: %v" , conf . VF , err )
141+ return fmt .Errorf ("failed to down vf %d device: %v" , vfIdx , err )
134142 }
135143
136144 // rename VF device
137145 err = renameLink (ifName , devName )
138146 if err != nil {
139- return fmt .Errorf ("failed to rename vf %d evice %q to %q: %v" , conf . VF , ifName , devName , err )
147+ return fmt .Errorf ("failed to rename vf %d evice %q to %q: %v" , vfIdx , ifName , devName , err )
140148 }
141149
142150 // move VF device to init netns
143151 if err = netlink .LinkSetNsFd (vfDev , int (initns .Fd ())); err != nil {
144- return fmt .Errorf ("failed to move vf %d to init netns: %v" , conf . VF , err )
152+ return fmt .Errorf ("failed to move vf %d to init netns: %v" , vfIdx , err )
145153 }
146154
147155 return nil
0 commit comments