summaryrefslogtreecommitdiff
path: root/neutron/agent/linux/ip_lib.py
blob: ff121580fe51f9ba08821c4c2083f79acf011f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import errno
from os import path
import re
import threading
import time

import eventlet
import netaddr
from neutron_lib import constants
from neutron_lib import exceptions
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import netutils
from pyroute2.netlink import exceptions \
    as netlink_exceptions  # pylint: disable=no-name-in-module
from pyroute2.netlink import rtnl  # pylint: disable=no-name-in-module
from pyroute2.netlink.rtnl import \
    ifaddrmsg  # pylint: disable=no-name-in-module
from pyroute2.netlink.rtnl import ifinfmsg  # pylint: disable=no-name-in-module
from pyroute2 import netns  # pylint: disable=no-name-in-module

from neutron._i18n import _
from neutron.agent.common import utils
from neutron.common import utils as common_utils
from neutron.privileged.agent.linux import ip_lib as privileged
from neutron.privileged.agent.linux import utils as priv_utils

LOG = logging.getLogger(__name__)


IP_NONLOCAL_BIND = 'net.ipv4.ip_nonlocal_bind'

LOOPBACK_DEVNAME = 'lo'
FB_TUNNEL_DEVICE_NAMES = ['gre0', 'gretap0', 'tunl0', 'erspan0', 'sit0',
                          'ip6tnl0', 'ip6gre0']
IP_RULE_TABLES = {'default': 253,
                  'main': 254,
                  'local': 255}

IP_RULE_TABLES_NAMES = {v: k for k, v in IP_RULE_TABLES.items()}

# Rule indexes: pyroute2.netlink.rtnl
# Rule names: https://www.systutorials.com/docs/linux/man/8-ip-rule/
# NOTE(ralonsoh): 'masquerade' type is printed as 'nat' in 'ip rule' command
IP_RULE_TYPES = {0: 'unspecified',
                 1: 'unicast',
                 6: 'blackhole',
                 7: 'unreachable',
                 8: 'prohibit',
                 10: 'nat'}

IP_ADDRESS_SCOPE = {rtnl.rtscopes['RT_SCOPE_UNIVERSE']: 'global',
                    rtnl.rtscopes['RT_SCOPE_SITE']: 'site',
                    rtnl.rtscopes['RT_SCOPE_LINK']: 'link',
                    rtnl.rtscopes['RT_SCOPE_HOST']: 'host'}

IP_ADDRESS_SCOPE_NAME = {v: k for k, v in IP_ADDRESS_SCOPE.items()}

IP_ADDRESS_EVENTS = {'RTM_NEWADDR': 'added',
                     'RTM_DELADDR': 'removed'}

SYS_NET_PATH = '/sys/class/net'
DEFAULT_GW_PATTERN = re.compile(r"via (\S+)")
METRIC_PATTERN = re.compile(r"metric (\S+)")
DEVICE_NAME_PATTERN = re.compile(r"(\d+?): (\S+?):.*")

# NOTE: no metric is interpreted by the kernel as having the highest priority
# (value 0). "ip route" uses the netlink API to communicate with the kernel. In
# IPv6, when the metric value is not set is translated as 1024 as default:
# https://access.redhat.com/solutions/3659171
IP_ROUTE_METRIC_DEFAULT = {constants.IP_VERSION_4: 0,
                           constants.IP_VERSION_6: 1024}


def remove_interface_suffix(interface):
    """Remove a possible "<if>@<endpoint>" suffix from an interface' name.

    This suffix can appear in some kernel versions, and intends on specifying,
    for example, a veth's pair. However, this interface name is useless to us
    as further 'ip' commands require that the suffix be removed.
    """
    # If '@' is not present, this will do nothing.
    return interface.partition("@")[0]


class AddressNotReady(exceptions.NeutronException):
    message = _("Failure waiting for address %(address)s to "
                "become ready: %(reason)s")


class DADFailed(AddressNotReady):
    pass


InvalidArgument = privileged.InvalidArgument


class SubProcessBase(object):
    def __init__(self, namespace=None,
                 log_fail_as_error=True):
        self.namespace = namespace
        self.log_fail_as_error = log_fail_as_error
        try:
            self.force_root = cfg.CONF.ip_lib_force_root
        except cfg.NoSuchOptError:
            # Only callers that need to force use of the root helper
            # need to register the option.
            self.force_root = False

    def _run(self, options, command, args):
        if self.namespace:
            return self._as_root(options, command, args)
        elif self.force_root:
            # Force use of the root helper to ensure that commands
            # will execute in dom0 when running under XenServer/XCP.
            return self._execute(options, command, args, run_as_root=True)
        else:
            return self._execute(options, command, args)

    def _as_root(self, options, command, args, use_root_namespace=False):
        namespace = self.namespace if not use_root_namespace else None

        return self._execute(options, command, args, run_as_root=True,
                             namespace=namespace)

    def _execute(self, options, command, args, run_as_root=False,
                 namespace=None):
        opt_list = ['-%s' % o for o in options]
        ip_cmd = add_namespace_to_cmd(['ip'], namespace)
        cmd = ip_cmd + opt_list + [command] + list(args)
        return utils.execute(cmd, run_as_root=run_as_root, privsep_exec=True,
                             log_fail_as_error=self.log_fail_as_error)

    def set_log_fail_as_error(self, fail_with_error):
        self.log_fail_as_error = fail_with_error

    def get_log_fail_as_error(self):
        return self.log_fail_as_error


