Skip to content
Permalink
Browse files

Add macaddr keyword in netns resource driver

* Useful to inject a specific mac address into the network namespace

* Example :

[DEFAULT]
id = 30e86bae-b050-47d5-8550-d81c586b6ac6

[ip#0]
type = netns
netns = container#network
macaddr = 00:01:02:03:04:05
ipdev = dummy-bridge
ipname = 192.168.1.123
netmask = 24
check_carrier = false

[container#network]
type = docker
image = markeijsermans/debug
rm = true
tty = true

root@node:/ # om demomac docker exec -it demomac.container.network ip addr show dev eth0
26: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:01:02:03:04:05 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.1.123/24 scope global eth0
       valid_lft forever preferred_lft forever
  • Loading branch information...
arnaudveron committed Aug 12, 2019
1 parent a0351b4 commit b538b858a8b248c474c3c7c25fab2a3b3343131c
Showing with 32 additions and 0 deletions.
  1. +23 −0 lib/resIpNetnsLinux.py
  2. +1 −0 lib/svcBuilder.py
  3. +8 −0 lib/svcdict.py
@@ -19,6 +19,7 @@ def __init__(self,
del_net_route=False,
netns=None,
nsdev=None,
macaddr=None,
vlan_tag=None,
vlan_mode=None,
**kwargs):
@@ -28,11 +29,13 @@ def __init__(self,
ipdev=ipdev,
ipname=ipname,
gateway=gateway,
macaddr=macaddr,
mask=mask,
**kwargs)
self.mode = mode if mode else "bridge"
self.network = network
self.nsdev = nsdev
self.macaddr = macaddr
self.del_net_route = del_net_route
self.container_rid = str(netns)
self.vlan_tag = vlan_tag
@@ -46,6 +49,20 @@ def on_add(self):
self.svc.register_dependency("stop", self.container_rid, self.rid)
self.set_label()

def set_macaddr(self):
"""
Set the intf mac addr
"""
if not self.macaddr:
return
try:
cmd = [rcEnv.syspaths.nsenter, "--net="+self.netns, "ip", "link", "set", self.final_guest_dev, "address", self.macaddr]
ret, out, err = self.vcall(cmd)
except ex.excError:
pass
if ret != 0:
return ret, out, err

def set_label(self):
"""
Set the resource label property.
@@ -333,6 +350,9 @@ def startip_cmd_shared_bridge(self):
if ret != 0:
return ret, out, err

# set the mac addr
self.set_macaddr()

# plumb ip
cmd = [rcEnv.syspaths.nsenter, "--net="+self.netns, "ip", "addr", "add", self.addr+"/"+to_cidr(self.mask), "dev", self.final_guest_dev]
ret, out, err = self.vcall(cmd)
@@ -415,6 +435,9 @@ def startip_cmd_shared_macvlan(self):
if ret != 0:
return ret, out, err

# set the mac addr
self.set_macaddr()

# plumb the ip
cmd = [rcEnv.syspaths.nsenter, "--net="+self.netns, "ip", "addr", "add", "%s/%s" % (self.addr, to_cidr(self.mask)), "dev", self.final_guest_dev]
ret, out, err = self.vcall(cmd)
@@ -206,6 +206,7 @@ def add_ip(svc, s):
kwargs["nsdev"] = svc.oget(s, "nsdev")
kwargs["mode"] = svc.oget(s, "mode")
kwargs["network"] = svc.oget(s, "network")
kwargs["macaddr"] = svc.oget(s, "macaddr")
kwargs["del_net_route"] = svc.oget(s, "del_net_route")
if kwargs["mode"] == "ovs":
kwargs["vlan_tag"] = svc.oget(s, "vlan_tag")
@@ -1872,6 +1872,14 @@
"required": False,
"text": "If specified, use this interface name in the netns. If not specified the first free ``eth<n>`` is chosen."
},
{
"section": "ip",
"rtype": ["netns", "docker"],
"keyword": "macaddr",
"at": True,
"required": False,
"text": "If specified, use this mac address in the netns."
},
{
"section": "ip",
"rtype": "cni",

0 comments on commit b538b85

Please sign in to comment.
You can’t perform that action at this time.