nsim: add packet loss simulation, docs
[vpp.git] / src / plugins / nsh / nsh.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef included_nsh_h
16 #define included_nsh_h
17
18 #include <vnet/vnet.h>
19 #include <nsh/nsh_packet.h>
20 #include <vnet/ip/ip4_packet.h>
21
22 typedef struct {
23   u16 class;
24   u8 type;
25   u8 pad;
26 } nsh_option_map_by_key_t;
27
28 typedef struct {
29   u32 option_id;
30 } nsh_option_map_t;
31
32 #define MAX_METADATA_LEN 62
33 /** Note:
34  * rewrite and rewrite_size used to support varied nsh header
35  */
36 typedef struct {
37   /* Required for pool_get_aligned  */
38   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
39
40   nsh_base_header_t nsh_base;
41   union {
42      nsh_md1_data_t md1_data;
43      nsh_md2_data_t md2_data;
44    } md;
45   u8 tlvs_len;    /* configured md2 metadata's length, unit: byte */
46   u8 * tlvs_data; /* configured md2 metadata, network order */
47
48   /** Rewrite string. network order
49    * contains base header and metadata */
50   u8 * rewrite;
51   u8  rewrite_size; /* unit: byte */
52 } nsh_entry_t;
53
54 typedef struct {
55   u8 is_add;
56   nsh_entry_t nsh_entry;
57 } nsh_add_del_entry_args_t;
58
59 typedef struct {
60   /* Required for pool_get_aligned  */
61   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
62
63   /** Key for nsh_header_t entry: 24bit NSP 8bit NSI */
64   u32 nsp_nsi;
65   /** Key for nsh_header_t entry to map to. : 24bit NSP 8bit NSI
66    *  This may be ~0 if next action is to decap to NSH next protocol
67    *  Note the following heuristic:
68    *  if nsp_nsi == mapped_nsp_nsi then use-case is like SFC SFF
69    *  if nsp_nsi != mapped_nsp_nsi then use-case is like SFC SF
70    *  Note: these are heuristics. Rules about NSI decrement are out of scope
71    */
72   u32 mapped_nsp_nsi;
73   /* NSH Header action: swap, push and pop */
74   u32 nsh_action;
75
76   /** vnet intfc hw_if_index */
77   u32 nsh_hw_if;
78   /* vnet intfc sw_if_index */
79   u32 nsh_sw_if;
80
81   /* encap if index */
82   u32 sw_if_index;
83   u32 rx_sw_if_index;
84   u32 next_node;
85   u32 adj_index;
86 } nsh_map_t;
87
88 typedef struct {
89   u8 is_add;
90   nsh_map_t map;
91 } nsh_add_del_map_args_t;
92
93 typedef struct {
94   u32 transport_type; /* 1:vxlan; */
95   u32 transport_index; /* transport's sw_if_index */
96 } nsh_proxy_session_by_key_t;
97
98 typedef struct {
99   /* 24bit NSP 8bit NSI */
100   u32 nsp_nsi;
101 } nsh_proxy_session_t;
102
103 #define MAX_MD2_OPTIONS 256
104
105 typedef struct {
106   /* API message ID base */
107   u16 msg_id_base;
108
109   /* vector of nsh_header entry instances */
110   nsh_entry_t *nsh_entries;
111
112   /* hash lookup nsh header by key: {u32: nsp_nsi} */
113   uword * nsh_entry_by_key;
114
115   /* vector of nsh_mappings */
116   nsh_map_t *nsh_mappings;
117
118   /* hash lookup nsh mapping by key: {u32: nsp_nsi} */
119   uword * nsh_mapping_by_key;
120   uword * nsh_mapping_by_mapped_key; // for use in NSHSFC
121
122   /* vector of nsh_proxy */
123   nsh_proxy_session_t *nsh_proxy_sessions;
124
125   /* hash lookup nsh_proxy by key */
126   uword * nsh_proxy_session_by_key;
127
128   /** Free vlib hw_if_indices */
129   u32 * free_nsh_tunnel_hw_if_indices;
130   /** Mapping from sw_if_index to tunnel index */
131   u32 * tunnel_index_by_sw_if_index;
132
133   /* vector of nsh_option_map */
134   nsh_option_map_t * nsh_option_mappings;
135   /* hash lookup nsh_option_map by key */
136   uword * nsh_option_map_by_key;
137
138   /* Array of function pointers to process MD-Type 2 handling routines */
139   /*
140    * For API or CLI configuration and construct the rewrite buffer, invokes add_options() function.
141    * In the encap node, i.e. when performing PUSH nsh header, invokes options() function.
142    * In the swap node, i.e. when performing SWAP nsh header, invokes swap_options() function.
143    * In the decap node, i.e. when performing POP nsh header, invokes pop_options() function.
144    */
145   u8 options_size[MAX_MD2_OPTIONS];  /* sum of header and metadata */
146   int (*add_options[MAX_MD2_OPTIONS]) (u8 * opt,
147                                            u8 * opt_size);
148   int (*options[MAX_MD2_OPTIONS]) (vlib_buffer_t * b,
149                                    nsh_tlv_header_t * opt);
150   int (*swap_options[MAX_MD2_OPTIONS]) (vlib_buffer_t * b,
151                                         nsh_tlv_header_t * old_opt,
152                                         nsh_tlv_header_t * new_opt);
153   int (*pop_options[MAX_MD2_OPTIONS]) (vlib_buffer_t * b,
154                                        nsh_tlv_header_t * opt);
155   u8 *(*trace[MAX_MD2_OPTIONS]) (u8 * s, nsh_tlv_header_t * opt);
156   uword decap_v4_next_override;
157
158   /* Feature arc indices */
159   u8 input_feature_arc_index;
160   u8 output_feature_arc_index;
161
162   /* convenience */
163   vlib_main_t * vlib_main;
164   vnet_main_t * vnet_main;
165 } nsh_main_t;
166
167 nsh_main_t nsh_main;
168
169 extern vlib_node_registration_t nsh_aware_vnf_proxy_node;
170 extern vlib_node_registration_t nsh_eth_output_node;
171
172 typedef struct {
173    u8 trace_data[256];
174 } nsh_input_trace_t;
175
176 u8 * format_nsh_input_map_trace (u8 * s, va_list * args);
177 u8 * format_nsh_header_with_length (u8 * s, va_list * args);
178
179 /* Helper macros used in nsh.c and nsh_test.c */
180 #define foreach_copy_nsh_base_hdr_field         \
181 _(ver_o_c)                                      \
182 _(length)                                       \
183 _(md_type)                                      \
184 _(next_protocol)                                \
185 _(nsp_nsi)
186
187 /* Statistics (not really errors) */
188 #define foreach_nsh_node_error    \
189 _(MAPPED, "NSH header found and mapped") \
190 _(NO_MAPPING, "no mapping for nsh key") \
191 _(NO_ENTRY, "no entry for nsh key") \
192 _(NO_PROXY, "no proxy for transport key") \
193 _(INVALID_NEXT_PROTOCOL, "invalid next protocol") \
194 _(INVALID_OPTIONS, "invalid md2 options") \
195 _(INVALID_TTL, "ttl equals zero") \
196
197 typedef enum {
198 #define _(sym,str) NSH_NODE_ERROR_##sym,
199   foreach_nsh_node_error
200 #undef _
201   NSH_NODE_N_ERROR,
202
203 } nsh_input_error_t;
204
205 #define foreach_nsh_node_next        \
206   _(DROP, "error-drop")                 \
207   _(ENCAP_GRE4, "gre4-input" )          \
208   _(ENCAP_GRE6, "gre6-input" )          \
209   _(ENCAP_VXLANGPE, "vxlan-gpe-encap" ) \
210   _(ENCAP_VXLAN4, "vxlan4-encap" )  \
211   _(ENCAP_VXLAN6, "vxlan6-encap" )  \
212   _(DECAP_ETH_INPUT, "ethernet-input" ) \
213   _(ENCAP_LISP_GPE, "interface-output" )  \
214   _(ENCAP_ETHERNET, "nsh-eth-output")   \
215 /*   _(DECAP_IP4_INPUT,  "ip4-input") \ */
216 /*   _(DECAP_IP6_INPUT,  "ip6-input" ) \  */
217
218 typedef enum {
219 #define _(s,n) NSH_NODE_NEXT_##s,
220   foreach_nsh_node_next
221 #undef _
222   NSH_NODE_N_NEXT,
223 } nsh_node_next_t;
224
225 typedef enum {
226   NSH_ACTION_SWAP,
227   NSH_ACTION_PUSH,
228   NSH_ACTION_POP,
229 } nsh_action_type;
230
231 typedef enum {
232   NSH_INPUT_TYPE,
233   NSH_PROXY_TYPE,
234   NSH_CLASSIFIER_TYPE,
235   NSH_AWARE_VNF_PROXY_TYPE,
236 } nsh_entity_type;
237
238 #define VNET_SW_INTERFACE_FLAG_ADMIN_DOWN 0
239
240 /* md2 class and type definition */
241 #define NSH_MD2_IOAM_CLASS 0x9
242 #define NSH_MD2_IOAM_OPTION_TYPE_TRACE   0x3B
243 #define NSH_MD2_IOAM_OPTION_TYPE_PROOF_OF_TRANSIT 0x3C
244
245 #define NSH_MD2_IOAM_TRACE_DUMMY_LEN 0x8
246
247 #define MAX_NSH_HEADER_LEN  256
248 #define MAX_NSH_OPTION_LEN  128
249
250 int
251 nsh_md2_register_option (u16 class,
252                       u8 type,
253                       u8 option_size,
254                       int add_options (u8 * opt,
255                                        u8 * opt_size),
256                       int options(vlib_buffer_t * b,
257                                   nsh_tlv_header_t * opt),
258                       int swap_options (vlib_buffer_t * b,
259                                         nsh_tlv_header_t * old_opt,
260                                         nsh_tlv_header_t * new_opt),
261                       int pop_options (vlib_buffer_t * b,
262                                        nsh_tlv_header_t * opt),
263                       u8 * trace (u8 * s,
264                                   nsh_tlv_header_t * opt));
265
266 typedef struct _nsh_main_dummy
267 {
268   u8 output_feature_arc_index;
269 } nsh_main_dummy_t;
270
271 #endif /* included_nsh_h */