class IPWrapper(SubProcessBase):
    def __init__(self, namespace=None):
        super(IPWrapper, self).__init__(namespace=namespace)
        self.netns = IpNetnsCommand(self)

    def device(self, name):
        return IPDevice(name, namespace=self.namespace)

    def get_devices_info(self, exclude_loopback=True,
                         exclude_fb_tun_devices=True):
        devices = get_devices_info(self.namespace)

        retval = []
        for device in devices:
            if (exclude_loopback and device['name'] == LOOPBACK_DEVNAME or
                    exclude_fb_tun_devices and
                    device['name'] in FB_TUNNEL_DEVICE_NAMES):
                continue
            retval.append(device)
        return retval

    def get_devices(self, exclude_loopback=True, exclude_fb_tun_devices=True):
        retval = []
        try:
            devices = privileged.get_device_names(self.namespace)
        except privileged.NetworkNamespaceNotFound:
            return retval

        for name in devices:
            if (exclude_loopback and name == LOOPBACK_DEVNAME or
                    exclude_fb_tun_devices and name in FB_TUNNEL_DEVICE_NAMES):
                continue
            retval.append(IPDevice(name, namespace=self.namespace))
        return retval

    def get_device_by_ip(self, ip):
        """Get the IPDevice from system which has ip configured.

        @param ip: look for the device holding this ip. If this is None,
                   None is returned.
        @type ip: str.
        """
        if not ip:
            return None

        cidr = common_utils.ip_to_cidr(ip)
        kwargs = {'address': common_utils.cidr_to_ip(cidr)}
        if not common_utils.is_cidr_host(cidr):
            kwargs['mask'] = common_utils.cidr_mask_length(cidr)
        devices = get_devices_with_ip(self.namespace, **kwargs)
        if not devices:
            # Search by broadcast address.
            broadcast = common_utils.cidr_broadcast_address(cidr)
            if broadcast:
                devices = get_devices_with_ip(self.namespace,
                                              broadcast=broadcast)

        if devices:
            return IPDevice(devices[0]['name'], namespace=self.namespace)

    def add_tuntap(self, name, mode='tap'):
        privileged.create_interface(
            name, self.namespace, "tuntap", mode=mode)
        return IPDevice(name, namespace=self.namespace)

    def add_veth(self, name1, name2, namespace2=None):
        peer = {'ifname': name2}

        if namespace2 is None:
            namespace2 = self.namespace
        else:
            self.ensure_namespace(namespace2)
            peer['net_ns_fd'] = namespace2

        privileged.create_interface(
            name1, self.namespace, 'veth', peer=peer)

        return (IPDevice(name1, namespace=self.namespace),
                IPDevice(name2, namespace=namespace2))

    def add_macvtap(self, name, src_dev, mode='bridge'):
        privileged.create_interface(name,
                                    self.namespace,
                                    "macvtap",
                                    physical_interface=src_dev,
                                    mode=mode)
        return IPDevice(name, namespace=self.namespace)

    def del_veth(self, name):
        """Delete a virtual interface between two namespaces."""
        privileged.delete_interface(name, self.namespace)

    def add_dummy(self, name):
        """Create a Linux dummy interface with the given name."""
        privileged.create_interface(name, self.namespace, "dummy")
        return IPDevice(name, namespace=self.namespace)

    def ensure_namespace(self, name):
        if not self.netns.exists(name):
            ip = self.netns.add(name)
            lo = ip.device(LOOPBACK_DEVNAME)
            lo.link.set_up()
        else:
            ip = IPWrapper(namespace=name)
        return ip

    def namespace_is_empty(self):
        return not self.get_devices()

    def garbage_collect_namespace(self):
        """Conditionally destroy the namespace if it is empty."""
        if self.namespace and self.netns.exists(self.namespace):
            if self.namespace_is_empty():
                self.netns.delete(self.namespace)
                return True
        return False

    def add_device_to_namespace(self, device):
        if self.namespace:
            device.link.set_netns(self.namespace)

    def add_vlan(self, name, physical_interface, vlan_id):
        privileged.create_interface(name,
                                    self.namespace,
                                    "vlan",
                                    physical_interface=physical_interface,
                                    vlan_id=vlan_id)
        return IPDevice(name, namespace=self.namespace)

    def add_vxlan(self, name, vni, dev, group=None, ttl=None, tos=None,
                  local=None, srcport=None, dstport=None, proxy=False):
        kwargs = {'vxlan_id': vni,
                  'physical_interface': dev}
        if group:
            try:
                ip_version = common_utils.get_ip_version(group)
                if ip_version == constants.IP_VERSION_6:
                    kwargs['vxlan_group6'] = group
                else:
                    kwargs['vxlan_group'] = group
            except netaddr.core.AddrFormatError:
                err_msg = _("Invalid group address: %s") % group
                raise exceptions.InvalidInput(error_message=err_msg)
        if dev:
            kwargs['physical_interface'] = dev
        if ttl:
            kwargs['vxlan_ttl'] = ttl
        if tos:
            kwargs['vxlan_tos'] = tos
        if local:
            try:
                ip_version = common_utils.get_ip_version(local)
                if ip_version == constants.IP_VERSION_6:
                    kwargs['vxlan_local6'] = local
                else:
                    kwargs['vxlan_local'] = local
            except netaddr.core.AddrFormatError:
                err_msg = _("Invalid local address: %s") % local
                raise exceptions.InvalidInput(error_message=err_msg)
        if proxy:
            kwargs['vxlan_proxy'] = proxy
        # tuple: min,max
        if srcport:
            if len(srcport) == 2 and srcport[0] <= srcport[1]:
                kwargs['vxlan_port_range'] = (str(srcport[0]), str(srcport[1]))
            else:
                raise exceptions.NetworkVxlanPortRangeError(
                    vxlan_range=srcport)
        if dstport:
            kwargs['vxlan_port'] = dstport
        privileged.create_interface(name, self.namespace, "vxlan", **kwargs)
        return (IPDevice(name, namespace=self.namespace))


class IPDevice(SubProcessBase):
    def __init__(self, name, namespace=None, kind='link'):
        super(IPDevice, self).__init__(namespace=namespace)
        self._name = name
        self.kind = kind
        self.link = IpLinkCommand(self)
        self.addr = IpAddrCommand(self)
        self.route = IpRouteCommand(self)
        self.neigh = IpNeighCommand(self)

    def __eq__(self, other):
        return (other is not None and self.name == other.name and
                self.namespace == other.namespace)

    def __str__(self):
        return self.name

    def __repr__(self):
        return "<IPDevice(name=%s, namespace=%s)>" % (self._name,
                                                      self.namespace)

    def exists(self):
        """Return True if the device exists in the namespace."""
        return privileged.interface_exists(self.name, self.namespace)

    def delete_addr_and_conntrack_state(self, cidr):
        """Delete an address along with its conntrack state

        This terminates any active connections through an IP.

        :param cidr: the IP address for which state should be removed.
            This can be passed as a string with or without /NN.
            A netaddr.IPAddress or netaddr.Network representing the IP address
            can also be passed.
        """
        self.addr.delete(cidr)
        self.delete_conntrack_state(cidr)

    def delete_conntrack_state(self, cidr):
        """Delete conntrack state rules

        Deletes both rules (if existing), the destination and the reply one.
        """
        ip_str = str(netaddr.IPNetwork(cidr).ip)
        ip_wrapper = IPWrapper(namespace=self.namespace)

        # Delete conntrack state for ingress traffic
        # If 0 flow entries have been deleted
        # conntrack -D will return 1
        try:
            ip_wrapper.netns.execute(["conntrack", "-D", "-d", ip_str],
                                     check_exit_code=True,
                                     extra_ok_codes=[1], privsep_exec=True)

        except RuntimeError:
            LOG.exception("Failed deleting ingress connection state of"
                          " floatingip %s", ip_str)

        # Delete conntrack state for egress traffic
        try:
            ip_wrapper.netns.execute(["conntrack", "-D", "-q", ip_str],
                                     check_exit_code=True,
                                     extra_ok_codes=[1], privsep_exec=True)
        except RuntimeError:
            LOG.exception("Failed deleting egress connection state of"
                          " floatingip %s", ip_str)

    def delete_socket_conntrack_state(self, cidr, dport, protocol):
        ip_str = str(netaddr.IPNetwork(cidr).ip)
        ip_wrapper = IPWrapper(namespace=self.namespace)
        cmd = ["conntrack", "-D", "-d", ip_str, '-p', protocol,
               '--dport', dport]
        try:
            ip_wrapper.netns.execute(cmd, check_exit_code=True,
                                     extra_ok_codes=[1], privsep_exec=True)

        except RuntimeError:
            LOG.exception("Failed deleting ingress connection state of "
                          "socket %(ip)s:%(port)s", {'ip': ip_str,
                                                     'port': dport})

    def disable_ipv6(self):
        if not netutils.is_ipv6_enabled():
            return
        sysctl_name = re.sub(r'\.', '/', self.name)
        cmd = ['net.ipv6.conf.%s.disable_ipv6=1' % sysctl_name]
        return sysctl(cmd, namespace=self.namespace)

    @property
    def name(self):
        if self._name:
            return self._name[:constants.DEVICE_NAME_MAX_LEN]
        return self._name

    @name.setter
    def name(self, name):
        self._name = name


