]> git.proxmox.com Git - mirror_ifupdown2.git/blame - ifupdown/rtnetlink_api.py
Multiple fixes and cleanup
[mirror_ifupdown2.git] / ifupdown / rtnetlink_api.py
CommitLineData
e74d01e1
RP
1#!/usr/bin/env python
2#
3# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4#
5# Author: Roopa Prabhu, roopa@cumulusnetworks.com
6#
7#
8
9from os import getpid
10from socket import AF_UNSPEC
11from iff import IFF_UP
12from rtnetlink import *
13import os
84ca006f 14import ifupdownmain
e74d01e1
RP
15
16class rtnetlinkApi(RtNetlink):
17
18 bind_done = False
19
20 def __init__(self, pid):
21 RtNetlink.__init__(self, pid)
22 self.logger = logging.getLogger('ifupdown.' +
23 self.__class__.__name__)
24 self.bind(0, None)
25 self.bind_done = True
26 self.ifindexmap = {}
27
28 def do_bind(self):
29 if self.bind_done:
30 return True
31 self.bind(0, None)
32 self.bind_done = True
33
34 def get_ifindex(self, ifname):
35 ifindex = self.ifindexmap.get(ifname)
36 if not ifindex:
37 with open('/sys/class/net/%s/ifindex' %ifname, 'r') as f:
38 ifindex = int(f.read())
39 self.ifindexmap[ifname] = ifindex
40 return ifindex
41
42 def create_vlan(self, link, ifname, vlanid):
a070c90e 43 self.logger.info('rtnetlink: creating vlan interface %s' %ifname)
84ca006f
RP
44 if ifupdownmain.ifupdownFlags.DRYRUN:
45 return
e74d01e1
RP
46 try:
47 ifindex = self.get_ifindex(link)
48 except Exception, e:
84ca006f
RP
49 raise Exception('cannot determine ifindex for link %s (%s)'
50 %(link, str(e)))
e74d01e1 51
e74d01e1
RP
52 ifm = Ifinfomsg(AF_UNSPEC)
53 rtas = {IFLA_IFNAME: ifname,
54 IFLA_LINK : ifindex,
55 IFLA_LINKINFO : {
56 IFLA_INFO_KIND : 'vlan',
57 IFLA_INFO_DATA : {
58 IFLA_VLAN_ID : vlanid,
59 }
60 }
61 }
84ca006f
RP
62 token = self.request(RTM_NEWLINK,
63 NLM_F_CREATE | NLM_F_REQUEST | NLM_F_ACK, ifm, rtas)
e74d01e1
RP
64 self.process_wait([token])
65
66 def create_macvlan(self, ifname, link, mode='private'):
a070c90e 67 self.logger.info('rtnetlink: creating macvlan interface %s' %ifname)
84ca006f
RP
68 if ifupdownmain.ifupdownFlags.DRYRUN:
69 return
e74d01e1
RP
70 try:
71 ifindex = self.get_ifindex(link)
72 except Exception, e:
84ca006f
RP
73 raise Exception('cannot determine ifindex for link %s (%s)'
74 %(link, str(e)))
e74d01e1
RP
75
76 ifm = Ifinfomsg(AF_UNSPEC)
77 rtas = {IFLA_IFNAME: ifname,
78 IFLA_LINK : ifindex,
79 IFLA_LINKINFO : {
80 IFLA_INFO_KIND : 'macvlan',
81 IFLA_INFO_DATA : {
82 IFLA_MACVLAN_MODE : MACVLAN_MODE_PRIVATE,
83 }
84 }
85 }
84ca006f
RP
86 token = self.request(RTM_NEWLINK, NLM_F_CREATE | NLM_F_REQUEST |
87 NLM_F_ACK, ifm, rtas)
e74d01e1
RP
88 self.process_wait([token])
89
90 def link_set(self, ifname, state):
91 flags = 0
e74d01e1 92 self.logger.info('rtnetlink: setting link %s %s' %(ifname, state))
84ca006f
RP
93 if ifupdownmain.ifupdownFlags.DRYRUN:
94 return
e74d01e1
RP
95
96 if state == "up":
97 flags |= IFF_UP
98 else:
99 flags &= ~IFF_UP
100
101 ifm = Ifinfomsg(AF_UNSPEC, ifi_change=IFF_UP, ifi_flags=flags)
102 rtas = {IFLA_IFNAME: ifname}
103
104 token = self.request(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK, ifm, rtas)
105 self.process_wait([token])
106
107rtnl_api = rtnetlinkApi(os.getpid())