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