class IpDeviceCommandBase(object):

    def __init__(self, parent):
        self._parent = parent

    @property
    def name(self):
        return self._parent.name

    @property
    def kind(self):
        return self._parent.kind


class IpLinkCommand(IpDeviceCommandBase):

    def set_address(self, mac_address):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, address=mac_address)

    def set_allmulticast_on(self):
        privileged.set_link_flags(
            self.name, self._parent.namespace, ifinfmsg.IFF_ALLMULTI)

    def set_mtu(self, mtu_size):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, mtu=mtu_size)

    def set_up(self):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, state='up')

    def set_down(self):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, state='down')

    def set_netns(self, namespace):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, net_ns_fd=namespace)
        self._parent.namespace = namespace
        common_utils.wait_until_true(lambda: self.exists, timeout=5,
                                     sleep=0.5)

    def set_name(self, name):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, ifname=name)
        self._parent.name = name

    def set_alias(self, alias_name):
        privileged.set_link_attribute(
            self.name, self._parent.namespace, ifalias=alias_name)

    def create(self):
        privileged.create_interface(self.name, self._parent.namespace,
                                    self.kind)

    def delete(self):
        privileged.delete_interface(self.name, self._parent.namespace)

    @property
    def address(self):
        return self.attributes.get('link/ether')

    @property
    def state(self):
        return self.attributes.get('state')

    @property
    def allmulticast(self):
        return self.attributes.get('allmulticast')

    @property
    def mtu(self):
        return self.attributes.get('mtu')

    @property
    def qdisc(self):
        return self.attributes.get('qdisc')

    @property
    def qlen(self):
        return self.attributes.get('qlen')

    @property
    def alias(self):
        return self.attributes.get('alias')

    @property
    def link_kind(self):
        return self.attributes.get('link_kind')

    @property
    def attributes(self):
        return privileged.get_link_attributes(self.name,
                                              self._parent.namespace)

    @property
    def exists(self):
        return privileged.interface_exists(self.name, self._parent.namespace)

    def get_vfs(self):
        return privileged.get_link_vfs(self.name, self._parent.namespace)

    def set_vf_feature(self, vf_config):
        return privileged.set_link_vf_feature(
            self.name, self._parent.namespace, vf_config)


class IpAddrCommand(IpDeviceCommandBase):

    def add(self, cidr, scope='global', add_broadcast=True):
        add_ip_address(cidr, self.name, self._parent.namespace, scope,
                       add_broadcast)

    def delete(self, cidr):
        delete_ip_address(cidr, self.name, self._parent.namespace)

    def flush(self, ip_version):
        flush_ip_addresses(ip_version, self.name, self._parent.namespace)

    def list(self, scope=None, to=None, filters=None, ip_version=None):
        """Get device details of a device named <self.name>."""
        def filter_device(device, filters):
            # Accepted filters: dynamic, permanent, tentative, dadfailed.
            for filter in filters:
                if filter == 'permanent' and device['dynamic']:
                    return False
                elif not device[filter]:
                    return False
            return True

        kwargs = {}
        if to:
            cidr = common_utils.ip_to_cidr(to)
            kwargs = {'address': common_utils.cidr_to_ip(cidr)}
            if not common_utils.is_cidr_host(cidr):
                kwargs['mask'] = common_utils.cidr_mask_length(cidr)
        if scope:
            kwargs['scope'] = scope
        if ip_version:
            kwargs['family'] = common_utils.get_socket_address_family(
                ip_version)

        devices = get_devices_with_ip(self._parent.namespace, name=self.name,
                                      **kwargs)
        if not filters:
            return devices

        filtered_devices = []
        for device in (device for device in devices
                       if filter_device(device, filters)):
            filtered_devices.append(device)

        return filtered_devices

    def wait_until_address_ready(self, address, wait_time=30):
        """Wait until an address is no longer marked 'tentative' or 'dadfailed'

        raises AddressNotReady if times out, address not present on interface
        raises DADFailed if Duplicate Address Detection fails
        """
        def is_address_ready():
            try:
                addr_info = self.list(to=address)[0]
            except IndexError:
                raise AddressNotReady(
                    address=address,
                    reason=_('Address not present on interface'))
            # Since both 'dadfailed' and 'tentative' will be set if DAD fails,
            # check 'dadfailed' first just to be explicit
            if addr_info['dadfailed']:
                raise DADFailed(
                    address=address, reason=_('Duplicate address detected'))
            if addr_info['tentative']:
                return False
            return True
        errmsg = _("Exceeded %s second limit waiting for "
                   "address to leave the tentative state.") % wait_time
        common_utils.wait_until_true(
            is_address_ready, timeout=wait_time, sleep=0.20,
            exception=AddressNotReady(address=address, reason=errmsg))


class IpRouteCommand(IpDeviceCommandBase):

    def __init__(self, parent, table=None):
        super(IpRouteCommand, self).__init__(parent)
        self._table = table

    def add_gateway(self, gateway, metric=None, table=None, scope='global'):
        self.add_route(None, via=gateway, table=table, metric=metric,
                       scope=scope)

    def delete_gateway(self, gateway, table=None, scope=None):
        self.delete_route(None, device=self.name, via=gateway, table=table,
                          scope=scope)

    def list_routes(self, ip_version, scope=None, via=None, table=None,
                    **kwargs):
        table = table or self._table
        return list_ip_routes(self._parent.namespace, ip_version, scope=scope,
                              via=via, table=table, device=self.name, **kwargs)

    def list_onlink_routes(self, ip_version):
        routes = self.list_routes(ip_version, scope='link')
        return [r for r in routes if not r['source_prefix']]

    def add_onlink_route(self, cidr):
        self.add_route(cidr, scope='link')

    def delete_onlink_route(self, cidr):
        self.delete_route(cidr, device=self.name, scope='link')

    def get_gateway(self, scope=None, table=None,
                    ip_version=constants.IP_VERSION_4):
        routes = self.list_routes(ip_version, scope=scope, table=table)
        for route in routes:
            if route['via'] and route['cidr'] in constants.IP_ANY.values():
                return route

    def flush(self, ip_version, table=None, **kwargs):
        for route in self.list_routes(ip_version, table=table):
            self.delete_route(route['cidr'], device=route['device'],
                              via=route['via'], table=table, **kwargs)

    def add_route(self, cidr, via=None, table=None, metric=None, scope=None,
                  **kwargs):
        table = table or self._table
        add_ip_route(self._parent.namespace, cidr, device=self.name, via=via,
                     table=table, metric=metric, scope=scope, **kwargs)

    def delete_route(self, cidr, device=None, via=None, table=None, scope=None,
                     **kwargs):
        table = table or self._table
        delete_ip_route(self._parent.namespace, cidr, device=device, via=via,
                        table=table, scope=scope, **kwargs)


