Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 1 | """ |
| 2 | IP Routes |
| 3 | |
| 4 | object abstractions for representing IP routes in VPP |
| 5 | """ |
| 6 | |
| 7 | import socket |
| 8 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 9 | # from vnet/vnet/mpls/mpls_types.h |
| 10 | MPLS_IETF_MAX_LABEL = 0xfffff |
| 11 | MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1 |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 12 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 13 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 14 | class RoutePath: |
| 15 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 16 | def __init__( |
| 17 | self, |
| 18 | nh_addr, |
| 19 | nh_sw_if_index, |
| 20 | nh_table_id=0, |
| 21 | labels=[], |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 22 | nh_via_label=MPLS_LABEL_INVALID, |
| 23 | is_ip6=0): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 24 | self.nh_itf = nh_sw_if_index |
| 25 | self.nh_table_id = nh_table_id |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 26 | self.nh_via_label = nh_via_label |
| 27 | self.nh_labels = labels |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 28 | 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 Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | class IpRoute: |
| 35 | """ |
| 36 | IP Route |
| 37 | """ |
| 38 | |
| 39 | def __init__(self, test, dest_addr, |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 40 | dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0): |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 41 | self._test = test |
| 42 | self.paths = paths |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 43 | self.dest_addr_len = dest_addr_len |
| 44 | self.table_id = table_id |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 45 | 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 Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 51 | |
| 52 | def add_vpp_config(self): |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 53 | if self.is_local: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 54 | self._test.vapi.ip_add_del_route( |
| 55 | self.dest_addr, |
| 56 | self.dest_addr_len, |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 57 | socket.inet_pton(socket.AF_INET6, "::"), |
| 58 | 0xffffffff, |
| 59 | is_local=1, |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 60 | table_id=self.table_id, |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 61 | 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 Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 75 | |
| 76 | def remove_vpp_config(self): |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 77 | 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 Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | class MplsIpBind: |
| 98 | """ |
| 99 | MPLS to IP Binding |
| 100 | """ |
| 101 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 102 | def __init__(self, test, local_label, dest_addr, dest_addr_len): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 103 | 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 | |
| 120 | class 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 Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 134 | 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 Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 146 | |
| 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) |