summaryrefslogtreecommitdiffstats
path: root/src/vnet/geneve/geneve.h
blob: 3bb9083fbd5592d989e73e1491e6cc1b40a71837 (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
/*
 * Copyright (c) 2017 SUSE LLC.
 * 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.
 */
#ifndef included_vnet_geneve_h
#define included_vnet_geneve_h

#include <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/l2_output.h>
#include <vnet/l2/l2_bd.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/geneve/geneve_packet.h>
#include <vnet/ip/ip4_packet.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/udp/udp.h>
#include <vnet/dpo/dpo.h>
#include <vnet/adj/adj_types.h>

#define SUPPORT_OPTIONS_HEADER 0

typedef CLIB_PACKED (struct
		     {
		     ip4_header_t ip4;	/* 20 bytes */
		     udp_header_t udp;	/* 8 bytes */
		     geneve_header_t geneve;	/* Min 8 bytes, Max 260 bytes */
		     }) ip4_geneve_header_t;

typedef CLIB_PACKED (struct
		     {
		     ip6_header_t ip6;	/* 40 bytes */
		     udp_header_t udp;	/* 8 bytes */
		     geneve_header_t geneve;	/* Min 8 bytes, Max 260 bytes */
		     }) ip6_geneve_header_t;

typedef CLIB_PACKED (struct
		     {
		     /*
		      * Key fields: ip source and geneve vni on incoming GENEVE packet
		      * all fields in NET byte order
		      */
		     union
		     {
		     struct
		     {
		     u32 remote;
		     u32 vni;	/* shifted left 8 bits */
		     };
		     u64 as_u64;
		     };
		     }) geneve4_tunnel_key_t;

typedef CLIB_PACKED (struct
		     {
		     /*
		      * Key fields: ip source and geneve vni on incoming GENEVE packet
		      * all fields in NET byte order
		      */
		     ip6_address_t remote;
		     u32 vni;	/* shifted left 8 bits */
		     }) geneve6_tunnel_key_t;

typedef struct
{
  u32 tunnel_index;
  u32 vni;
} geneve_encap_trace_t;

typedef struct
{
  /* Required for pool_get_aligned */
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);

  /* Rewrite string. $$$$ embed vnet_rewrite header */
  u8 *rewrite;

  /* FIB DPO for IP forwarding of GENEVE encap packet */
  dpo_id_t next_dpo;

  /* geneve VNI in HOST byte order */
  u32 vni;

  /* geneve OPTIONS LEN in HOST byte order */
#if SUPPORT_OPTIONS_HEADER==1
  u8 options_len;
#endif

  /* tunnel local and remote addresses */
  ip46_address_t local;
  ip46_address_t remote;

  /* mcast packet output intfc index (used only if remote is mcast) */
  u32 mcast_sw_if_index;

  /* decap next index */
  u32 decap_next_index;

  /* The FIB index for local/remote addresses */
  u32 encap_fib_index;

  /* vnet intfc index */
  u32 sw_if_index;
  u32 hw_if_index;

  /**
   * Linkage into the FIB object graph
   */
  fib_node_t node;

  /*
   * The FIB entry for (depending on GENEVE tunnel is unicast or mcast)
   * sending unicast GENEVE encap packets or receiving mcast GENEVE packets
   */
  fib_node_index_t fib_entry_index;
  adj_index_t mcast_adj_index;

  /**
   * The tunnel is a child of the FIB entry for its desintion. This is
   * so it receives updates when the forwarding information for that entry
   * changes.
   * The tunnels sibling index on the FIB entry's dependency list.
   */
  u32 sibling_index;
} geneve_tunnel_t;

#define foreach_geneve_input_next        \
_(DROP, "error-drop")                   \
_(L2_INPUT, "l2-input")

typedef enum
{
#define _(s,n) GENEVE_INPUT_NEXT_##s,
  foreach_geneve_input_next
#undef _
    GENEVE_INPUT_N_NEXT,
} geneve_input_next_t;

typedef enum
{
#define geneve_error(n,s) GENEVE_ERROR_##n,
#include <vnet/geneve/geneve_error.def>
#undef geneve_error
  GENEVE_N_ERROR,
} geneve_input_error_t;

