nsh.h revision fda07ec4
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  u32 adj_index;
80} nsh_map_t;
81
82typedef struct {
83  u8 is_add;
84  nsh_map_t map;
85} nsh_add_del_map_args_t;
86
87typedef struct {
88  u32 transport_type; /* 1:vxlan; */
89  u32 transport_index; /* transport's sw_if_index */
90} nsh_proxy_session_by_key_t;
91
92typedef struct {
93  /* 24bit NSP 8bit NSI */
94  u32 nsp_nsi;
95} nsh_proxy_session_t;
96
97#define MAX_MD2_OPTIONS 256
98
99typedef struct {
100  /* API message ID base */
101  u16 msg_id_base;
102
103  /* vector of nsh_header entry instances */
104  nsh_entry_t *nsh_entries;
105
106  /* hash lookup nsh header by key: {u32: nsp_nsi} */
107  uword * nsh_entry_by_key;
108
109  /* vector of nsh_mappings */
110  nsh_map_t *nsh_mappings;
111
112  /* hash lookup nsh mapping by key: {u32: nsp_nsi} */
113  uword * nsh_mapping_by_key;
114  uword * nsh_mapping_by_mapped_key; // for use in NSHSFC
115
116  /* vector of nsh_proxy */
117  nsh_proxy_session_t *nsh_proxy_sessions;
118
119  /* hash lookup nsh_proxy by key */
120  uword * nsh_proxy_session_by_key;
121
122  /** Free vlib hw_if_indices */
123  u32 * free_nsh_tunnel_hw_if_indices;
124  /** Mapping from sw_if_index to tunnel index */
125  u32 * tunnel_index_by_sw_if_index;
126
127  /* vector of nsh_option_map */
128  nsh_option_map_t * nsh_option_mappings;
129  /* hash lookup nsh_option_map by key */
130  uword * nsh_option_map_by_key;
131
132  /* Array of function pointers to process MD-Type 2 handling routines */
133  /*
134   * For API or CLI configuration and construct the rewrite buffer, invokes add_options() function.
135   * In the encap node, i.e. when performing PUSH nsh header, invokes options() function.
136   * In the swap node, i.e. when performing SWAP nsh header, invokes swap_options() function.
137   * In the decap node, i.e. when performing POP nsh header, invokes pop_options() function.
138   */
139  u8 options_size[MAX_MD2_OPTIONS];  /* sum of header and metadata */
140  int (*add_options[MAX_MD2_OPTIONS]) (u8 * opt,
141					   u8 * opt_size);
142  int (*options[MAX_MD2_OPTIONS]) (vlib_buffer_t * b,
143                                   nsh_tlv_header_t * opt);
144  int (*swap_options[MAX_MD2_OPTIONS]) (vlib_buffer_t * b,
145                                        nsh_tlv_header_t * old_opt,
146					nsh_tlv_header_t * new_opt);
147  int (*pop_options[MAX_MD2_OPTIONS]) (vlib_buffer_t * b,
148				       nsh_tlv_header_t * opt);
149  u8 *(*trace[MAX_MD2_OPTIONS]) (u8 * s, nsh_tlv_header_t * opt);
150  uword decap_v4_next_override;
151
152  /* Feature arc indices */
153  u8 input_feature_arc_index;
154  u8 output_feature_arc_index;
155
156  /* convenience */
157  vlib_main_t * vlib_main;
158  vnet_main_t * vnet_main;
159} nsh_main_t;
160
161nsh_main_t nsh_main;
162
163extern vlib_node_registration_t nsh_aware_vnf_proxy_node;
164extern vlib_node_registration_t nsh_eth_output_node;
165
166typedef struct {
167   u8 trace_data[256];
168} nsh_input_trace_t;
169
170u8 * format_nsh_input_map_trace (u8 * s, va_list * args);
171u8 * format_nsh_header_with_length (u8 * s, va_list * args);
172
173/* Helper macros used in nsh.c and nsh_test.c */
174#define foreach_copy_nsh_base_hdr_field         \
175_(ver_o_c)					\
176_(length)					\
177_(md_type)					\
178_(next_protocol)				\
179_(nsp_nsi)
180
181/* Statistics (not really errors) */
182#define foreach_nsh_node_error    \
183_(MAPPED, "NSH header found and mapped") \
184_(NO_MAPPING, "no mapping for nsh key") \
185_(NO_ENTRY, "no entry for nsh key") \
186_(NO_PROXY, "no proxy for transport key") \
187_(INVALID_NEXT_PROTOCOL, "invalid next protocol") \
188_(INVALID_OPTIONS, "invalid md2 options") \
189_(INVALID_TTL, "ttl equals zero") \
190
191typedef enum {
192#define _(sym,str) NSH_NODE_ERROR_##sym,
193  foreach_nsh_node_error
194#undef _
195  NSH_NODE_N_ERROR,
196
197} nsh_input_error_t;
198
199#define foreach_nsh_node_next        \
200  _(DROP, "error-drop")			\
201  _(ENCAP_GRE4, "gre4-input" )		\
202  _(ENCAP_GRE6, "gre6-input" )		\
203  _(ENCAP_VXLANGPE, "vxlan-gpe-encap" ) \
204  _(ENCAP_VXLAN4, "vxlan4-encap" )  \
205  _(ENCAP_VXLAN6, "vxlan6-encap" )  \
206  _(DECAP_ETH_INPUT, "ethernet-input" ) \
207  _(ENCAP_LISP_GPE, "interface-output" )  \
208  _(ENCAP_ETHERNET, "nsh-eth-output")   \
209/*   _(DECAP_IP4_INPUT,  "ip4-input") \ */
210/*   _(DECAP_IP6_INPUT,  "ip6-input" ) \  */
211
212typedef enum {
213#define _(s,n) NSH_NODE_NEXT_##s,
214  foreach_nsh_node_next
215#undef _
216  NSH_NODE_N_NEXT,
217} nsh_node_next_t;
218
219typedef enum {
220  NSH_ACTION_SWAP,
221  NSH_ACTION_PUSH,
222  NSH_ACTION_POP,
223} nsh_action_type;
224
225typedef enum {
226  NSH_INPUT_TYPE,
227  NSH_PROXY_TYPE,
228  NSH_CLASSIFIER_TYPE,
229  NSH_AWARE_VNF_PROXY_TYPE,
230} nsh_entity_type;
231
232#define VNET_SW_INTERFACE_FLAG_ADMIN_DOWN 0
233
234/* md2 class and type definition */
235#define NSH_MD2_IOAM_CLASS 0x9
236#define NSH_MD2_IOAM_OPTION_TYPE_TRACE   0x3B
237#define NSH_MD2_IOAM_OPTION_TYPE_PROOF_OF_TRANSIT 0x3C
238
239#define NSH_MD2_IOAM_TRACE_DUMMY_LEN 0x8
240
241#define MAX_NSH_HEADER_LEN  256
242#define MAX_NSH_OPTION_LEN  128
243
244int
245nsh_md2_register_option (u16 class,
246                      u8 type,
247                      u8 option_size,
248                      int add_options (u8 * opt,
249                                       u8 * opt_size),
250                      int options(vlib_buffer_t * b,
251                                  nsh_tlv_header_t * opt),
252                      int swap_options (vlib_buffer_t * b,
253				        nsh_tlv_header_t * old_opt,
254		                        nsh_tlv_header_t * new_opt),
255                      int pop_options (vlib_buffer_t * b,
256                                       nsh_tlv_header_t * opt),
257                      u8 * trace (u8 * s,
258                                  nsh_tlv_header_t * opt));
259
260typedef struct _nsh_main_dummy
261{
262  u8 output_feature_arc_index;
263} nsh_main_dummy_t;
264
265#endif /* included_nsh_h */
266