class IPRoute(SubProcessBase):
    def __init__(self, namespace=None, table=None):
        super(IPRoute, self).__init__(namespace=namespace)
        self.name = None
        self.route = IpRouteCommand(self, table=table)


class IpNeighCommand(IpDeviceCommandBase):

    def add(self, ip_address, mac_address, nud_state=None, **kwargs):
        add_neigh_entry(ip_address,
                        mac_address,
                        self.name,
                        namespace=self._parent.namespace,
                        nud_state=nud_state,
                        **kwargs)

    def delete(self, ip_address, mac_address, **kwargs):
        delete_neigh_entry(ip_address,
                           mac_address,
                           self.name,
                           self._parent.namespace,
                           **kwargs)

    def dump(self, ip_version, **kwargs):
        return dump_neigh_entries(ip_version,
                                  self.name,
                                  self._parent.namespace,
                                  **kwargs)

    def flush(self, ip_version, ip_address):
        """Flush neighbour entries

        Given address entry is removed from neighbour cache (ARP or NDP). To
        flush all entries pass string 'all' as an address.

        From https://man.archlinux.org/man/core/iproute2/ip-neighbour.8.en:
          "the default neighbour states to be flushed do not include permanent
           and noarp".

        :param ip_version: Either 4 or 6 for IPv4 or IPv6 respectively
        :param ip_address: The prefix selecting the neighbours to flush or
                           "all"
        """
        cidr = netaddr.IPNetwork(ip_address) if ip_address != 'all' else None
        for entry in self.dump(ip_version):
            if entry['state'] in ('permanent', 'noarp'):
                continue
            if ip_address == 'all' or entry['dst'] in cidr:
                self.delete(entry['dst'], entry['lladdr'])


class IpNetnsCommand(object):

    def __init__(self, parent):
        self._parent = parent

    def add(self, name):
        create_network_namespace(name)
        wrapper = IPWrapper(namespace=name)
        wrapper.netns.execute(['sysctl', '-w',
                               'net.ipv4.conf.all.promote_secondaries=1'],
                              privsep_exec=True)
        return wrapper

    def delete(self, name):
        delete_network_namespace(name)

    def execute(self, cmds, addl_env=None, check_exit_code=True,
                log_fail_as_error=True, extra_ok_codes=None,
                run_as_root=False, privsep_exec=False):
        ns_params = []
        if self._parent.namespace:
            run_as_root = True
            ns_params = ['ip', 'netns', 'exec', self._parent.namespace]

        env_params = []
        if addl_env:
            env_params = (['env'] +
                          ['%s=%s' % pair for pair in addl_env.items()])
        cmd = ns_params + env_params + list(cmds)
        return utils.execute(cmd, check_exit_code=check_exit_code,
                             extra_ok_codes=extra_ok_codes,
                             log_fail_as_error=log_fail_as_error,
                             run_as_root=run_as_root,
                             privsep_exec=privsep_exec)

    def exists(self, name):
        return network_namespace_exists(name)


def vlan_in_use(segmentation_id, namespace=None):
    """Return True if VLAN ID is in use by an interface, else False."""
    interfaces = get_devices_info(namespace)
    vlans = {interface.get('vlan_id') for interface in interfaces
             if interface.get('vlan_id')}
    return segmentation_id in vlans


def vxlan_in_use(segmentation_id, namespace=None):
    """Return True if VXLAN VNID is in use by an interface, else False."""
    interfaces = get_devices_info(namespace)
    vxlans = {interface.get('vxlan_id') for interface in interfaces
              if interface.get('vxlan_id')}
    return segmentation_id in vxlans


def device_exists(device_name, namespace=None):
    """Return True if the device exists in the namespace."""
    return IPDevice(device_name, namespace=namespace).exists()


def device_exists_with_ips_and_mac(device_name, ip_cidrs, mac, namespace=None):
    """Return True if the device with the given IP addresses and MAC address
    exists in the namespace.
    """
    try:
        device = IPDevice(device_name, namespace=namespace)
        if mac and mac != device.link.address:
            return False
        device_ip_cidrs = [ip['cidr'] for ip in device.addr.list()]
        for ip_cidr in ip_cidrs:
            if ip_cidr not in device_ip_cidrs:
                return False
    except RuntimeError:
        return False
    else:
        return True


def get_device_mac(device_name, namespace=None):
    """Return the MAC address of the device."""
    return IPDevice(device_name, namespace=namespace).link.address


def get_device_mtu(device_name, namespace=None):
    """Return the MTU value of the device."""
    return IPDevice(device_name, namespace=namespace).link.mtu


NetworkNamespaceNotFound = privileged.NetworkNamespaceNotFound
NetworkInterfaceNotFound = privileged.NetworkInterfaceNotFound
IpAddressAlreadyExists = privileged.IpAddressAlreadyExists


def add_ip_address(cidr, device, namespace=None, scope='global',
                   add_broadcast=True):
    """Add an IP address.

    :param cidr: IP address to add, in CIDR notation
    :param device: Device name to use in adding address
    :param namespace: The name of the namespace in which to add the address
    :param scope: scope of address being added
    :param add_broadcast: should broadcast address be added
    """
    net = netaddr.IPNetwork(cidr)
    broadcast = None
    if add_broadcast and net.version == 4:
        # NOTE(slaweq): in case if cidr is /32 net.broadcast is None so
        # same IP address as cidr should be set as broadcast
        broadcast = str(net.broadcast or net.ip)
    privileged.add_ip_address(
        net.version, str(net.ip), net.prefixlen,
        device, namespace, scope, broadcast)


def delete_ip_address(cidr, device, namespace=None):
    """Delete an IP address.

    :param cidr: IP address to delete, in CIDR notation
    :param device: Device name to use in deleting address
    :param namespace: The name of the namespace in which to delete the address
    """
    net = netaddr.IPNetwork(cidr)
    privileged.delete_ip_address(
        net.version, str(net.ip), net.prefixlen, device, namespace)


def flush_ip_addresses(ip_version, device, namespace=None):
    """Flush all IP addresses.

    :param ip_version: IP version of addresses to flush
    :param device: Device name to use in flushing addresses
    :param namespace: The name of the namespace in which to flush the addresses
    """
    privileged.flush_ip_addresses(ip_version, device, namespace)


def get_routing_table(ip_version, namespace=None):
    """Return a list of dictionaries, each representing a route.

    @param ip_version: the routes of version to return, for example 4
    @param namespace
    @return: a list of dictionaries, each representing a route.
    The dictionary format is: {'destination': cidr,
                               'nexthop': ip,
                               'device': device_name,
                               'scope': scope}
    """
    # oslo.privsep turns lists to tuples in its IPC code. Change it back
    return list(privileged.get_routing_table(ip_version, namespace))