typedef struct
{
  /* vector of encap tunnel instances */
  geneve_tunnel_t *tunnels;

  /* lookup tunnel by key */
  uword *geneve4_tunnel_by_key;	/* keyed on ipv4.remote + vni */
  uword *geneve6_tunnel_by_key;	/* keyed on ipv6.remote + vni */

  /* local VTEP IPs ref count used by geneve-bypass node to check if
     received GENEVE packet DIP matches any local VTEP address */
  uword *vtep4;			/* local ip4 VTEPs keyed on their ip4 addr */
  uword *vtep6;			/* local ip6 VTEPs keyed on their ip6 addr */

  /* mcast shared info */
  uword *mcast_shared;		/* keyed on mcast ip46 addr */

  /* Free vlib hw_if_indices */
  u32 *free_geneve_tunnel_hw_if_indices;

  /* Mapping from sw_if_index to tunnel index */
  u32 *tunnel_index_by_sw_if_index;

  /* convenience */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} geneve_main_t;

extern geneve_main_t geneve_main;

extern vlib_node_registration_t geneve4_input_node;
extern vlib_node_registration_t geneve6_input_node;
extern vlib_node_registration_t geneve4_encap_node;
extern vlib_node_registration_t geneve6_encap_node;

u8 *format_geneve_encap_trace (u8 * s, va_list * args);

typedef struct
{
  u8 is_add;

  /* we normally use is_ip4, but since this adds to the
   * structure, this seems less of abreaking change */
  u8 is_ip6;
  ip46_address_t local, remote;
  u32 mcast_sw_if_index;
  u32 encap_fib_index;
  u32 decap_next_index;
  u32 vni;
} vnet_geneve_add_del_tunnel_args_t;

int vnet_geneve_add_del_tunnel
  (vnet_geneve_add_del_tunnel_args_t * a, u32 * sw_if_indexp);

