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