# NOTE(haleyb): These neighbour functions live outside the IpNeighCommand
# class since not all callers require it.
def add_neigh_entry(ip_address, mac_address, device, namespace=None,
                    nud_state=None, **kwargs):
    """Add a neighbour entry.

    :param ip_address: IP address of entry to add
    :param mac_address: MAC address of entry to add
    :param device: Device name to use in adding entry
    :param namespace: The name of the namespace in which to add the entry
    :param nud_state: The NUD (Neighbour Unreachability Detection) state of
                      the entry; defaults to "permanent"
    """
    ip_version = common_utils.get_ip_version(ip_address)
    nud_state = nud_state or 'permanent'
    privileged.add_neigh_entry(ip_version,
                               ip_address,
                               mac_address,
                               device,
                               namespace,
                               nud_state,
                               **kwargs)


def delete_neigh_entry(ip_address, mac_address, device, namespace=None,
                       **kwargs):
    """Delete a neighbour entry.

    :param ip_address: IP address of entry to delete
    :param mac_address: MAC address of entry to delete
    :param device: Device name to use in deleting entry
    :param namespace: The name of the namespace in which to delete the entry
    """
    ip_version = common_utils.get_ip_version(ip_address)
    privileged.delete_neigh_entry(ip_version,
                                  ip_address,
                                  mac_address,
                                  device,
                                  namespace,
                                  **kwargs)


def dump_neigh_entries(ip_version, device=None, namespace=None, **kwargs):
    """Dump all neighbour entries.

    :param ip_version: IP version of entries to show (4 or 6)
    :param device: Device name to use in dumping entries
    :param namespace: The name of the namespace in which to dump the entries
    :param kwargs: Callers add any filters they use as kwargs
    :return: a list of dictionaries, each representing a neighbour.
    The dictionary format is: {'dst': ip_address,
                               'lladdr': mac_address,
                               'device': device_name}
    """
    return list(privileged.dump_neigh_entries(ip_version,
                                              device,
                                              namespace,
                                              **kwargs))


def create_network_namespace(namespace, **kwargs):
    """Create a network namespace.

    :param namespace: The name of the namespace to create
    :param kwargs: Callers add any filters they use as kwargs
    """
    privileged.create_netns(namespace, **kwargs)


def delete_network_namespace(namespace, **kwargs):
    """Delete a network namespace.

    :param namespace: The name of the namespace to delete
    :param kwargs: Callers add any filters they use as kwargs
    """
    privileged.remove_netns(namespace, **kwargs)


def list_network_namespaces(**kwargs):
    """List all network namespace entries.

    :param kwargs: Callers add any filters they use as kwargs
    """
    if cfg.CONF.AGENT.use_helper_for_ns_read:
        return privileged.list_netns(**kwargs)
    else:
        return netns.listnetns(**kwargs)


def network_namespace_exists(namespace, try_is_ready=False, **kwargs):
    """Check if a network namespace exists.

    :param namespace: The name of the namespace to check
    :param try_is_ready: Try to open the namespace to know if the namespace
                         is ready to be operated.
    :param kwargs: Callers add any filters they use as kwargs
    """
    if not namespace:
        return False

    if not try_is_ready:
        nspath = kwargs.get('nspath') or netns.NETNS_RUN_DIR
        nspath += '/' + namespace
        if cfg.CONF.AGENT.use_helper_for_ns_read:
            return priv_utils.path_exists(nspath)
        else:
            return path.exists(nspath)

    try:
        privileged.open_namespace(namespace)
        return True
    except (RuntimeError, OSError):
        pass
    return False


def list_namespace_pids(namespace):
    """List namespace process PIDs

    :param namespace: (string) the name of the namespace
    :return: (tuple)
    """
    return privileged.list_ns_pids(namespace)


def ensure_device_is_ready(device_name, namespace=None):
    dev = IPDevice(device_name, namespace=namespace)
    try:
        # Ensure the device has a MAC address and is up, even if it is already
        # up.
        if not dev.link.exists or not dev.link.address:
            LOG.info("Device %s cannot be used as it has no MAC "
                     "address", device_name)
            return False
        dev.link.set_up()
    except RuntimeError:
        return False
    return True


def iproute_arg_supported(command, arg):
    command += ['help']
    stdout, stderr = utils.execute(command, check_exit_code=False,
                                   return_stderr=True, log_fail_as_error=False)
    return any(arg in line for line in stderr.split('\n'))


def _arping(ns_name, iface_name, address, count, log_exception):
    # Due to a Linux kernel bug*, it's advised to spread gratuitous updates
    # more, injecting an interval between consequent packets that is longer
    # than 1s which is currently hardcoded** in arping. To achieve that, we
    # call arping tool the 'count' number of times, each issuing a single ARP
    # update, and wait between iterations.
    #
    # *  https://patchwork.ozlabs.org/patch/760372/
    # ** https://github.com/iputils/iputils/pull/86
    first = True
    # Since arping is used to send gratuitous ARP, a response is
    # not expected. In some cases (no response) and with some
    # platforms (>=Ubuntu 14.04), arping exit code can be 1.
    extra_ok_codes = [1]
    ip_wrapper = IPWrapper(namespace=ns_name)
    for i in range(count):
        if not first:
            # hopefully enough for kernel to get out of locktime loop
            time.sleep(2)
            # On the second (and subsequent) arping calls, we can get a
            # "bind: Cannot assign requested address" error since
            # the IP address might have been deleted concurrently.
            # We will log an error below if this isn't the case, so
            # no need to have execute() log one as well.
            extra_ok_codes = [1, 2]
        first = False

        # some Linux kernels* don't honour REPLYs. Send both gratuitous REQUEST
        # and REPLY packets (REQUESTs are left for backwards compatibility for
        # in case if some network peers, vice versa, honor REPLYs and not
        # REQUESTs)
        #
        # * https://patchwork.ozlabs.org/patch/763016/
        for arg in ('-U', '-A'):
            arping_cmd = ['arping', arg, '-I', iface_name, '-c', 1,
                          # Pass -w to set timeout to ensure exit if interface
                          # removed while running
                          '-w', 2, address]
            try:
                ip_wrapper.netns.execute(arping_cmd,
                                         extra_ok_codes=extra_ok_codes,
                                         privsep_exec=True)
            except Exception as exc:
                # Since this is spawned in a thread and executed 2 seconds
                # apart, something may have been deleted while we were
                # sleeping. Downgrade message to info and return early
                # unless it was the first try.
                exists = device_exists_with_ips_and_mac(iface_name,
                                                        [address],
                                                        mac=None,
                                                        namespace=ns_name)
                msg = ("Failed sending gratuitous ARP to %(addr)s on "
                       "%(iface)s in namespace %(ns)s: %(err)s")
                logger_method = LOG.exception
                if not (log_exception and (first or exists)):
                    logger_method = LOG.info
                logger_method(msg, {'addr': address,
                                    'iface': iface_name,
                                    'ns': ns_name,
                                    'err': exc})
                if not exists:
                    LOG.info("Interface %(iface)s or address %(addr)s "
                             "in namespace %(ns)s was deleted concurrently",
                             {'iface': iface_name,
                              'addr': address,
                              'ns': ns_name})
                    return