void vnet_int_geneve_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable);
#endif /* included_vnet_geneve_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
an> = 0 UNIFORM = 1 def find_route(test, ip_addr, len, table_id=0, inet=AF_INET): if inet == AF_INET: s = 4 routes = test.vapi.ip_fib_dump() else: s = 16 routes = test.vapi.ip6_fib_dump() route_addr = inet_pton(inet, ip_addr) for e in routes: if route_addr == e.address[:s] \ and len == e.address_length \ and table_id == e.table_id: return True return False def find_mroute(test, grp_addr, src_addr, grp_addr_len, table_id=0, inet=AF_INET): if inet == AF_INET: s = 4 routes = test.vapi.ip_mfib_dump() else: s = 16 routes = test.vapi.ip6_mfib_dump() gaddr = inet_pton(inet, grp_addr) saddr = inet_pton(inet, src_addr) for e in routes: if gaddr == e.grp_address[:s] \ and grp_addr_len == e.address_length \ and saddr == e.src_address[:s] \ and table_id == e.table_id: return True return False class VppIpTable(VppObject): def __init__(self, test, table_id, is_ip6=0): self._test = test self.table_id = table_id self.is_ip6 = is_ip6 def add_vpp_config(self): self._test.vapi.ip_table_add_del( self.table_id, is_ipv6=self.is_ip6, is_add=1) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): self._test.vapi.ip_table_add_del( self.table_id, is_ipv6=self.is_ip6, is_add=0) def query_vpp_config(self): # find the default route return find_route(self._test, "::" if self.is_ip6 else "0.0.0.0", 0, self.table_id, inet=AF_INET6 if self.is_ip6 == 1 else AF_INET) def __str__(self): return self.object_id() def object_id(self): return ("table-%s-%d" % ("v6" if self.is_ip6 == 1 else "v4", self.table_id)) class VppMplsLabel(object): def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0): self.value = value self.mode = mode self.ttl = ttl self.exp = exp def encode(self): is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1 return {'label': self.value, 'ttl': self.ttl, 'exp': self.exp, 'is_uniform': is_uniform} class VppRoutePath(object): def __init__( self, nh_addr, nh_sw_if_index, nh_table_id=0, labels=[], nh_via_label=MPLS_LABEL_INVALID, rpf_id=0, is_interface_rx=0, is_resolve_host=0, is_resolve_attached=0, is_source_lookup=0, is_udp_encap=0, is_dvr=0, next_hop_id=0xffffffff, proto=DpoProto.DPO_PROTO_IP4): self.nh_itf = nh_sw_if_index self.nh_table_id = nh_table_id self.nh_via_label = nh_via_label self.nh_labels = labels self.weight = 1 self.rpf_id = rpf_id self.proto = proto if self.proto is DpoProto.DPO_PROTO_IP6: self.nh_addr = inet_pton(AF_INET6, nh_addr) elif self.proto is DpoProto.DPO_PROTO_IP4: self.nh_addr = inet_pton(AF_INET, nh_addr) else: self.nh_addr = inet_pton(AF_INET6, "::") self.is_resolve_host = is_resolve_host self.is_resolve_attached = is_resolve_attached self.is_interface_rx = is_interface_rx self.is_source_lookup = is_source_lookup self.is_rpf_id = 0 if rpf_id != 0: self.is_rpf_id = 1 self.nh_itf = rpf_id self.is_udp_encap = is_udp_encap self.next_hop_id = next_hop_id self.is_dvr = is_dvr def encode_labels(self): lstack = [] for l in self.nh_labels: if type(l) == VppMplsLabel: lstack.append(l.encode()) else: lstack.append({'label': l, 'ttl': 255}) return lstack def encode(self): return {'next_hop': self.nh_addr, 'weight': 1, 'afi': 0, 'preference': 0, 'table_id': self.nh_table_id, 'next_hop_id': self.next_hop_id, 'sw_if_index': self.nh_itf, 'afi': self.proto, 'is_udp_encap': self.is_udp_encap, 'n_labels': len(self.nh_labels), 'label_stack': self.encode_labels()} class VppMRoutePath(VppRoutePath): def __init__(self, nh_sw_if_index, flags, nh=None, proto=DpoProto.DPO_PROTO_IP4, bier_imp=0): if not nh: nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0" super(VppMRoutePath, self).__init__(nh, nh_sw_if_index, proto=proto) self.nh_i_flags = flags self.bier_imp = bier_imp class VppIpRoute(VppObject): """ IP Route """ def __init__(self, test, dest_addr, dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0, is_unreach=0, is_prohibit=0): self._test = test self.paths = paths self.dest_addr_len = dest_addr_len self.table_id = table_id self.is_ip6 = is_ip6 self.is_local = is_local self.is_unreach = is_unreach self.is_prohibit = is_prohibit self.dest_addr_p = dest_addr if is_ip6: self.dest_addr = inet_pton(AF_INET6, dest_addr) else: self.dest_addr = inet_pton(AF_INET, dest_addr) def modify(self, paths, is_local=0, is_unreach=0, is_prohibit=0): self.paths = paths self.is_local = is_local self.is_unreach = is_unreach self.is_prohibit = is_prohibit def add_vpp_config(self): if self.is_local or self.is_unreach or self.is_prohibit: self._test.vapi.ip_add_del_route( self.dest_addr, self.dest_addr_len, inet_pton(AF_INET6, "::"), 0xffffffff, is_local=self.is_local, is_unreach=self.is_unreach, is_prohibit=self.is_prohibit, table_id=self.table_id, is_ipv6=self.is_ip6) else: for path in self.paths: lstack = path.encode_labels() self._test.vapi.ip_add_del_route( self.dest_addr, self.dest_addr_len, path.nh_addr, path.nh_itf, table_id=self.table_id, next_hop_out_label_stack=lstack, next_hop_n_out_labels=len(lstack), next_hop_via_label=path.nh_via_label, next_hop_table_id=path.nh_table_id, next_hop_id=path.next_hop_id, is_ipv6=self.is_ip6, is_dvr=path.is_dvr, is_resolve_host=path.is_resolve_host, is_resolve_attached=path.is_resolve_attached, is_source_lookup=path.is_source_lookup, is_udp_encap=path.is_udp_encap, is_multipath=1 if len(self.paths) > 1 else 0) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): if self.is_local or self.is_unreach or self.is_prohibit: self._test.vapi.ip_add_del_route( self.dest_addr, self.dest_addr_len, inet_pton(AF_INET6, "::"), 0xffffffff, is_local=self.is_local, is_unreach=self.is_unreach, is_prohibit=self.is_prohibit, is_add=0, table_id=self.table_id, is_ipv6=self.is_ip6) else: for path in self.paths: self._test.vapi.ip_add_del_route( self.dest_addr, self.dest_addr_len, path.nh_addr, path.nh_itf, table_id=self.table_id, next_hop_table_id=path.nh_table_id, next_hop_via_label=path.nh_via_label, next_hop_id=path.next_hop_id, is_add=0, is_udp_encap=path.is_udp_encap, is_ipv6=self.is_ip6, is_dvr=path.is_dvr) def query_vpp_config(self): return find_route(self._test, self.dest_addr_p, self.dest_addr_len, self.table_id, inet=AF_INET6 if self.is_ip6 == 1 else AF_INET) def __str__(self): return self.object_id() def object_id(self): return ("%d:%s/%d" % (self.table_id, self.dest_addr_p, self.dest_addr_len)) class VppIpMRoute(VppObject): """ IP Multicast Route """ def __init__(self, test, src_addr, grp_addr, grp_addr_len, e_flags, paths, table_id=0, rpf_id=0, is_ip6=0): self._test = test self.paths = paths self.grp_addr_len = grp_addr_len self.table_id = table_id self.e_flags = e_flags self.is_ip6 = is_ip6 self.rpf_id = rpf_id self.grp_addr_p = grp_addr self.src_addr_p = src_addr if is_ip6: self.grp_addr = inet_pton(AF_INET6, grp_addr) self.src_addr = inet_pton(AF_INET6, src_addr) else: self.grp_addr = inet_pton(AF_INET, grp_addr) self.src_addr = inet_pton(AF_INET, src_addr) def add_vpp_config(self): for path in self.paths: self._test.vapi.ip_mroute_add_del(self.src_addr, self.grp_addr, self.grp_addr_len, self.e_flags, path.proto, path.nh_itf, path.nh_addr, path.nh_i_flags, bier_imp=path.bier_imp, rpf_id=self.rpf_id, table_id=self.table_id, is_ipv6=self.is_ip6) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): for path in self.paths: self._test.vapi.ip_mroute_add_del(self.src_addr, self.grp_addr, self.grp_addr_len, self.e_flags, path.proto, path.nh_itf, path.nh_addr, path.nh_i_flags, table_id=self.table_id, bier_imp=path.bier_imp, is_add=0, is_ipv6=self.is_ip6) def update_entry_flags(self, flags): self.e_flags = flags self._test.vapi.ip_mroute_add_del(self.src_addr, self.grp_addr, self.grp_addr_len, self.e_flags, 0, 0xffffffff, "", 0, table_id=self.table_id, is_ipv6=self.is_ip6) def update_rpf_id(self, rpf_id): self.rpf_id = rpf_id self._test.vapi.ip_mroute_add_del(self.src_addr, self.grp_addr, self.grp_addr_len, self.e_flags, 0, 0xffffffff, "", 0, rpf_id=self.rpf_id, table_id=self.table_id, is_ipv6=self.is_ip6) def update_path_flags(self, itf, flags): for path in self.paths: if path.nh_itf == itf: path.nh_i_flags = flags break self._test.vapi.ip_mroute_add_del(self.src_addr, self.grp_addr, self.grp_addr_len, self.e_flags, path.proto, path.nh_itf, path.nh_addr, path.nh_i_flags, table_id=self.table_id, is_ipv6=self.is_ip6) def query_vpp_config(self): return find_mroute(self._test, self.grp_addr_p, self.src_addr_p, self.grp_addr_len, self.table_id, inet=AF_INET6 if self.is_ip6 == 1 else AF_INET) def __str__(self): return self.object_id() def object_id(self): if self.is_ip6: return ("%d:(%s,%s/%d)" % (self.table_id, inet_ntop(AF_INET6, self.src_addr), inet_ntop(AF_INET6, self.grp_addr), self.grp_addr_len)) else: return ("%d:(%s,%s/%d)" % (self.table_id, inet_ntop(AF_INET, self.src_addr), inet_ntop(AF_INET, self.grp_addr), self.grp_addr_len)) class VppMFibSignal(object): def __init__(self, test, route, interface, packet): self.route = route self.interface = interface self.packet = packet self.test = test def compare(self, signal): self.test.assertEqual(self.interface, signal.sw_if_index) self.test.assertEqual(self.route.table_id, signal.table_id) self.test.assertEqual(self.route.grp_addr_len, signal.grp_address_len) for i in range(self.route.grp_addr_len / 8): self.test.assertEqual(self.route.grp_addr[i], signal.grp_address[i]) if (self.route.grp_addr_len > 32): for i in range(4): self.test.assertEqual(self.route.src_addr[i], signal.src_address[i]) class VppMplsIpBind(VppObject): """ MPLS to IP Binding """ def __init__(self, test, local_label, dest_addr, dest_addr_len, table_id=0, ip_table_id=0, is_ip6=0): self._test = test self.dest_addr_len = dest_addr_len self.dest_addr = dest_addr self.local_label = local_label self.table_id = table_id self.ip_table_id = ip_table_id self.is_ip6 = is_ip6 if is_ip6: self.dest_addrn = inet_pton(AF_INET6, dest_addr) else: self.dest_addrn = inet_pton(AF_INET, dest_addr) def add_vpp_config(self): self._test.vapi.mpls_ip_bind_unbind(self.local_label, self.dest_addrn, self.dest_addr_len, table_id=self.table_id, ip_table_id=self.ip_table_id, is_ip4=(self.is_ip6 == 0)) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): self._test.vapi.mpls_ip_bind_unbind(self.local_label, self.dest_addrn, self.dest_addr_len, table_id=self.table_id, ip_table_id=self.ip_table_id, is_bind=0, is_ip4=(self.is_ip6 == 0)) def query_vpp_config(self): dump = self._test.vapi.mpls_fib_dump() for e in dump: if self.local_label == e.label \ and self.table_id == e.table_id: return True return False def __str__(self): return self.object_id() def object_id(self): return ("%d:%s binds %d:%s/%d" % (self.table_id, self.local_label, self.ip_table_id, self.dest_addr, self.dest_addr_len)) class VppMplsTable(VppObject): def __init__(self, test, table_id): self._test = test self.table_id = table_id def add_vpp_config(self): self._test.vapi.mpls_table_add_del( self.table_id, is_add=1) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): self._test.vapi.mpls_table_add_del( self.table_id, is_add=0) def query_vpp_config(self): # find the default route dump = self._test.vapi.mpls_fib_dump() if len(dump): return True return False def __str__(self): return self.object_id() def object_id(self): return ("table-mpls-%d" % (self.table_id)) class VppMplsRoute(VppObject): """ MPLS Route/LSP """ def __init__(self, test, local_label, eos_bit, paths, table_id=0, is_multicast=0): self._test = test self.paths = paths self.local_label = local_label self.eos_bit = eos_bit self.table_id = table_id self.is_multicast = is_multicast def add_vpp_config(self): is_multipath = len(self.paths) > 1 for path in self.paths: lstack = path.encode_labels() self._test.vapi.mpls_route_add_del( self.local_label, self.eos_bit, path.proto, path.nh_addr, path.nh_itf, is_multicast=self.is_multicast, is_multipath=is_multipath, table_id=self.table_id, is_interface_rx=path.is_interface_rx, is_rpf_id=path.is_rpf_id, next_hop_out_label_stack=lstack, next_hop_n_out_labels=len(lstack), next_hop_via_label=path.nh_via_label, next_hop_table_id=path.nh_table_id) self._test.registry.register(self, self._test.logger) def remove_vpp_config(self): for path in self.paths: self._test.vapi.mpls_route_add_del(self.local_label, self.eos_bit, path.proto, path.nh_addr, path.nh_itf, is_rpf_id=path.is_rpf_id, table_id=self.table_id, is_add=0) def query_vpp_config(self): dump = self._test.vapi.mpls_fib_dump() for e in dump: if self.local_label == e.label \ and self.eos_bit == e.eos_bit \ and self.table_id == e.table_id: return True return False def __str__(self): return self.object_id() def object_id(self): return ("%d:%s/%d" % (self.table_id, self.local_label, 20+self.eos_bit))