How to attach a veth to a bridge using pyroute2 IPRoute

664 views
Skip to first unread message

Rayene Ben Rayana

unread,
Jul 26, 2015, 4:26:35 PM7/26/15
to pyroute2-dev
Hi all,

I need to 'brctl addif br0 veth1' using IPRoute.
I looked into the source code and I'm pretty sure it is implemented in compat_bridge_port().
I just need to have the right IPRoute call.. Something like   : ipr.link('add', master='br0, port='veth1')

Thanks in advance,


Peter Saveliev

unread,
Jul 27, 2015, 3:06:20 AM7/27/15
to pyroute2-dev
Like that:

# create bridge
ipr.link_create(ifname='br_test', kind='bridge')

# get if index
idx_br = ipr.link_lookup(ifname='br_test')[0]

# create veth
ipr.link_create(ifname='ve0', peer='ve1', kind='veth')

# get if index
idx_ve = ipr.link_lookup(ifname='ve1')

# add ve1 as a port to br_test
ipr.link('set', index=idx_ve, master=idx_br)

# check
ipr.get_links(idx_ve)[0].get_attr('IFLA_MASTER')

Some calls return result like

[{'event': 'NLMSG_ERROR',
  'header': {'error': None,
   'flags': 0,
   'length': 36,
   'pid': 23119,
   'sequence_number': 255,
   'type': 2}}]


It is ok. Since IPRoute is just a 1:1 mapping to the RTNL protocol, it returns what the kernel returns, and this result means that the kernel returns NLMSG_ERROR messages with error set to None — so, the operation succeeded.

--
You received this message because you are subscribed to the Google Groups "pyroute2-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyroute2-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Saveliev

unread,
Jul 27, 2015, 3:08:45 AM7/27/15
to pyroute2-dev
errata: the IPRoute.link_lookup() call always returns a list of results, so there is a mistake — in the idx_ve assignment should be used [0] in the same way as for idx_br.

Rayene Ben Rayana

unread,
Jul 27, 2015, 7:56:13 AM7/27/15
to Peter Saveliev, pyroute2-dev
Thanks Peter,

I'll try that tonight.

Cheers,

You received this message because you are subscribed to a topic in the Google Groups "pyroute2-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyroute2-dev/aDyrNDz9hQk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyroute2-dev...@googlegroups.com.
Message has been deleted

Santhosh Nag Vadlamani

unread,
Sep 9, 2015, 2:11:02 AM9/9/15
to pyroute2-dev
Hi Peter,
I am trying to do the following

- Create eth0.5
- create br0
- Add eth0.5 to br0

- remove eth0.5 from br0
- delete eth0.5
- delete br0

Could please help me on how to acheive these, especially the delete parts.

Thanks

Peter Saveliev

unread,
Sep 9, 2015, 7:41:51 AM9/9/15
to pyroute2-dev, Santhosh Nag Vadlamani
ipr = IPRoute()

> - Create eth0.5
> - create br0

# Assume you want to create vlan:
ipr.link_create(ifname='eth0.5', kind='vlan', vlan_id=5, link=ipr.link_lookup(ifname='eth0')[0])
ipr.link_create(ifname='br0', kind='bridge')
# Pls keep in mind that you can give any name to the vlan interface. Literally any, up to 16 ascii symbols.

> - Add eth0.5 to br0
ipr.link('set', index=ipr.link_lookup(ifname='eth0.5')[0], master=ipr.link_lookup(ifname='br0')[0])

> - remove eth0.5 from br0
ipr.link('set', index=ipr.link_lookup(ifname='eth0.5')[0], master=0)

> - delete eth0.5
> - delete br0
ipr.link_remove(index=ipr.link_lookup(ifname='eth0.5')[0])
ipr.link_remove(index=ipr.link_lookup(ifname='br0'[0])

If you don't care about additional threads in the application, but instead want synchronous execution (so the script will continue only after an interface really gets created etc.), you can use IPDB (http://docs.pyroute2.org/ipdb.html)

Santhosh Nag Vadlamani

unread,
Sep 9, 2015, 12:37:00 PM9/9/15
to pyroute2-dev, sant...@gmail.com
Hi Peter,
I was able to achieve the above using IPDB(), but it seems to be consuming lot of memory.
I am trying to use this on a low memory device(256MB) where my script consumes almost 25% after using IPDB.
IPDB().release() doesnt seem to release the memory.

So trying to move to IPRoute.

Thanks

Peter V. Saveliev

unread,
Sep 9, 2015, 12:40:12 PM9/9/15
to pyrout...@googlegroups.com, Santhosh Nag Vadlamani


Could you please tell the version you use?

Multiple IPDB vs. memory issues were fixed recently, and if there are still problems, it's better to know (and fix).

Santhosh Nag Vadlamani

unread,
Sep 9, 2015, 12:45:23 PM9/9/15
to pyroute2-dev, sant...@gmail.com
Hi,
We started with pyroute2 0.3.9, yesterday we moved to 0.3.14 after reading another forum where there was a discussion regarding the memory improvement.

Simple test i tried was in python shell.
>>> from pyroute2 import IPDB
>> ip = IPDB()
when i check the memory it was using upto 30MB,
>>> ip.release()
I did not see any change in the memory usage.
Once i kill the python shell, the memory is freed.

Thanks

Peter V. Saveliev

unread,
Sep 9, 2015, 12:55:33 PM9/9/15
to pyrout...@googlegroups.com, Santhosh Nag Vadlamani
Let me to ask:

On 09/09/2015 06:45 PM, Santhosh Nag Vadlamani wrote:
<skip />


Simple test i tried was in python shell.
>>> from pyroute2 import IPDB
>> ip = IPDB()
when i check the memory it was using upto 30MB,
>>> ip.release()
I did not see any change in the memory usage.

Any change comparing to 0.3.9, or any change comparing to state before release()?

Since IPDB keeps all the info about the IP stack (addresses, routes, neighbours etc.), it surely takes some memory, and the amount depends on what the system has — how many addresses, routes etc.

There is a way to ignore routing tables, if there is no need to work with them, e.g. IPDB(ignore_rtables=[255]); that will save some memory by ignoring automatic kernel routes.

But yes, if you need it only for quick simple operations, the IPRoute() object will work a way better and faster, and will consume significantly less memory.
Reply all
Reply to author
Forward
0 new messages