def send_ip_addr_adv_notif(
        ns_name, iface_name, address, count=3, log_exception=True,
        use_eventlet=True):
    """Send advance notification of an IP address assignment.

    If the address is in the IPv4 family, send gratuitous ARP.

    If the address is in the IPv6 family, no advance notification is
    necessary, since the Neighbor Discovery Protocol (NDP), Duplicate
    Address Discovery (DAD), and (for stateless addresses) router
    advertisements (RAs) are sufficient for address resolution and
    duplicate address detection.

    :param ns_name: Namespace name which GARPs are gonna be sent from.
    :param iface_name: Name of interface which GARPs are gonna be sent from.
    :param address: Advertised IP address.
    :param count: (Optional) How many GARPs are gonna be sent. Default is 3.
    :param log_exception: (Optional) True if possible failures should be logged
                          on exception level. Otherwise they are logged on
                          WARNING level. Default is True.
    :param use_eventlet: (Optional) True if the arping command will be spawned
                         using eventlet, False to use Python threads
                         (threading).
    """
    def arping():
        _arping(ns_name, iface_name, address, count, log_exception)

    if count > 0 and netaddr.IPAddress(address).version == 4:
        if use_eventlet:
            eventlet.spawn_n(arping)
        else:
            threading.Thread(target=arping).start()


def sysctl(cmd, namespace=None, log_fail_as_error=True):
    """Run sysctl command 'cmd'

    @param cmd: a list containing the sysctl command to run
    @param namespace: network namespace to run command in
    @param log_fail_as_error: failure logged as LOG.error

    execute() doesn't return the exit status of the command it runs,
    it returns stdout and stderr. Setting check_exit_code=True will cause
    it to raise a RuntimeError if the exit status of the command is
    non-zero, which in sysctl's case is an error. So we're normalizing
    that into zero (success) and one (failure) here to mimic what
    "echo $?" in a shell would be.

    This is all because sysctl is too verbose and prints the value you
    just set on success, unlike most other utilities that print nothing.

    execute() will have dumped a message to the logs with the actual
    output on failure, so it's not lost, and we don't need to print it
    here.
    """
    cmd = ['sysctl', '-w'] + cmd
    ip_wrapper = IPWrapper(namespace=namespace)
    try:
        ip_wrapper.netns.execute(cmd, run_as_root=True,
                                 log_fail_as_error=log_fail_as_error,
                                 privsep_exec=True)
    except RuntimeError as rte:
        LOG.warning(
            "Setting %(cmd)s in namespace %(ns)s failed: %(err)s.",
            {'cmd': cmd,
             'ns': namespace,
             'err': rte})
        return 1

    return 0


def add_namespace_to_cmd(cmd, namespace=None):
    """Add an optional namespace to the command."""

    return ['ip', 'netns', 'exec', namespace] + cmd if namespace else cmd


def get_ipv6_lladdr(mac_addr):
    return '%s/64' % netaddr.EUI(mac_addr).ipv6_link_local()


def get_ip_nonlocal_bind(namespace=None):
    """Get kernel option value of ip_nonlocal_bind in given namespace."""
    cmd = ['sysctl', '-bn', IP_NONLOCAL_BIND]
    ip_wrapper = IPWrapper(namespace)
    return int(ip_wrapper.netns.execute(cmd, run_as_root=True,
                                        privsep_exec=True))


def set_ip_nonlocal_bind(value, namespace=None, log_fail_as_error=True):
    """Set sysctl knob of ip_nonlocal_bind to given value."""
    cmd = ['%s=%d' % (IP_NONLOCAL_BIND, value)]
    return sysctl(cmd, namespace=namespace,
                  log_fail_as_error=log_fail_as_error)


def set_ip_nonlocal_bind_for_namespace(namespace, value, root_namespace=False):
    """Set ip_nonlocal_bind but don't raise exception on failure."""
    failed = set_ip_nonlocal_bind(value, namespace=namespace,
                                  log_fail_as_error=False)
    if failed and root_namespace:
        # Somewhere in the 3.19 kernel timeframe ip_nonlocal_bind was
        # changed to be a per-namespace attribute.  To be backwards
        # compatible we need to try both if at first we fail.
        LOG.debug('Namespace (%s) does not support setting %s, '
                  'trying in root namespace', namespace, IP_NONLOCAL_BIND)
        return set_ip_nonlocal_bind(value)
    if failed:
        LOG.warning(
            "%s will not be set to %d in the root namespace in order to "
            "not break DVR, which requires this value be set to 1. This "
            "may introduce a race between moving a floating IP to a "
            "different network node, and the peer side getting a "
            "populated ARP cache for a given floating IP address.",
            IP_NONLOCAL_BIND, value)


def get_ipv6_forwarding(device, namespace=None):
    """Get kernel value of IPv6 forwarding for device in given namespace."""
    cmd = ['sysctl', '-b', "net.ipv6.conf.%s.forwarding" % device]
    ip_wrapper = IPWrapper(namespace)
    return int(ip_wrapper.netns.execute(cmd, run_as_root=True,
                                        privsep_exec=True))


def _parse_ip_rule(rule, ip_version):
    """Parse a pyroute2 rule and returns a dictionary

    Parameters contained in the returned dictionary:
    - priority: rule priority
    - from: source IP address
    - to: (optional) destination IP address
    - type: rule type (see RULE_TYPES)
    - table: table name or number (see RULE_TABLES)
    - fwmark: (optional) FW mark
    - iif: (optional) input interface name
    - oif: (optional) output interface name

     :param rule: pyroute2 rule dictionary
     :param ip_version: IP version (4, 6)
     :return: dictionary with IP rule information
    """
    parsed_rule = {'priority': str(rule['attrs'].get('FRA_PRIORITY', 0))}
    from_ip = rule['attrs'].get('FRA_SRC')
    if from_ip:
        parsed_rule['from'] = common_utils.ip_to_cidr(
            from_ip, prefix=rule['src_len'])
        if common_utils.is_cidr_host(parsed_rule['from']):
            parsed_rule['from'] = common_utils.cidr_to_ip(parsed_rule['from'])
    else:
        parsed_rule['from'] = constants.IP_ANY[ip_version]
    to_ip = rule['attrs'].get('FRA_DST')
    if to_ip:
        parsed_rule['to'] = common_utils.ip_to_cidr(
            to_ip, prefix=rule['dst_len'])
        if common_utils.is_cidr_host(parsed_rule['to']):
            parsed_rule['to'] = common_utils.cidr_to_ip(parsed_rule['to'])
    parsed_rule['type'] = IP_RULE_TYPES[rule['action']]
    table_num = rule['attrs']['FRA_TABLE']
    for table_name in (name for (name, index) in
                       IP_RULE_TABLES.items() if index == table_num):
        parsed_rule['table'] = table_name
        break
    else:
        parsed_rule['table'] = str(table_num)
    fwmark = rule['attrs'].get('FRA_FWMARK')
    if fwmark:
        fwmask = rule['attrs'].get('FRA_FWMASK')
        parsed_rule['fwmark'] = '{0:#x}/{1:#x}'.format(fwmark, fwmask)
    iifname = rule['attrs'].get('FRA_IIFNAME')
    if iifname:
        parsed_rule['iif'] = iifname
    oifname = rule['attrs'].get('FRA_OIFNAME')
    if oifname:
        parsed_rule['oif'] = oifname

    return parsed_rule


