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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/nsh/nsh.h>
24 nsh_header_t nsh_header;
27 u8 * format_nsh_header (u8 * s, va_list * args)
29 nsh_header_t * nsh = va_arg (*args, nsh_header_t *);
31 s = format (s, "nsh ver %d ", (nsh->ver_o_c>>6));
32 if (nsh->ver_o_c & NSH_O_BIT)
33 s = format (s, "O-set ");
35 if (nsh->ver_o_c & NSH_C_BIT)
36 s = format (s, "C-set ");
38 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
39 nsh->length, nsh->length * 4, nsh->md_type, nsh->next_protocol);
41 s = format (s, " service path %d service index %d\n",
42 (nsh->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
43 nsh->nsp_nsi & NSH_NSI_MASK);
45 s = format (s, " c1 %d c2 %d c3 %d c4 %d\n",
46 nsh->c1, nsh->c2, nsh->c3, nsh->c4);
51 u8 * format_nsh_map (u8 * s, va_list * args)
53 nsh_map_t * map = va_arg (*args, nsh_map_t *);
55 s = format (s, "nsh entry nsp: %d nsi: %d ",
56 (map->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
57 map->nsp_nsi & NSH_NSI_MASK);
58 s = format (s, "maps to nsp: %d nsi: %d ",
59 (map->mapped_nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
60 map->mapped_nsp_nsi & NSH_NSI_MASK);
62 switch (map->next_node)
64 case NSH_INPUT_NEXT_ENCAP_GRE:
66 s = format (s, "encapped by GRE intf: %d", map->sw_if_index);
69 case NSH_INPUT_NEXT_ENCAP_VXLANGPE:
71 s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index);
75 s = format (s, "only GRE and VXLANGPE support in this rev");
82 #define foreach_copy_nshhdr_field \
92 /* Temp killing tlvs as its causing pain - fix in NSH_SFC */
95 #define foreach_32bit_field \
103 u8 * format_nsh_header_with_length (u8 * s, va_list * args)
105 nsh_header_t * h = va_arg (*args, nsh_header_t *);
106 u32 max_header_bytes = va_arg (*args, u32);
107 u32 tmp, header_bytes;
109 header_bytes = sizeof (h[0]);
110 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
111 return format (s, "nsh header truncated");
113 tmp = clib_net_to_host_u32 (h->nsp_nsi);
114 s = format (s, " nsp %d nsi %d ",
115 (tmp>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
118 s = format (s, "c1 %u c2 %u c3 %u c4 %u",
119 clib_net_to_host_u32 (h->c1),
120 clib_net_to_host_u32 (h->c2),
121 clib_net_to_host_u32 (h->c3),
122 clib_net_to_host_u32 (h->c4));
124 s = format (s, "ver %d ", h->ver_o_c>>6);
126 if (h->ver_o_c & NSH_O_BIT)
127 s = format (s, "O-set ");
129 if (h->ver_o_c & NSH_C_BIT)
130 s = format (s, "C-set ");
132 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
133 h->length, h->length * 4, h->md_type, h->next_protocol);
137 u8 * format_nsh_input_map_trace (u8 * s, va_list * args)
139 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
140 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
141 nsh_input_trace_t * t
142 = va_arg (*args, nsh_input_trace_t *);
144 s = format (s, "\n %U", format_nsh_header, &t->nsh_header,
145 (u32) sizeof (t->nsh_header) );
151 nsh_input_map (vlib_main_t * vm,
152 vlib_node_runtime_t * node,
153 vlib_frame_t * from_frame)
155 u32 n_left_from, next_index, * from, * to_next;
156 nsh_main_t * nm = &nsh_main;
158 from = vlib_frame_vector_args (from_frame);
159 n_left_from = from_frame->n_vectors;
161 next_index = node->cached_next_index;
163 while (n_left_from > 0)
167 vlib_get_next_frame (vm, node, next_index,
168 to_next, n_left_to_next);
170 while (n_left_from >= 4 && n_left_to_next >= 2)
173 vlib_buffer_t * b0, * b1;
174 u32 next0 = NSH_INPUT_NEXT_DROP, next1 = NSH_INPUT_NEXT_DROP;
175 uword * entry0, * entry1;
176 nsh_header_t * hdr0 = 0, * hdr1 = 0;
177 u32 nsp_nsi0, nsp_nsi1;
179 nsh_map_t * map0 = 0, * map1 = 0;
181 /* Prefetch next iteration. */
183 vlib_buffer_t * p2, * p3;
185 p2 = vlib_get_buffer (vm, from[2]);
186 p3 = vlib_get_buffer (vm, from[3]);
188 vlib_prefetch_buffer_header (p2, LOAD);
189 vlib_prefetch_buffer_header (p3, LOAD);
191 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
192 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
207 b0 = vlib_get_buffer (vm, bi0);
208 hdr0 = vlib_buffer_get_current (b0);
209 nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
210 entry0 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi0);
212 b1 = vlib_get_buffer (vm, bi1);
213 hdr1 = vlib_buffer_get_current (b1);
214 nsp_nsi1 = clib_net_to_host_u32(hdr1->nsp_nsi);
215 entry1 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi1);
217 if (PREDICT_FALSE(entry0 == 0))
219 error0 = NSH_INPUT_ERROR_NO_MAPPING;
223 if (PREDICT_FALSE(entry1 == 0))
225 error1 = NSH_INPUT_ERROR_NO_MAPPING;
229 /* Entry should point to a mapping ...*/
230 map0 = pool_elt_at_index (nm->nsh_mappings, entry0[0]);
231 map1 = pool_elt_at_index (nm->nsh_mappings, entry1[0]);
233 if (PREDICT_FALSE(map0 == 0))
235 error0 = NSH_INPUT_ERROR_NO_MAPPING;
239 if (PREDICT_FALSE(map1 == 0))
241 error1 = NSH_INPUT_ERROR_NO_MAPPING;
245 entry0 = hash_get_mem (nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
246 entry1 = hash_get_mem (nm->nsh_entry_by_key, &map1->mapped_nsp_nsi);
248 if (PREDICT_FALSE(entry0 == 0))
250 error0 = NSH_INPUT_ERROR_NO_MAPPING;
253 if (PREDICT_FALSE(entry1 == 0))
255 error1 = NSH_INPUT_ERROR_NO_MAPPING;
259 hdr0 = pool_elt_at_index (nm->nsh_entries, entry0[0]);
260 hdr1 = pool_elt_at_index (nm->nsh_entries, entry1[0]);
262 /* set up things for next node to transmit ie which node to handle it and where */
263 next0 = map0->next_node;
264 next1 = map1->next_node;
265 vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
266 vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index;
269 b0->error = error0 ? node->errors[error0] : 0;
271 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
273 nsh_input_trace_t *tr =
274 vlib_add_trace (vm, node, b0, sizeof (*tr));
275 tr->nsh_header = *hdr0;
279 b1->error = error1 ? node->errors[error1] : 0;
281 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
283 nsh_input_trace_t *tr =
284 vlib_add_trace (vm, node, b1, sizeof (*tr));
285 tr->nsh_header = *hdr1;
289 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
290 to_next, n_left_to_next,
291 bi0, bi1, next0, next1);
295 while (n_left_from > 0 && n_left_to_next > 0)
299 u32 next0 = NSH_INPUT_NEXT_DROP;
301 nsh_header_t * hdr0 = 0;
304 nsh_map_t * map0 = 0;
315 b0 = vlib_get_buffer (vm, bi0);
316 hdr0 = vlib_buffer_get_current (b0);
317 nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
318 entry0 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi0);
320 if (PREDICT_FALSE(entry0 == 0))
322 error0 = NSH_INPUT_ERROR_NO_MAPPING;
326 /* Entry should point to a mapping ...*/
327 map0 = pool_elt_at_index (nm->nsh_mappings, entry0[0]);
329 if (PREDICT_FALSE(map0 == 0))
331 error0 = NSH_INPUT_ERROR_NO_MAPPING;
335 entry0 = hash_get_mem (nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
337 if (PREDICT_FALSE(entry0 == 0))
339 error0 = NSH_INPUT_ERROR_NO_MAPPING;
343 hdr0 = pool_elt_at_index (nm->nsh_entries, entry0[0]);
345 /* set up things for next node to transmit ie which node to handle it and where */
346 next0 = map0->next_node;
347 vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
350 b0->error = error0 ? node->errors[error0] : 0;
352 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
354 nsh_input_trace_t *tr =
355 vlib_add_trace (vm, node, b0, sizeof (*tr));
356 tr->nsh_header = *hdr0;
360 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
361 to_next, n_left_to_next,
365 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
370 return from_frame->n_vectors;
374 int vnet_nsh_add_del_map (vnet_nsh_add_del_map_args_t *a)
376 nsh_main_t * nm = &nsh_main;
382 key = a->map.nsp_nsi;
384 entry = hash_get_mem (nm->nsh_mapping_by_key, &key);
388 /* adding an entry, must not already exist */
390 return VNET_API_ERROR_INVALID_VALUE;
392 pool_get_aligned (nm->nsh_mappings, map, CLIB_CACHE_LINE_BYTES);
393 memset (map, 0, sizeof (*map));
395 /* copy from arg structure */
396 map->nsp_nsi = a->map.nsp_nsi;
397 map->mapped_nsp_nsi = a->map.mapped_nsp_nsi;
398 map->sw_if_index = a->map.sw_if_index;
399 map->next_node = a->map.next_node;
402 key_copy = clib_mem_alloc (sizeof (*key_copy));
403 clib_memcpy (key_copy, &key, sizeof (*key_copy));
405 hash_set_mem (nm->nsh_mapping_by_key, key_copy,
406 map - nm->nsh_mappings);
411 return VNET_API_ERROR_NO_SUCH_ENTRY;
413 map = pool_elt_at_index (nm->nsh_mappings, entry[0]);
414 hp = hash_get_pair (nm->nsh_mapping_by_key, &key);
415 key_copy = (void *)(hp->key);
416 hash_unset_mem (nm->nsh_mapping_by_key, &key);
417 clib_mem_free (key_copy);
419 pool_put (nm->nsh_mappings, map);
425 static clib_error_t *
426 nsh_add_del_map_command_fn (vlib_main_t * vm,
427 unformat_input_t * input,
428 vlib_cli_command_t * cmd)
430 unformat_input_t _line_input, * line_input = &_line_input;
432 u32 nsp, nsi, mapped_nsp, mapped_nsi;
433 int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
435 u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
436 vnet_nsh_add_del_map_args_t _a, * a = &_a;
439 /* Get a line of input. */
440 if (! unformat_user (input, unformat_line_input, line_input))
443 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
444 if (unformat (line_input, "del"))
446 else if (unformat (line_input, "nsp %d", &nsp))
448 else if (unformat (line_input, "nsi %d", &nsi))
450 else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
452 else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
454 else if (unformat (line_input, "encap-gre-intf %d", &sw_if_index))
455 next_node = NSH_INPUT_NEXT_ENCAP_GRE;
456 else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
457 next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE;
458 else if (unformat (line_input, "encap-none"))
459 next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
461 return clib_error_return (0, "parse error: '%U'",
462 format_unformat_error, line_input);
465 unformat_free (line_input);
467 if (nsp_set == 0 || nsi_set == 0)
468 return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
470 if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
471 return clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
474 return clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
476 memset (a, 0, sizeof (*a));
478 /* set args structure */
480 a->map.nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
481 a->map.mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
482 a->map.sw_if_index = sw_if_index;
483 a->map.next_node = next_node;
486 rv = vnet_nsh_add_del_map (a);
492 case VNET_API_ERROR_INVALID_VALUE:
493 return clib_error_return (0, "mapping already exists. Remove it first.");
495 case VNET_API_ERROR_NO_SUCH_ENTRY:
496 return clib_error_return (0, "mapping does not exist.");
499 return clib_error_return
500 (0, "vnet_nsh_add_del_map returned %d", rv);
506 VLIB_CLI_COMMAND (create_nsh_map_command, static) = {
507 .path = "create nsh map",
509 "create nsh map nsp <nn> nsi <nn> [del] map-nsp <nn> map-nsi <nn> [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]\n",
510 .function = nsh_add_del_map_command_fn,
513 static clib_error_t *
514 show_nsh_map_command_fn (vlib_main_t * vm,
515 unformat_input_t * input,
516 vlib_cli_command_t * cmd)
518 nsh_main_t * nm = &nsh_main;
521 if (pool_elts (nm->nsh_mappings) == 0)
522 vlib_cli_output (vm, "No nsh maps configured.");
524 pool_foreach (map, nm->nsh_mappings,
526 vlib_cli_output (vm, "%U", format_nsh_map, map);
532 VLIB_CLI_COMMAND (show_nsh_map_command, static) = {
533 .path = "show nsh map",
534 .function = show_nsh_map_command_fn,
538 int vnet_nsh_add_del_entry (vnet_nsh_add_del_entry_args_t *a)
540 nsh_main_t * nm = &nsh_main;
541 nsh_header_t *hdr = 0;
546 key = a->nsh.nsp_nsi;
548 entry = hash_get_mem (nm->nsh_entry_by_key, &key);
552 /* adding an entry, must not already exist */
554 return VNET_API_ERROR_INVALID_VALUE;
556 pool_get_aligned (nm->nsh_entries, hdr, CLIB_CACHE_LINE_BYTES);
557 memset (hdr, 0, sizeof (*hdr));
559 /* copy from arg structure */
560 #define _(x) hdr->x = a->nsh.x;
561 foreach_copy_nshhdr_field;
564 key_copy = clib_mem_alloc (sizeof (*key_copy));
565 clib_memcpy (key_copy, &key, sizeof (*key_copy));
567 hash_set_mem (nm->nsh_entry_by_key, key_copy,
568 hdr - nm->nsh_entries);
573 return VNET_API_ERROR_NO_SUCH_ENTRY;
575 hdr = pool_elt_at_index (nm->nsh_entries, entry[0]);
576 hp = hash_get_pair (nm->nsh_entry_by_key, &key);
577 key_copy = (void *)(hp->key);
578 hash_unset_mem (nm->nsh_entry_by_key, &key);
579 clib_mem_free (key_copy);
581 pool_put (nm->nsh_entries, hdr);
588 static clib_error_t *
589 nsh_add_del_entry_command_fn (vlib_main_t * vm,
590 unformat_input_t * input,
591 vlib_cli_command_t * cmd)
593 unformat_input_t _line_input, * line_input = &_line_input;
598 u8 next_protocol = 1; /* default: ip4 */
611 vnet_nsh_add_del_entry_args_t _a, * a = &_a;
613 /* Get a line of input. */
614 if (! unformat_user (input, unformat_line_input, line_input))
617 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
618 if (unformat (line_input, "del"))
620 else if (unformat (line_input, "version %d", &tmp))
621 ver_o_c |= (tmp & 3) << 6;
622 else if (unformat (line_input, "o-bit %d", &tmp))
623 ver_o_c |= (tmp & 1) << 5;
624 else if (unformat (line_input, "c-bit %d", &tmp))
625 ver_o_c |= (tmp & 1) << 4;
626 else if (unformat (line_input, "md-type %d", &tmp))
628 else if (unformat(line_input, "next-ip4"))
630 else if (unformat(line_input, "next-ip6"))
632 else if (unformat(line_input, "next-ethernet"))
634 else if (unformat (line_input, "c1 %d", &c1))
636 else if (unformat (line_input, "c2 %d", &c2))
638 else if (unformat (line_input, "c3 %d", &c3))
640 else if (unformat (line_input, "c4 %d", &c4))
642 else if (unformat (line_input, "nsp %d", &nsp))
644 else if (unformat (line_input, "nsi %d", &nsi))
646 else if (unformat (line_input, "tlv %x"))
647 vec_add1 (tlvs, tmp);
649 return clib_error_return (0, "parse error: '%U'",
650 format_unformat_error, line_input);
653 unformat_free (line_input);
656 return clib_error_return (0, "nsp not specified");
659 return clib_error_return (0, "nsi not specified");
662 return clib_error_return (0, "md-type 1 only supported at this time");
667 nsp_nsi = (nsp<<8) | nsi;
669 memset (a, 0, sizeof (*a));
673 #define _(x) a->nsh.x = x;
674 foreach_copy_nshhdr_field;
677 a->nsh.tlvs[0] = 0 ; // TODO FIXME this shouldn't be set 0 - in NSH_SFC project
679 rv = vnet_nsh_add_del_entry (a);
686 return clib_error_return
687 (0, "vnet_nsh_add_del_entry returned %d", rv);
693 VLIB_CLI_COMMAND (create_nsh_entry_command, static) = {
694 .path = "create nsh entry",
696 "create nsh entry {nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn>"
697 " [md-type <nn>] [tlv <xx>] [del]\n",
698 .function = nsh_add_del_entry_command_fn,
701 static clib_error_t *
702 show_nsh_entry_command_fn (vlib_main_t * vm,
703 unformat_input_t * input,
704 vlib_cli_command_t * cmd)
706 nsh_main_t * nm = &nsh_main;
709 if (pool_elts (nm->nsh_entries) == 0)
710 vlib_cli_output (vm, "No nsh entries configured.");
712 pool_foreach (hdr, nm->nsh_entries,
714 vlib_cli_output (vm, "%U", format_nsh_header, hdr);
720 VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
721 .path = "show nsh entry",
722 .function = show_nsh_entry_command_fn,
725 static char * nsh_input_error_strings[] = {
726 #define _(sym,string) string,
727 foreach_nsh_input_error
731 VLIB_REGISTER_NODE (nsh_input_node) = {
732 .function = nsh_input_map,
734 .vector_size = sizeof (u32),
735 .format_trace = format_nsh_input_map_trace,
736 .format_buffer = format_nsh_header_with_length,
737 .type = VLIB_NODE_TYPE_INTERNAL,
739 .n_errors = ARRAY_LEN(nsh_input_error_strings),
740 .error_strings = nsh_input_error_strings,
742 .n_next_nodes = NSH_INPUT_N_NEXT,
745 #define _(s,n) [NSH_INPUT_NEXT_##s] = n,
746 foreach_nsh_input_next
751 clib_error_t *nsh_init (vlib_main_t *vm)
753 nsh_main_t *nm = &nsh_main;
755 nm->vnet_main = vnet_get_main();
758 nm->nsh_mapping_by_key
759 = hash_create_mem (0, sizeof(u32), sizeof (uword));
761 nm->nsh_mapping_by_mapped_key
762 = hash_create_mem (0, sizeof(u32), sizeof (uword));
765 = hash_create_mem (0, sizeof(u32), sizeof (uword));
770 VLIB_INIT_FUNCTION(nsh_init);