Interface.Remove() was directly accessing Namespace "internals", such
as locking/unlocking. Move the code from Interface.Remove() into the
Namespace instead.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -88,53 +88,8 @@ func (i *Interface) Routes() []*net.IPNet {
|
| 88 | 88 |
// Remove an interface from the sandbox by renaming to original name |
| 89 | 89 |
// and moving it out of the sandbox. |
| 90 | 90 |
func (i *Interface) Remove() error {
|
| 91 |
- i.ns.Lock() |
|
| 92 |
- isDefault := i.ns.isDefault |
|
| 93 |
- nlh := i.ns.nlHandle |
|
| 94 |
- i.ns.Unlock() |
|
| 95 |
- |
|
| 96 |
- // Find the network interface identified by the DstName attribute. |
|
| 97 |
- iface, err := nlh.LinkByName(i.DstName()) |
|
| 98 |
- if err != nil {
|
|
| 99 |
- return err |
|
| 100 |
- } |
|
| 101 |
- |
|
| 102 |
- // Down the interface before configuring |
|
| 103 |
- if err := nlh.LinkSetDown(iface); err != nil {
|
|
| 104 |
- return err |
|
| 105 |
- } |
|
| 106 |
- |
|
| 107 |
- err = nlh.LinkSetName(iface, i.SrcName()) |
|
| 108 |
- if err != nil {
|
|
| 109 |
- log.G(context.TODO()).Debugf("LinkSetName failed for interface %s: %v", i.SrcName(), err)
|
|
| 110 |
- return err |
|
| 111 |
- } |
|
| 112 |
- |
|
| 113 |
- // if it is a bridge just delete it. |
|
| 114 |
- if i.Bridge() {
|
|
| 115 |
- if err := nlh.LinkDel(iface); err != nil {
|
|
| 116 |
- return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
|
|
| 117 |
- } |
|
| 118 |
- } else if !isDefault {
|
|
| 119 |
- // Move the network interface to caller namespace. |
|
| 120 |
- if err := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
|
|
| 121 |
- log.G(context.TODO()).Debugf("LinkSetNsPid failed for interface %s: %v", i.SrcName(), err)
|
|
| 122 |
- return err |
|
| 123 |
- } |
|
| 124 |
- } |
|
| 125 |
- |
|
| 126 |
- i.ns.Lock() |
|
| 127 |
- for index, intf := range i.ns.iFaces {
|
|
| 128 |
- if intf == i {
|
|
| 129 |
- i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...) |
|
| 130 |
- break |
|
| 131 |
- } |
|
| 132 |
- } |
|
| 133 |
- i.ns.Unlock() |
|
| 134 |
- |
|
| 135 |
- i.ns.checkLoV6() |
|
| 136 |
- |
|
| 137 |
- return nil |
|
| 91 |
+ nameSpace := i.ns |
|
| 92 |
+ return nameSpace.RemoveInterface(i) |
|
| 138 | 93 |
} |
| 139 | 94 |
|
| 140 | 95 |
// Statistics returns the sandbox's side veth interface statistics. |
| ... | ... |
@@ -294,6 +249,57 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti |
| 294 | 294 |
return nil |
| 295 | 295 |
} |
| 296 | 296 |
|
| 297 |
+// RemoveInterface removes an interface from the namespace by renaming to |
|
| 298 |
+// original name and moving it out of the sandbox. |
|
| 299 |
+func (n *Namespace) RemoveInterface(i *Interface) error {
|
|
| 300 |
+ n.Lock() |
|
| 301 |
+ isDefault := n.isDefault |
|
| 302 |
+ nlh := n.nlHandle |
|
| 303 |
+ n.Unlock() |
|
| 304 |
+ |
|
| 305 |
+ // Find the network interface identified by the DstName attribute. |
|
| 306 |
+ iface, err := nlh.LinkByName(i.DstName()) |
|
| 307 |
+ if err != nil {
|
|
| 308 |
+ return err |
|
| 309 |
+ } |
|
| 310 |
+ |
|
| 311 |
+ // Down the interface before configuring |
|
| 312 |
+ if err := nlh.LinkSetDown(iface); err != nil {
|
|
| 313 |
+ return err |
|
| 314 |
+ } |
|
| 315 |
+ |
|
| 316 |
+ err = nlh.LinkSetName(iface, i.SrcName()) |
|
| 317 |
+ if err != nil {
|
|
| 318 |
+ log.G(context.TODO()).Debugf("LinkSetName failed for interface %s: %v", i.SrcName(), err)
|
|
| 319 |
+ return err |
|
| 320 |
+ } |
|
| 321 |
+ |
|
| 322 |
+ // if it is a bridge just delete it. |
|
| 323 |
+ if i.Bridge() {
|
|
| 324 |
+ if err := nlh.LinkDel(iface); err != nil {
|
|
| 325 |
+ return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
|
|
| 326 |
+ } |
|
| 327 |
+ } else if !isDefault {
|
|
| 328 |
+ // Move the network interface to caller namespace. |
|
| 329 |
+ if err := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
|
|
| 330 |
+ log.G(context.TODO()).Debugf("LinkSetNsFd failed for interface %s: %v", i.SrcName(), err)
|
|
| 331 |
+ return err |
|
| 332 |
+ } |
|
| 333 |
+ } |
|
| 334 |
+ |
|
| 335 |
+ n.Lock() |
|
| 336 |
+ for index, intf := range i.ns.iFaces {
|
|
| 337 |
+ if intf == i {
|
|
| 338 |
+ i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...) |
|
| 339 |
+ break |
|
| 340 |
+ } |
|
| 341 |
+ } |
|
| 342 |
+ n.Unlock() |
|
| 343 |
+ |
|
| 344 |
+ n.checkLoV6() |
|
| 345 |
+ return nil |
|
| 346 |
+} |
|
| 347 |
+ |
|
| 297 | 348 |
func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
|
| 298 | 349 |
ifaceName := iface.Attrs().Name |
| 299 | 350 |
ifaceConfigurators := []struct {
|