blob: fc9133fbec635824b4941743c61215f41b7a85d7 [file] [log] [blame]
Neale Ranns177bbdc2016-11-15 09:46:51 +00001"""
2 IP Routes
3
4 object abstractions for representing IP routes in VPP
5"""
6
7import socket
8
Neale Rannsad422ed2016-11-02 14:20:04 +00009# from vnet/vnet/mpls/mpls_types.h
10MPLS_IETF_MAX_LABEL = 0xfffff
11MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000012
Neale Ranns177bbdc2016-11-15 09:46:51 +000013
Neale Rannsad422ed2016-11-02 14:20:04 +000014class RoutePath:
15
Klement Sekerada505f62017-01-04 12:58:53 +010016 def __init__(
17 self,
18 nh_addr,
19 nh_sw_if_index,
20 nh_table_id=0,
21 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -080022 nh_via_label=MPLS_LABEL_INVALID,
23 is_ip6=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +000024 self.nh_itf = nh_sw_if_index
25 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +000026 self.nh_via_label = nh_via_label
27 self.nh_labels = labels
Neale Rannsfca0c242017-01-13 07:57:46 -080028 if is_ip6:
29 self.nh_addr = socket.inet_pton(socket.AF_INET6, nh_addr)
30 else:
31 self.nh_addr = socket.inet_pton(socket.AF_INET, nh_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +000032
33
34class IpRoute:
35 """
36 IP Route
37 """
38
39 def __init__(self, test, dest_addr,
Neale Rannsfca0c242017-01-13 07:57:46 -080040 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +000041 self._test = test
42 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +000043 self.dest_addr_len = dest_addr_len
44 self.table_id = table_id
Neale Rannsfca0c242017-01-13 07:57:46 -080045 self.is_ip6 = is_ip6
46 self.is_local = is_local
47 if is_ip6:
48 self.dest_addr = socket.inet_pton(socket.AF_INET6, dest_addr)
49 else:
50 self.dest_addr = socket.inet_pton(socket.AF_INET, dest_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +000051
52 def add_vpp_config(self):
Neale Rannsfca0c242017-01-13 07:57:46 -080053 if self.is_local:
Klement Sekerada505f62017-01-04 12:58:53 +010054 self._test.vapi.ip_add_del_route(
55 self.dest_addr,
56 self.dest_addr_len,
Neale Rannsfca0c242017-01-13 07:57:46 -080057 socket.inet_pton(socket.AF_INET6, "::"),
58 0xffffffff,
59 is_local=1,
Klement Sekerada505f62017-01-04 12:58:53 +010060 table_id=self.table_id,
Neale Rannsfca0c242017-01-13 07:57:46 -080061 is_ipv6=self.is_ip6)
62 else:
63 for path in self.paths:
64 self._test.vapi.ip_add_del_route(
65 self.dest_addr,
66 self.dest_addr_len,
67 path.nh_addr,
68 path.nh_itf,
69 table_id=self.table_id,
70 next_hop_out_label_stack=path.nh_labels,
71 next_hop_n_out_labels=len(
72 path.nh_labels),
73 next_hop_via_label=path.nh_via_label,
74 is_ipv6=self.is_ip6)
Neale Ranns177bbdc2016-11-15 09:46:51 +000075
76 def remove_vpp_config(self):
Neale Rannsfca0c242017-01-13 07:57:46 -080077 if self.is_local:
78 self._test.vapi.ip_add_del_route(
79 self.dest_addr,
80 self.dest_addr_len,
81 socket.inet_pton(socket.AF_INET6, "::"),
82 0xffffffff,
83 is_local=1,
84 is_add=0,
85 table_id=self.table_id,
86 is_ipv6=self.is_ip6)
87 else:
88 for path in self.paths:
89 self._test.vapi.ip_add_del_route(self.dest_addr,
90 self.dest_addr_len,
91 path.nh_addr,
92 path.nh_itf,
93 table_id=self.table_id,
94 is_add=0)
Neale Rannsad422ed2016-11-02 14:20:04 +000095
96
97class MplsIpBind:
98 """
99 MPLS to IP Binding
100 """
101
Klement Sekerada505f62017-01-04 12:58:53 +0100102 def __init__(self, test, local_label, dest_addr, dest_addr_len):
Neale Rannsad422ed2016-11-02 14:20:04 +0000103 self._test = test
104 self.dest_addr = socket.inet_pton(socket.AF_INET, dest_addr)
105 self.dest_addr_len = dest_addr_len
106 self.local_label = local_label
107
108 def add_vpp_config(self):
109 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
110 self.dest_addr,
111 self.dest_addr_len)
112
113 def remove_vpp_config(self):
114 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
115 self.dest_addr,
116 self.dest_addr_len,
117 is_bind=0)
118
119
120class MplsRoute:
121 """
122 MPLS Route
123 """
124
125 def __init__(self, test, local_label, eos_bit, paths, table_id=0):
126 self._test = test
127 self.paths = paths
128 self.local_label = local_label
129 self.eos_bit = eos_bit
130 self.table_id = table_id
131
132 def add_vpp_config(self):
133 for path in self.paths:
Klement Sekerada505f62017-01-04 12:58:53 +0100134 self._test.vapi.mpls_route_add_del(
135 self.local_label,
136 self.eos_bit,
137 1,
138 path.nh_addr,
139 path.nh_itf,
140 table_id=self.table_id,
141 next_hop_out_label_stack=path.nh_labels,
142 next_hop_n_out_labels=len(
143 path.nh_labels),
144 next_hop_via_label=path.nh_via_label,
145 next_hop_table_id=path.nh_table_id)
Neale Rannsad422ed2016-11-02 14:20:04 +0000146
147 def remove_vpp_config(self):
148 for path in self.paths:
149 self._test.vapi.mpls_route_add_del(self.local_label,
150 self.eos_bit,
151 1,
152 path.nh_addr,
153 path.nh_itf,
154 table_id=self.table_id,
155 is_add=0)