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