def list_ip_rules(namespace, ip_version):
    """List all IP rules in a namespace

    :param namespace: namespace name
    :param ip_version: IP version (4, 6)
    :return: list of dictionaries with the rules information
    """
    rules = privileged.list_ip_rules(namespace, ip_version)
    return [_parse_ip_rule(rule, ip_version) for rule in rules]


def _make_pyroute2_args(ip, iif, table, priority, to):
    """Returns a dictionary of arguments to be used in pyroute rule commands

    :param ip: (string) source IP or CIDR address (IPv4, IPv6)
    :param iif: (string) input interface name
    :param table: (string, int) table number (as an int or a string) or table
                  name ('default', 'main', 'local')
    :param priority: (string, int) rule priority
    :param to: (string) destination IP or CIDR address (IPv4, IPv6)
    :return: a dictionary with the kwargs needed in pyroute rule commands
    """
    ip_version = common_utils.get_ip_version(ip)
    # In case we need to add a rule based on an incoming interface, no
    # IP address is given; the rule default source ("from") address is
    # "all".
    cmd_args = {'family': common_utils.get_socket_address_family(ip_version)}
    if iif:
        cmd_args['iifname'] = iif
    else:
        cmd_args['src'] = common_utils.cidr_to_ip(ip)
        cmd_args['src_len'] = common_utils.cidr_mask(ip)
    if to:
        cmd_args['dst'] = common_utils.cidr_to_ip(to)
        cmd_args['dst_len'] = common_utils.cidr_mask(to)
    if table:
        cmd_args['table'] = IP_RULE_TABLES.get(table) or int(table)
    if priority:
        cmd_args['priority'] = int(priority)
    return cmd_args


def _exist_ip_rule(rules, ip, iif, table, priority, to):
    """Check if any rule matches the conditions"""
    for rule in rules:
        if iif and rule.get('iif') != iif:
            continue
        if not iif and rule['from'] != ip:
            continue
        if table and rule.get('table') != str(table):
            continue
        if priority and rule['priority'] != str(priority):
            continue
        if to and rule.get('to') != to:
            continue
        break
    else:
        return False
    return True


def add_ip_rule(namespace, ip, iif=None, table=None, priority=None, to=None):
    """Create an IP rule in a namespace

    :param namespace: (string) namespace name
    :param ip: (string) source IP or CIDR address (IPv4, IPv6)
    :param iif: (Optional) (string) input interface name
    :param table: (Optional) (string, int) table number
    :param priority: (Optional) (string, int) rule priority
    :param to: (Optional) (string) destination IP or CIDR address (IPv4, IPv6)
    """
    ip_version = common_utils.get_ip_version(ip)
    rules = list_ip_rules(namespace, ip_version)
    if _exist_ip_rule(rules, ip, iif, table, priority, to):
        return
    cmd_args = _make_pyroute2_args(ip, iif, table, priority, to)
    privileged.add_ip_rule(namespace, **cmd_args)


def delete_ip_rule(namespace, ip, iif=None, table=None, priority=None,
                   to=None):
    """Delete an IP rule in a namespace

    :param namespace: (string) namespace name
    :param ip: (string) source IP or CIDR address (IPv4, IPv6)
    :param iif: (Optional) (string) input interface name
    :param table: (Optional) (string, int) table number
    :param priority: (Optional) (string, int) rule priority
    :param to: (Optional) (string) destination IP or CIDR address (IPv4, IPv6)
    """
    cmd_args = _make_pyroute2_args(ip, iif, table, priority, to)
    privileged.delete_ip_rule(namespace, **cmd_args)


def get_attr(pyroute2_obj, attr_name):
    """Get an attribute from a PyRoute2 object"""
    rule_attrs = pyroute2_obj.get('attrs', [])
    for attr in (attr for attr in rule_attrs if attr[0] == attr_name):
        return attr[1]


def _parse_ip_address(pyroute2_address, device_name):
    ip = get_attr(pyroute2_address, 'IFA_ADDRESS')
    ip_length = pyroute2_address['prefixlen']
    event = IP_ADDRESS_EVENTS.get(pyroute2_address.get('event'))
    cidr = common_utils.ip_to_cidr(ip, prefix=ip_length)
    flags = get_attr(pyroute2_address, 'IFA_FLAGS')
    dynamic = not bool(flags & ifaddrmsg.IFA_F_PERMANENT)
    tentative = bool(flags & ifaddrmsg.IFA_F_TENTATIVE)
    dadfailed = bool(flags & ifaddrmsg.IFA_F_DADFAILED)
    scope = IP_ADDRESS_SCOPE[pyroute2_address['scope']]
    return {'name': device_name,
            'cidr': cidr,
            'scope': scope,
            'broadcast': get_attr(pyroute2_address, 'IFA_BROADCAST'),
            'dynamic': dynamic,
            'tentative': tentative,
            'dadfailed': dadfailed,
            'event': event}


def get_devices_with_ip(namespace, name=None, **kwargs):
    retval = []
    link_args = {}
    if name:
        link_args['ifname'] = name
    scope = kwargs.pop('scope', None)
    if scope:
        kwargs['scope'] = IP_ADDRESS_SCOPE_NAME[scope]

    if not link_args:
        ip_addresses = privileged.get_ip_addresses(namespace, **kwargs)
    else:
        device = get_devices_info(namespace, **link_args)
        if not device:
            return retval
        ip_addresses = privileged.get_ip_addresses(
            namespace, index=device[0]['index'], **kwargs)

    devices = {}  # {device index: name}
    for ip_address in ip_addresses:
        index = ip_address['index']
        name = get_attr(ip_address, 'IFA_LABEL') or devices.get(index)
        if not name:
            device = get_devices_info(namespace, index=index)
            if not device:
                continue
            name = device[0]['name']

        retval.append(_parse_ip_address(ip_address, name))
        devices[index] = name

    return retval


