FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
nsh.c
Go to the documentation of this file.
1 /*
2  * nsh.c - nsh mapping
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <nsh/nsh.h>
21 #include <vnet/gre/gre.h>
22 #include <vnet/vxlan/vxlan.h>
24 #include <vnet/l2/l2_classify.h>
25 #include <vnet/adj/adj.h>
26 #include <vpp/app/version.h>
27 
29 
30 /* Uses network order's class and type to register */
31 int
33  u8 type,
34  u8 option_size,
35  int add_options (u8 * opt,
36  u8 * opt_size),
37  int options (vlib_buffer_t * b,
38  nsh_tlv_header_t * opt),
39  int swap_options (vlib_buffer_t * b,
40  nsh_tlv_header_t * old_opt,
41  nsh_tlv_header_t * new_opt),
42  int pop_options (vlib_buffer_t * b,
43  nsh_tlv_header_t * opt),
44  u8 * trace (u8 * s, nsh_tlv_header_t * opt))
45 {
47  nsh_option_map_by_key_t key, *key_copy;
48  uword *p;
49  nsh_option_map_t *nsh_option;
50 
51  key.class = class;
52  key.type = type;
53  key.pad = 0;
54 
55  p = hash_get_mem (nm->nsh_option_map_by_key, &key);
56  /* Already registered */
57  if (p != 0)
58  {
59  return (-1);
60  }
61 
62  pool_get_aligned (nm->nsh_option_mappings, nsh_option,
64  clib_memset (nsh_option, 0, sizeof (*nsh_option));
65  nsh_option->option_id = nsh_option - nm->nsh_option_mappings;
66 
67  key_copy = clib_mem_alloc (sizeof (*key_copy));
68  clib_memcpy (key_copy, &key, sizeof (*key_copy));
69  hash_set_mem (nm->nsh_option_map_by_key, key_copy,
70  nsh_option - nm->nsh_option_mappings);
71 
72  if (option_size > (MAX_NSH_OPTION_LEN + sizeof (nsh_tlv_header_t)))
73  {
74  return (-1);
75  }
76  nm->options_size[nsh_option->option_id] = option_size;
77  nm->add_options[nsh_option->option_id] = add_options;
78  nm->options[nsh_option->option_id] = options;
79  nm->swap_options[nsh_option->option_id] = swap_options;
80  nm->pop_options[nsh_option->option_id] = pop_options;
81  nm->trace[nsh_option->option_id] = trace;
82 
83  return (0);
84 }
85 
86 /* Uses network order's class and type to lookup */
89 {
92  uword *p;
93 
94  key.class = class;
95  key.type = type;
96  key.pad = 0;
97 
98  p = hash_get_mem (nm->nsh_option_map_by_key, &key);
99  /* not registered */
100  if (p == 0)
101  {
102  return NULL;
103  }
104 
105  return pool_elt_at_index (nm->nsh_option_mappings, p[0]);
106 
107 }
108 
109 /* Uses network order's class and type to unregister */
110 int
112  u8 type,
113  int options (vlib_buffer_t * b,
114  nsh_tlv_header_t * opt),
115  u8 * trace (u8 * s, nsh_tlv_header_t * opt))
116 {
117  nsh_main_t *nm = &nsh_main;
118  nsh_option_map_by_key_t key, *key_copy;
119  uword *p;
120  hash_pair_t *hp;
121  nsh_option_map_t *nsh_option;
122 
123  key.class = class;
124  key.type = type;
125  key.pad = 0;
126 
127  p = hash_get_mem (nm->nsh_option_map_by_key, &key);
128  /* not registered */
129  if (p == 0)
130  {
131  return (-1);
132  }
133 
134  nsh_option = pool_elt_at_index (nm->nsh_option_mappings, p[0]);
135  nm->options[nsh_option->option_id] = NULL;
136  nm->add_options[nsh_option->option_id] = NULL;
137  nm->pop_options[nsh_option->option_id] = NULL;
138  nm->trace[nsh_option->option_id] = NULL;
139 
140  hp = hash_get_pair (nm->nsh_option_map_by_key, &key);
141  key_copy = (void *) (hp->key);
142  hash_unset_mem (nm->nsh_option_map_by_key, &key_copy);
143  clib_mem_free (key_copy);
144 
145  pool_put (nm->nsh_option_mappings, nsh_option);
146 
147  return (0);
148 }
149 
150 /**
151  * @brief Formatting function for tracing VXLAN GPE with length
152  *
153  * @param *s
154  * @param *args
155  *
156  * @return *s
157  *
158  */
159 static u8 *
160 format_nsh_tunnel_with_length (u8 * s, va_list * args)
161 {
162  u32 dev_instance = va_arg (*args, u32);
163  s = format (s, "unimplemented dev %u", dev_instance);
164  return s;
165 }
166 
167 /* *INDENT-OFF* */
169  .name = "NSH",
170  .format_header = format_nsh_tunnel_with_length,
171  .build_rewrite = default_build_rewrite,
173 };
174 /* *INDENT-ON* */
175 
176 void
178 {
179  nsh_main_t *hm = &nsh_main;
181  return;
182 }
183 
184 clib_error_t *
186 {
187  vlib_node_t *node;
188  nsh_main_t *nm = &nsh_main;
189  clib_error_t *error = 0;
190  uword next_node;
191 
192  /* Init the main structures from VPP */
193  nm->vlib_main = vm;
194  nm->vnet_main = vnet_get_main ();
195 
196  /* Various state maintenance mappings */
197  nm->nsh_mapping_by_key = hash_create_mem (0, sizeof (u32), sizeof (uword));
198 
199  nm->nsh_mapping_by_mapped_key
200  = hash_create_mem (0, sizeof (u32), sizeof (uword));
201 
202  nm->nsh_entry_by_key = hash_create_mem (0, sizeof (u32), sizeof (uword));
203 
204  nm->nsh_proxy_session_by_key
205  =
206  hash_create_mem (0, sizeof (nsh_proxy_session_by_key_t), sizeof (uword));
207 
208  nm->nsh_option_map_by_key
209  = hash_create_mem (0, sizeof (nsh_option_map_by_key_t), sizeof (uword));
210 
211  error = nsh_api_init (vm, nm);
212  if (error)
213  return error;
214 
215  node = vlib_get_node_by_name (vm, (u8 *) "nsh-input");
216  nm->nsh_input_node_index = node->index;
217 
218  node = vlib_get_node_by_name (vm, (u8 *) "nsh-proxy");
219  nm->nsh_proxy_node_index = node->index;
220 
221  node = vlib_get_node_by_name (vm, (u8 *) "nsh-classifier");
222  nm->nsh_classifier_node_index = node->index;
223 
224  /* Add dispositions to nodes that feed nsh-input */
225  //alagalah - validate we don't really need to use the node value
226  next_node =
228  nm->nsh_input_node_index);
230  nm->nsh_proxy_node_index);
233  vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_NSH, next_node);
234 
236  nm->nsh_input_node_index);
238  nm->nsh_proxy_node_index);
241 
242  vlib_node_add_next (vm, gre4_input_node.index, nm->nsh_input_node_index);
243  vlib_node_add_next (vm, gre4_input_node.index, nm->nsh_proxy_node_index);
246 
247  vlib_node_add_next (vm, gre6_input_node.index, nm->nsh_input_node_index);
248  vlib_node_add_next (vm, gre6_input_node.index, nm->nsh_proxy_node_index);
251 
252  /* Add NSH-Proxy support */
253  vlib_node_add_next (vm, vxlan4_input_node.index, nm->nsh_proxy_node_index);
254  vlib_node_add_next (vm, vxlan6_input_node.index, nm->nsh_proxy_node_index);
255 
256  /* Add NSH-Classifier support */
258  nm->nsh_classifier_node_index);
260  nm->nsh_classifier_node_index);
262  nm->nsh_classifier_node_index);
263 
264  /* Add Ethernet+NSH support */
265  ethernet_register_input_type (vm, ETHERNET_TYPE_NSH,
266  nm->nsh_input_node_index);
267 
268  return error;
269 }
270 
272 
273 /* *INDENT-OFF* */
275  .version = VPP_BUILD_VER,
276  .description = "Network Service Header (NSH)",
277 };
278 /* *INDENT-ON* */
279 
280 /*
281  * fd.io coding-style-patch-verification: ON
282  *
283  * Local Variables:
284  * eval: (c-set-style "gnu")
285  * End:
286  */
l2_input_classify_node
vlib_node_registration_t l2_input_classify_node
(constructor) VLIB_REGISTER_NODE (l2_input_classify_node)
Definition: l2_input_classify.c:447
adj.h
trace
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:899
vxlan.h
nsh_api_init
clib_error_t * nsh_api_init(vlib_main_t *vm, nsh_main_t *nm)
Definition: nsh_api.c:698
vlib_node_add_next
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1177
ip6_classify_node
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:334
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
format_nsh_tunnel_with_length
static u8 * format_nsh_tunnel_with_length(u8 *s, va_list *args)
Formatting function for tracing VXLAN GPE with length.
Definition: nsh.c:160
nsh_hw_class
vnet_hw_interface_class_t nsh_hw_class
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
vxlan6_input_node
vlib_node_registration_t vxlan6_input_node
(constructor) VLIB_REGISTER_NODE (vxlan6_input_node)
Definition: decap.c:24
MAX_NSH_OPTION_LEN
#define MAX_NSH_OPTION_LEN
Definition: nsh.h:251
pool_get_aligned
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P with alignment A.
Definition: pool.h:249
next
u16 * next
Definition: nat44_ei_out2in.c:718
clib_mem_free
static void clib_mem_free(void *p)
Definition: mem.h:311
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
hash_get_pair
#define hash_get_pair(h, key)
Definition: hash.h:252
u16
unsigned short u16
Definition: types.h:57
pool_put
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:305
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
error
Definition: cJSON.c:88
nsh_md2_set_next_ioam_export_override
void nsh_md2_set_next_ioam_export_override(uword next)
Definition: nsh.c:177
nsh_main_t
Definition: nsh.h:105
key
typedef key
Definition: ipsec_types.api:88
vxlan_gpe.h
VXLAN GPE definitions.
VNET_HW_INTERFACE_CLASS
VNET_HW_INTERFACE_CLASS(nsh_hw_class)
ip4_classify_node
vlib_node_registration_t ip4_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_classify_node)
Definition: ip_classify.c:313
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
uword
u64 uword
Definition: types.h:112
hash_create_mem
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:660
hash_set_mem
#define hash_set_mem(h, key, value)
Definition: hash.h:275
nat44_ei_main_s::vnet_main
vnet_main_t * vnet_main
Definition: nat44_ei.h:479
vxlan4_input_node
vlib_node_registration_t vxlan4_input_node
(constructor) VLIB_REGISTER_NODE (vxlan4_input_node)
Definition: decap.c:23
nsh_init
clib_error_t * nsh_init(vlib_main_t *vm)
Definition: nsh.c:185
nsh_md2_lookup_option
nsh_option_map_t * nsh_md2_lookup_option(u16 class, u8 type)
Definition: nsh.c:88
nsh_proxy_session_by_key_t
Definition: nsh.h:93
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
plugin.h
nsh_option_map_t::option_id
u32 option_id
Definition: nsh.h:29
vlib_get_node_by_name
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
nsh_md2_register_option
int nsh_md2_register_option(u16 class, u8 type, u8 option_size, int add_options(u8 *opt, u8 *opt_size), int options(vlib_buffer_t *b, nsh_tlv_header_t *opt), int swap_options(vlib_buffer_t *b, nsh_tlv_header_t *old_opt, nsh_tlv_header_t *new_opt), int pop_options(vlib_buffer_t *b, nsh_tlv_header_t *opt), u8 *trace(u8 *s, nsh_tlv_header_t *opt))
Definition: nsh.c:32
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
nsh_option_map_by_key_t
Definition: nsh.h:22
nsh_main
nsh_main_t nsh_main
Definition: nsh.c:28
format
description fragment has unexpected format
Definition: map.api:433
nsh.h
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
nsh_md2_unregister_option
int nsh_md2_unregister_option(u16 class, u8 type, int options(vlib_buffer_t *b, nsh_tlv_header_t *opt), u8 *trace(u8 *s, nsh_tlv_header_t *opt))
Definition: nsh.c:111
options
static struct option options[]
Definition: main.c:52
nsh_main_t::decap_v4_next_override
uword decap_v4_next_override
Definition: nsh.h:156
hash_pair_t::key
uword key
Definition: hash.h:162
l2_classify.h
gre6_input_node
vlib_node_registration_t gre6_input_node
(constructor) VLIB_REGISTER_NODE (gre6_input_node)
Definition: node.c:474
nm
nat44_ei_main_t * nm
Definition: nat44_ei_hairpinning.c:413
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
hash_unset_mem
#define hash_unset_mem(h, key)
Definition: hash.h:291
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
vxlan_gpe_register_decap_protocol
void vxlan_gpe_register_decap_protocol(u8 protocol_id, uword next_node_index)
Definition: decap.c:687
default_build_rewrite
u8 * default_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Return a complete, zero-length (aka placeholder) rewrite.
Definition: interface.c:1705
VNET_HW_INTERFACE_CLASS_FLAG_P2P
@ VNET_HW_INTERFACE_CLASS_FLAG_P2P
a point 2 point interface
Definition: interface.h:394
gre4_input_node
vlib_node_registration_t gre4_input_node
(constructor) VLIB_REGISTER_NODE (gre4_input_node)
Definition: node.c:454
nsh_option_map_t
Definition: nsh.h:28
nsh_aware_vnf_proxy_node
vlib_node_registration_t nsh_aware_vnf_proxy_node
(constructor) VLIB_REGISTER_NODE (nsh_aware_vnf_proxy_node)
Definition: nsh_node.c:965
vnet.h
vxlan4_gpe_input_node
vlib_node_registration_t vxlan4_gpe_input_node
(constructor) VLIB_REGISTER_NODE (vxlan4_gpe_input_node)
Definition: decap.c:732
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ethernet_register_input_type
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:2263
vxlan6_gpe_input_node
vlib_node_registration_t vxlan6_gpe_input_node
(constructor) VLIB_REGISTER_NODE (vxlan6_gpe_input_node)
Definition: decap.c:754
gre.h
hash_pair_t
Definition: hash.h:159
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111