def get_devices_info(namespace, **kwargs):
    devices = privileged.get_link_devices(namespace, **kwargs)
    retval = {}
    for device in devices:
        ret = {'index': device['index'],
               'name': get_attr(device, 'IFLA_IFNAME'),
               'operstate': get_attr(device, 'IFLA_OPERSTATE'),
               'linkmode': get_attr(device, 'IFLA_LINKMODE'),
               'mtu': get_attr(device, 'IFLA_MTU'),
               'promiscuity': get_attr(device, 'IFLA_PROMISCUITY'),
               'mac': get_attr(device, 'IFLA_ADDRESS'),
               'broadcast': get_attr(device, 'IFLA_BROADCAST')}
        ifla_link = get_attr(device, 'IFLA_LINK')
        if ifla_link:
            ret['parent_index'] = ifla_link
        ifla_linkinfo = get_attr(device, 'IFLA_LINKINFO')
        if ifla_linkinfo:
            ret['kind'] = get_attr(ifla_linkinfo, 'IFLA_INFO_KIND')
            ifla_data = get_attr(ifla_linkinfo, 'IFLA_INFO_DATA')
            if ret['kind'] == 'vxlan':
                ret['vxlan_id'] = get_attr(ifla_data, 'IFLA_VXLAN_ID')
                ret['vxlan_group'] = get_attr(ifla_data, 'IFLA_VXLAN_GROUP')
                ret['vxlan_link_index'] = get_attr(ifla_data,
                                                   'IFLA_VXLAN_LINK')
            elif ret['kind'] == 'vlan':
                ret['vlan_id'] = get_attr(ifla_data, 'IFLA_VLAN_ID')
            elif ret['kind'] == 'bridge':
                ret['stp'] = get_attr(ifla_data, 'IFLA_BR_STP_STATE')
                ret['forward_delay'] = get_attr(ifla_data,
                                                'IFLA_BR_FORWARD_DELAY')
        retval[device['index']] = ret

    for device in retval.values():
        if device.get('parent_index'):
            parent_device = retval.get(device['parent_index'])
            if parent_device:
                device['parent_name'] = parent_device['name']
        elif device.get('vxlan_link_index'):
            device['vxlan_link_name'] = (
                retval[device['vxlan_link_index']]['name'])

    return list(retval.values())


def ip_monitor(namespace, queue, event_stop, event_started):
    """Monitor IP address changes

    If namespace is not None, this function must be executed as root user, but
    cannot use privsep because is a blocking function and can exhaust the
    number of working threads.
    """
    def get_device_name(index):
        try:
            with privileged.get_iproute(namespace) as ip:
                device = ip.link('get', index=index)
                if device:
                    attrs = device[0].get('attrs', [])
                    for attr in (attr for attr in attrs
                                 if attr[0] == 'IFLA_IFNAME'):
                        return attr[1]
        except netlink_exceptions.NetlinkError as e:
            if e.code == errno.ENODEV:
                return
            raise

    def read_ip_updates(_ip, _queue):
        """Read Pyroute2.IPRoute input socket

        The aim of this function is to open and bind an IPRoute socket only for
        reading the netlink changes; no other operations are done with this
        opened socket. This function is executed in a separate thread,
        dedicated only to this task.
        """
        _ip.bind(async_cache=True)
        try:
            while True:
                ip_addresses = _ip.get()
                for ip_address in ip_addresses:
                    _queue.put(ip_address)
        except EOFError:
            pass

    _queue = eventlet.Queue()
    try:
        cache_devices = {}
        with privileged.get_iproute(namespace) as ip:
            for device in ip.get_links():
                cache_devices[device['index']] = get_attr(device,
                                                          'IFLA_IFNAME')
        _ip = privileged.get_iproute(namespace)
        ip_updates_thread = threading.Thread(target=read_ip_updates,
                                             args=(_ip, _queue))
        ip_updates_thread.start()
        event_started.set()
        while not event_stop.is_set():
            try:
                ip_address = _queue.get(timeout=1)
            except eventlet.queue.Empty:
                continue
            if 'index' in ip_address and 'prefixlen' in ip_address:
                index = ip_address['index']
                name = (get_device_name(index) or
                        cache_devices.get(index))
                if not name:
                    continue

                cache_devices[index] = name
                queue.put(_parse_ip_address(ip_address, name))

        _ip.close()
        ip_updates_thread.join(timeout=5)

    except OSError as e:
        if e.errno == errno.ENOENT:
            raise privileged.NetworkNamespaceNotFound(netns_name=namespace)
        raise


def add_ip_route(namespace, cidr, device=None, via=None, table=None,
                 metric=None, scope=None, proto='static', **kwargs):
    """Add an IP route"""
    if table:
        table = IP_RULE_TABLES.get(table, table)
    ip_version = common_utils.get_ip_version(cidr or via)
    privileged.add_ip_route(namespace, cidr, ip_version,
                            device=device, via=via, table=table,
                            metric=metric, scope=scope, proto=proto, **kwargs)


def list_ip_routes(namespace, ip_version, scope=None, via=None, table=None,
                   device=None, **kwargs):
    """List IP routes"""
    def get_device(index, devices):
        for device in (d for d in devices if d['index'] == index):
            return get_attr(device, 'IFLA_IFNAME')

    def get_proto(proto_number):
        if proto_number in rtnl.rt_proto:
            return rtnl.rt_proto[proto_number]
        elif str(proto_number) in constants.IP_PROTOCOL_NUM_TO_NAME_MAP:
            return constants.IP_PROTOCOL_NUM_TO_NAME_MAP[str(proto_number)]

    table = table if table else 'main'
    table = IP_RULE_TABLES.get(table, table)
    routes = privileged.list_ip_routes(namespace, ip_version, device=device,
                                       table=table, **kwargs)
    devices = privileged.get_link_devices(namespace)
    ret = []
    for route in routes:
        cidr = get_attr(route, 'RTA_DST')
        if cidr:
            cidr = '%s/%s' % (cidr, route['dst_len'])
        else:
            cidr = constants.IP_ANY[ip_version]
        table = int(get_attr(route, 'RTA_TABLE'))
        metric = (get_attr(route, 'RTA_PRIORITY') or
                  IP_ROUTE_METRIC_DEFAULT[ip_version])
        proto = get_proto(route['proto'])
        value = {
            'table': IP_RULE_TABLES_NAMES.get(table, table),
            'source_prefix': get_attr(route, 'RTA_PREFSRC'),
            'cidr': cidr,
            'scope': IP_ADDRESS_SCOPE[int(route['scope'])],
            'metric': metric,
            'proto': proto,
        }

        multipath = get_attr(route, 'RTA_MULTIPATH')
        if multipath:
            value['device'] = None
            mp_via = []
            for mp in multipath:
                mp_via.append({'device': get_device(int(mp['oif']), devices),
                               'via': get_attr(mp, 'RTA_GATEWAY'),
                               'weight': int(mp['hops']) + 1})
            value['via'] = mp_via
        else:
            value['device'] = get_device(int(get_attr(route, 'RTA_OIF')),
                                         devices)
            value['via'] = get_attr(route, 'RTA_GATEWAY')

        ret.append(value)

    if scope:
        ret = [route for route in ret if route['scope'] == scope]
    if via:
        ret = [route for route in ret if route['via'] == via]

    return ret


def delete_ip_route(namespace, cidr, device=None, via=None, table=None,
                    scope=None, **kwargs):
    """Delete an IP route"""
    if table:
        table = IP_RULE_TABLES.get(table, table)
    ip_version = common_utils.get_ip_version(cidr or via)
    privileged.delete_ip_route(namespace, cidr, ip_version,
                               device=device, via=via, table=table,
                               scope=scope, **kwargs)