nsh.c revision b436fcce
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
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24#include <vlibsocket/api.h>
25
26/* define message IDs */
27#include <nsh/nsh_msg_enum.h>
28
29/* define message structures */
30#define vl_typedefs
31#include <nsh/nsh_all_api_h.h>
32#undef vl_typedefs
33
34/* define generated endian-swappers */
35#define vl_endianfun
36#include <nsh/nsh_all_api_h.h>
37#undef vl_endianfun
38
39/* instantiate all the print functions we know about */
40#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41#define vl_printfun
42#include <nsh/nsh_all_api_h.h>
43#undef vl_printfun
44
45/* Get the API version number */
46#define vl_api_version(n,v) static u32 api_version=(v);
47#include <nsh/nsh_all_api_h.h>
48#undef vl_api_version
49
50/*
51 * A handy macro to set up a message reply.
52 * Assumes that the following variables are available:
53 * mp - pointer to request message
54 * rmp - pointer to reply message type
55 * rv - return value
56 */
57
58#define REPLY_MACRO(t)                                          \
59  do {								\
60    unix_shared_memory_queue_t * q =                            \
61      vl_api_client_index_to_input_queue (mp->client_index);	\
62    if (!q)                                                     \
63      return;							\
64                                                                \
65    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
66    rmp->_vl_msg_id = ntohs((t)+nm->msg_id_base);               \
67    rmp->context = mp->context;                                 \
68    rmp->retval = ntohl(rv);                                    \
69                                                                \
70    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
71  } while(0);
72
73/* List of message types that this plugin understands */
74
75#define foreach_nsh_plugin_api_msg		\
76  _(NSH_ADD_DEL_ENTRY, nsh_add_del_entry)	\
77  _(NSH_ADD_DEL_MAP, nsh_add_del_map)
78
79clib_error_t *
80vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
81                      int from_early_init)
82{
83  nsh_main_t * nm = &nsh_main;
84  clib_error_t * error = 0;
85
86  nm->vlib_main = vm;
87  nm->vnet_main = h->vnet_main;
88
89  return error;
90}
91
92typedef struct {
93  nsh_header_t nsh_header;
94} nsh_input_trace_t;
95
96u8 * format_nsh_header (u8 * s, va_list * args)
97{
98  nsh_header_t * nsh = va_arg (*args, nsh_header_t *);
99
100  s = format (s, "nsh ver %d ", (nsh->ver_o_c>>6));
101  if (nsh->ver_o_c & NSH_O_BIT)
102    s = format (s, "O-set ");
103
104  if (nsh->ver_o_c & NSH_C_BIT)
105    s = format (s, "C-set ");
106
107  s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
108              nsh->length, nsh->length * 4, nsh->md_type, nsh->next_protocol);
109
110  s = format (s, "  service path %d service index %d\n",
111              (nsh->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
112              nsh->nsp_nsi & NSH_NSI_MASK);
113
114  s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
115              nsh->c1, nsh->c2, nsh->c3, nsh->c4);
116
117  return s;
118}
119
120u8 * format_nsh_map (u8 * s, va_list * args)
121{
122  nsh_map_t * map = va_arg (*args, nsh_map_t *);
123
124  s = format (s, "nsh entry nsp: %d nsi: %d ",
125              (map->nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
126              map->nsp_nsi & NSH_NSI_MASK);
127  s = format (s, "maps to nsp: %d nsi: %d ",
128              (map->mapped_nsp_nsi>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
129              map->mapped_nsp_nsi & NSH_NSI_MASK);
130
131  switch (map->next_node)
132    {
133    case NSH_INPUT_NEXT_ENCAP_GRE:
134      {
135	s = format (s, "encapped by GRE intf: %d", map->sw_if_index);
136	break;
137      }
138    case NSH_INPUT_NEXT_ENCAP_VXLANGPE:
139      {
140	s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index);
141	break;
142      }
143    default:
144      s = format (s, "only GRE and VXLANGPE support in this rev");
145    }
146
147  return s;
148}
149
150u8 * format_nsh_header_with_length (u8 * s, va_list * args)
151{
152  nsh_header_t * h = va_arg (*args, nsh_header_t *);
153  u32 max_header_bytes = va_arg (*args, u32);
154  u32 tmp, header_bytes;
155
156  header_bytes = sizeof (h[0]);
157  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
158    return format (s, "nsh header truncated");
159
160  tmp = clib_net_to_host_u32 (h->nsp_nsi);
161  s = format (s, "  nsp %d nsi %d ",
162              (tmp>>NSH_NSP_SHIFT) & NSH_NSP_MASK,
163              tmp & NSH_NSI_MASK);
164
165  s = format (s, "c1 %u c2 %u c3 %u c4 %u",
166              clib_net_to_host_u32 (h->c1),
167              clib_net_to_host_u32 (h->c2),
168              clib_net_to_host_u32 (h->c3),
169              clib_net_to_host_u32 (h->c4));
170
171  s = format (s, "ver %d ", h->ver_o_c>>6);
172
173  if (h->ver_o_c & NSH_O_BIT)
174    s = format (s, "O-set ");
175
176  if (h->ver_o_c & NSH_C_BIT)
177    s = format (s, "C-set ");
178
179  s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
180              h->length, h->length * 4, h->md_type, h->next_protocol);
181  return s;
182}
183
184u8 * format_nsh_input_map_trace (u8 * s, va_list * args)
185{
186  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
187  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
188  nsh_input_trace_t * t
189    = va_arg (*args, nsh_input_trace_t *);
190
191  s = format (s, "\n  %U", format_nsh_header, &t->nsh_header,
192              (u32) sizeof (t->nsh_header) );
193
194  return s;
195}
196
197/**
198 * Action function to add or del an nsh map.
199 * Shared by both CLI and binary API
200 **/
201
202int nsh_add_del_map (nsh_add_del_map_args_t *a)
203{
204  nsh_main_t * nm = &nsh_main;
205  nsh_map_t *map = 0;
206  u32 key, *key_copy;
207  uword * entry;
208  hash_pair_t *hp;
209
210  key = a->map.nsp_nsi;
211
212  entry = hash_get_mem (nm->nsh_mapping_by_key, &key);
213
214  if (a->is_add)
215    {
216      /* adding an entry, must not already exist */
217      if (entry)
218        return -1; //TODO API_ERROR_INVALID_VALUE;
219
220      pool_get_aligned (nm->nsh_mappings, map, CLIB_CACHE_LINE_BYTES);
221      memset (map, 0, sizeof (*map));
222
223      /* copy from arg structure */
224      map->nsp_nsi = a->map.nsp_nsi;
225      map->mapped_nsp_nsi = a->map.mapped_nsp_nsi;
226      map->sw_if_index = a->map.sw_if_index;
227      map->next_node = a->map.next_node;
228
229
230      key_copy = clib_mem_alloc (sizeof (*key_copy));
231      clib_memcpy (key_copy, &key, sizeof (*key_copy));
232
233      hash_set_mem (nm->nsh_mapping_by_key, key_copy,
234                    map - nm->nsh_mappings);
235    }
236  else
237    {
238      if (!entry)
239	return -2 ; //TODO API_ERROR_NO_SUCH_ENTRY;
240
241      map = pool_elt_at_index (nm->nsh_mappings, entry[0]);
242      hp = hash_get_pair (nm->nsh_mapping_by_key, &key);
243      key_copy = (void *)(hp->key);
244      hash_unset_mem (nm->nsh_mapping_by_key, &key);
245      clib_mem_free (key_copy);
246
247      pool_put (nm->nsh_mappings, map);
248    }
249
250  return 0;
251}
252
253/**
254 * CLI command for NSH map
255 */
256
257
258static clib_error_t *
259nsh_add_del_map_command_fn (vlib_main_t * vm,
260			    unformat_input_t * input,
261			    vlib_cli_command_t * cmd)
262{
263  unformat_input_t _line_input, * line_input = &_line_input;
264  u8 is_add = 1;
265  u32 nsp, nsi, mapped_nsp, mapped_nsi;
266  int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
267  u32 next_node = ~0;
268  u32 sw_if_index = ~0; // temporary requirement to get this moved over to NSHSFC
269  nsh_add_del_map_args_t _a, * a = &_a;
270  int rv;
271
272  /* Get a line of input. */
273  if (! unformat_user (input, unformat_line_input, line_input))
274    return 0;
275
276  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
277    if (unformat (line_input, "del"))
278      is_add = 0;
279    else if (unformat (line_input, "nsp %d", &nsp))
280      nsp_set = 1;
281    else if (unformat (line_input, "nsi %d", &nsi))
282      nsi_set = 1;
283    else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
284      mapped_nsp_set = 1;
285    else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
286      mapped_nsi_set = 1;
287    else if (unformat (line_input, "encap-gre-intf %d", &sw_if_index))
288      next_node = NSH_INPUT_NEXT_ENCAP_GRE;
289    else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
290      next_node = NSH_INPUT_NEXT_ENCAP_VXLANGPE;
291    else if (unformat (line_input, "encap-none"))
292      next_node = NSH_INPUT_NEXT_DROP; // Once moved to NSHSFC see nsh.h:foreach_nsh_input_next to handle this case
293    else
294      return clib_error_return (0, "parse error: '%U'",
295                                format_unformat_error, line_input);
296  }
297
298  unformat_free (line_input);
299
300  if (nsp_set == 0 || nsi_set == 0)
301    return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
302
303  if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
304    return clib_error_return (0, "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
305
306  if (next_node == ~0)
307    return clib_error_return (0, "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]");
308
309  memset (a, 0, sizeof (*a));
310
311  /* set args structure */
312  a->is_add = is_add;
313  a->map.nsp_nsi = (nsp<< NSH_NSP_SHIFT) | nsi;
314  a->map.mapped_nsp_nsi = (mapped_nsp<< NSH_NSP_SHIFT) | mapped_nsi;
315  a->map.sw_if_index = sw_if_index;
316  a->map.next_node = next_node;
317
318
319  rv = nsh_add_del_map (a);
320
321  switch(rv)
322    {
323    case 0:
324      break;
325    case -1: //TODO API_ERROR_INVALID_VALUE:
326      return clib_error_return (0, "mapping already exists. Remove it first.");
327
328    case -2: // TODO API_ERROR_NO_SUCH_ENTRY:
329      return clib_error_return (0, "mapping does not exist.");
330
331    default:
332      return clib_error_return
333        (0, "nsh_add_del_map returned %d", rv);
334    }
335  return 0;
336}
337
338VLIB_CLI_COMMAND (create_nsh_map_command, static) = {
339  .path = "create nsh map",
340  .short_help =
341  "create nsh map nsp <nn> nsi <nn> [del] mapped-nsp <nn> mapped-nsi <nn> [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-none]\n",
342  .function = nsh_add_del_map_command_fn,
343};
344
345/** API message handler */
346static void vl_api_nsh_add_del_map_t_handler
347(vl_api_nsh_add_del_map_t * mp)
348{
349  vl_api_nsh_add_del_map_reply_t * rmp;
350  nsh_main_t * nm = &nsh_main;
351  int rv;
352  nsh_add_del_map_args_t *a = 0;
353
354  rv = nsh_add_del_map (a);
355
356  REPLY_MACRO(VL_API_NSH_ADD_DEL_MAP_REPLY);
357}
358
359/**
360 * CLI command for showing the mapping between NSH entries
361 */
362static clib_error_t *
363show_nsh_map_command_fn (vlib_main_t * vm,
364			 unformat_input_t * input,
365			 vlib_cli_command_t * cmd)
366{
367  nsh_main_t * nm = &nsh_main;
368  nsh_map_t * map;
369
370  if (pool_elts (nm->nsh_mappings) == 0)
371    vlib_cli_output (vm, "No nsh maps configured.");
372
373  pool_foreach (map, nm->nsh_mappings,
374		({
375		  vlib_cli_output (vm, "%U", format_nsh_map, map);
376		}));
377
378  return 0;
379}
380
381VLIB_CLI_COMMAND (show_nsh_map_command, static) = {
382  .path = "show nsh map",
383  .function = show_nsh_map_command_fn,
384};
385
386/**
387 * Action function for adding an NSH entry
388 */
389
390int nsh_add_del_entry (nsh_add_del_entry_args_t *a)
391{
392  nsh_main_t * nm = &nsh_main;
393  nsh_header_t *hdr = 0;
394  u32 key, *key_copy;
395  uword * entry;
396  hash_pair_t *hp;
397
398  key = a->nsh.nsp_nsi;
399
400  entry = hash_get_mem (nm->nsh_entry_by_key, &key);
401
402  if (a->is_add)
403    {
404      /* adding an entry, must not already exist */
405      if (entry)
406        return -1; // TODO VNET_API_ERROR_INVALID_VALUE;
407
408      pool_get_aligned (nm->nsh_entries, hdr, CLIB_CACHE_LINE_BYTES);
409      memset (hdr, 0, sizeof (*hdr));
410
411      /* copy from arg structure */
412#define _(x) hdr->x = a->nsh.x;
413      foreach_copy_nshhdr_field;
414#undef _
415
416      key_copy = clib_mem_alloc (sizeof (*key_copy));
417      clib_memcpy (key_copy, &key, sizeof (*key_copy));
418
419      hash_set_mem (nm->nsh_entry_by_key, key_copy,
420                    hdr - nm->nsh_entries);
421    }
422  else
423    {
424      if (!entry)
425	return -2; //TODO API_ERROR_NO_SUCH_ENTRY;
426
427      hdr = pool_elt_at_index (nm->nsh_entries, entry[0]);
428      hp = hash_get_pair (nm->nsh_entry_by_key, &key);
429      key_copy = (void *)(hp->key);
430      hash_unset_mem (nm->nsh_entry_by_key, &key);
431      clib_mem_free (key_copy);
432
433      pool_put (nm->nsh_entries, hdr);
434    }
435
436  return 0;
437}
438
439
440/**
441 * CLI command for adding NSH entry
442 */
443
444static clib_error_t *
445nsh_add_del_entry_command_fn (vlib_main_t * vm,
446			      unformat_input_t * input,
447			      vlib_cli_command_t * cmd)
448{
449  unformat_input_t _line_input, * line_input = &_line_input;
450  u8 is_add = 1;
451  u8 ver_o_c = 0;
452  u8 length = 0;
453  u8 md_type = 0;
454  u8 next_protocol = 1; /* default: ip4 */
455  u32 nsp;
456  u8 nsp_set = 0;
457  u32 nsi;
458  u8 nsi_set = 0;
459  u32 nsp_nsi;
460  u32 c1 = 0;
461  u32 c2 = 0;
462  u32 c3 = 0;
463  u32 c4 = 0;
464  u32 *tlvs = 0;
465  u32 tmp;
466  int rv;
467  nsh_add_del_entry_args_t _a, * a = &_a;
468
469  /* Get a line of input. */
470  if (! unformat_user (input, unformat_line_input, line_input))
471    return 0;
472
473  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
474    if (unformat (line_input, "del"))
475      is_add = 0;
476    else if (unformat (line_input, "version %d", &tmp))
477      ver_o_c |= (tmp & 3) << 6;
478    else if (unformat (line_input, "o-bit %d", &tmp))
479      ver_o_c |= (tmp & 1) << 5;
480    else if (unformat (line_input, "c-bit %d", &tmp))
481      ver_o_c |= (tmp & 1) << 4;
482    else if (unformat (line_input, "md-type %d", &tmp))
483      md_type = tmp;
484    else if (unformat(line_input, "next-ip4"))
485      next_protocol = 1;
486    else if (unformat(line_input, "next-ip6"))
487      next_protocol = 2;
488    else if (unformat(line_input, "next-ethernet"))
489      next_protocol = 3;
490    else if (unformat (line_input, "c1 %d", &c1))
491      ;
492    else if (unformat (line_input, "c2 %d", &c2))
493      ;
494    else if (unformat (line_input, "c3 %d", &c3))
495      ;
496    else if (unformat (line_input, "c4 %d", &c4))
497      ;
498    else if (unformat (line_input, "nsp %d", &nsp))
499      nsp_set = 1;
500    else if (unformat (line_input, "nsi %d", &nsi))
501      nsi_set = 1;
502    else if (unformat (line_input, "tlv %x"))
503      vec_add1 (tlvs, tmp);
504    else
505      return clib_error_return (0, "parse error: '%U'",
506                                format_unformat_error, line_input);
507  }
508
509  unformat_free (line_input);
510
511  if (nsp_set == 0)
512    return clib_error_return (0, "nsp not specified");
513
514  if (nsi_set == 0)
515    return clib_error_return (0, "nsi not specified");
516
517  if (md_type != 1)
518    return clib_error_return (0, "md-type 1 only supported at this time");
519
520  md_type = 1;
521  length = 6;
522
523  nsp_nsi = (nsp<<8) | nsi;
524
525  memset (a, 0, sizeof (*a));
526
527  a->is_add = is_add;
528
529#define _(x) a->nsh.x = x;
530  foreach_copy_nshhdr_field;
531#undef _
532
533  a->nsh.tlvs[0] = 0 ; // TODO FIXME this shouldn't be set 0 - in NSH_SFC project
534
535  rv = nsh_add_del_entry (a);
536
537  switch(rv)
538    {
539    case 0:
540      break;
541    default:
542      return clib_error_return
543        (0, "nsh_add_del_entry returned %d", rv);
544    }
545
546  return 0;
547}
548
549VLIB_CLI_COMMAND (create_nsh_entry_command, static) = {
550  .path = "create nsh entry",
551  .short_help =
552  "create nsh entry {nsp <nn> nsi <nn>} c1 <nn> c2 <nn> c3 <nn> c4 <nn>"
553  " [md-type <nn>] [tlv <xx>] [del]\n",
554  .function = nsh_add_del_entry_command_fn,
555};
556
557/** API message handler */
558static void vl_api_nsh_add_del_entry_t_handler
559(vl_api_nsh_add_del_entry_t * mp)
560{
561  vl_api_nsh_add_del_entry_reply_t * rmp;
562  nsh_main_t * nm = &nsh_main;
563  int rv;
564  nsh_add_del_entry_args_t *a = 0;
565
566  rv = nsh_add_del_entry (a);
567
568  REPLY_MACRO(VL_API_NSH_ADD_DEL_ENTRY_REPLY);
569}
570
571static clib_error_t *
572show_nsh_entry_command_fn (vlib_main_t * vm,
573			   unformat_input_t * input,
574			   vlib_cli_command_t * cmd)
575{
576  nsh_main_t * nm = &nsh_main;
577  nsh_header_t * hdr;
578
579  if (pool_elts (nm->nsh_entries) == 0)
580    vlib_cli_output (vm, "No nsh entries configured.");
581
582  pool_foreach (hdr, nm->nsh_entries,
583		({
584		  vlib_cli_output (vm, "%U", format_nsh_header, hdr);
585		}));
586
587  return 0;
588}
589
590VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
591  .path = "show nsh entry",
592  .function = show_nsh_entry_command_fn,
593};
594
595
596/* Set up the API message handling tables */
597static clib_error_t *
598nsh_plugin_api_hookup (vlib_main_t *vm)
599{
600  nsh_main_t * nm = &nsh_main;
601#define _(N,n)                                                  \
602  vl_msg_api_set_handlers((VL_API_##N + nm->msg_id_base),	\
603			  #n,					\
604			  vl_api_##n##_t_handler,		\
605			  vl_noop_handler,			\
606			  vl_api_##n##_t_endian,		\
607			  vl_api_##n##_t_print,			\
608			  sizeof(vl_api_##n##_t), 1);
609  foreach_nsh_plugin_api_msg;
610#undef _
611
612  return 0;
613}
614
615
616
617
618static uword
619nsh_input_map (vlib_main_t * vm,
620               vlib_node_runtime_t * node,
621               vlib_frame_t * from_frame)
622{
623  u32 n_left_from, next_index, *from, *to_next;
624  nsh_main_t * nm = &nsh_main;
625
626  from = vlib_frame_vector_args(from_frame);
627  n_left_from = from_frame->n_vectors;
628
629  next_index = node->cached_next_index;
630
631  while (n_left_from > 0)
632    {
633      u32 n_left_to_next;
634
635      vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
636
637      while (n_left_from >= 4 && n_left_to_next >= 2)
638	{
639	  u32 bi0, bi1;
640	  vlib_buffer_t * b0, *b1;
641	  u32 next0 = NSH_INPUT_NEXT_DROP, next1 = NSH_INPUT_NEXT_DROP;
642	  uword * entry0, *entry1;
643	  nsh_header_t * hdr0 = 0, *hdr1 = 0;
644	  u32 nsp_nsi0, nsp_nsi1;
645	  u32 error0, error1;
646	  nsh_map_t * map0 = 0, *map1 = 0;
647
648	  /* Prefetch next iteration. */
649	  {
650	    vlib_buffer_t * p2, *p3;
651
652	    p2 = vlib_get_buffer(vm, from[2]);
653	    p3 = vlib_get_buffer(vm, from[3]);
654
655	    vlib_prefetch_buffer_header(p2, LOAD);
656	    vlib_prefetch_buffer_header(p3, LOAD);
657
658	    CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
659	    CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
660	  }
661
662	  bi0 = from[0];
663	  bi1 = from[1];
664	  to_next[0] = bi0;
665	  to_next[1] = bi1;
666	  from += 2;
667	  to_next += 2;
668	  n_left_from -= 2;
669	  n_left_to_next -= 2;
670
671	  error0 = 0;
672	  error1 = 0;
673
674	  b0 = vlib_get_buffer(vm, bi0);
675	  hdr0 = vlib_buffer_get_current(b0);
676	  nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
677	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
678
679	  b1 = vlib_get_buffer(vm, bi1);
680	  hdr1 = vlib_buffer_get_current(b1);
681	  nsp_nsi1 = clib_net_to_host_u32(hdr1->nsp_nsi);
682	  entry1 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi1);
683
684	  if (PREDICT_FALSE(entry0 == 0))
685	    {
686	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
687	      goto trace0;
688	    }
689
690	  if (PREDICT_FALSE(entry1 == 0))
691	    {
692	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
693	      goto trace1;
694	    }
695
696	  /* Entry should point to a mapping ...*/
697	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
698	  map1 = pool_elt_at_index(nm->nsh_mappings, entry1[0]);
699
700	  if (PREDICT_FALSE(map0 == 0))
701	    {
702	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
703	      goto trace0;
704	    }
705
706	  if (PREDICT_FALSE(map1 == 0))
707	    {
708	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
709	      goto trace1;
710	    }
711
712	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
713	  entry1 = hash_get_mem(nm->nsh_entry_by_key, &map1->mapped_nsp_nsi);
714
715	  if (PREDICT_FALSE(entry0 == 0))
716	    {
717	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
718	      goto trace0;
719	    }
720	  if (PREDICT_FALSE(entry1 == 0))
721	    {
722	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
723	      goto trace1;
724	    }
725
726	  hdr0 = pool_elt_at_index(nm->nsh_entries, entry0[0]);
727	  hdr1 = pool_elt_at_index(nm->nsh_entries, entry1[0]);
728
729	  /* set up things for next node to transmit ie which node to handle it and where */
730	  next0 = map0->next_node;
731	  next1 = map1->next_node;
732	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
733	  vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index;
734
735	trace0: b0->error = error0 ? node->errors[error0] : 0;
736
737	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
738	    {
739	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
740	      tr->nsh_header = *hdr0;
741	    }
742
743	trace1: b1->error = error1 ? node->errors[error1] : 0;
744
745	  if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
746	    {
747	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
748	      tr->nsh_header = *hdr1;
749	    }
750
751	  vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
752					  n_left_to_next, bi0, bi1, next0, next1);
753
754	}
755
756      while (n_left_from > 0 && n_left_to_next > 0)
757	{
758	  u32 bi0;
759	  vlib_buffer_t * b0;
760	  u32 next0 = NSH_INPUT_NEXT_DROP;
761	  uword * entry0;
762	  nsh_header_t * hdr0 = 0;
763	  u32 nsp_nsi0;
764	  u32 error0;
765	  nsh_map_t * map0 = 0;
766
767	  bi0 = from[0];
768	  to_next[0] = bi0;
769	  from += 1;
770	  to_next += 1;
771	  n_left_from -= 1;
772	  n_left_to_next -= 1;
773	  error0 = 0;
774
775	  b0 = vlib_get_buffer(vm, bi0);
776	  hdr0 = vlib_buffer_get_current(b0);
777	  nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
778	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
779
780	  if (PREDICT_FALSE(entry0 == 0))
781	    {
782	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
783	      goto trace00;
784	    }
785
786	  /* Entry should point to a mapping ...*/
787	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
788
789	  if (PREDICT_FALSE(map0 == 0))
790	    {
791	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
792	      goto trace00;
793	    }
794
795	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
796
797	  if (PREDICT_FALSE(entry0 == 0))
798	    {
799	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
800	      goto trace00;
801	    }
802
803	  hdr0 = pool_elt_at_index(nm->nsh_entries, entry0[0]);
804
805	  /* set up things for next node to transmit ie which node to handle it and where */
806	  next0 = map0->next_node;
807	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
808
809	trace00: b0->error = error0 ? node->errors[error0] : 0;
810
811	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
812	    {
813	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
814	      tr->nsh_header = *hdr0;
815	    }
816
817	  vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
818					  n_left_to_next, bi0, next0);
819	}
820
821      vlib_put_next_frame(vm, node, next_index, n_left_to_next);
822
823    }
824
825  return from_frame->n_vectors;
826}
827
828
829static char * nsh_input_error_strings[] = {
830#define _(sym,string) string,
831  foreach_nsh_input_error
832#undef _
833};
834
835VLIB_REGISTER_NODE (nsh_input_node) = {
836  .function = nsh_input_map,
837  .name = "nsh-input",
838  .vector_size = sizeof (u32),
839  .format_trace = format_nsh_input_map_trace,
840  .format_buffer = format_nsh_header_with_length,
841  .type = VLIB_NODE_TYPE_INTERNAL,
842
843  .n_errors = ARRAY_LEN(nsh_input_error_strings),
844  .error_strings = nsh_input_error_strings,
845
846  .n_next_nodes = NSH_INPUT_N_NEXT,
847
848  .next_nodes = {
849#define _(s,n) [NSH_INPUT_NEXT_##s] = n,
850    foreach_nsh_input_next
851#undef _
852  },
853};
854
855clib_error_t *nsh_init (vlib_main_t *vm)
856{
857  nsh_main_t *nm = &nsh_main;
858  clib_error_t * error = 0;
859  vlib_node_t * vxlan4_gpe_input_node = 0;
860  vlib_node_t * vxlan6_gpe_input_node = 0;
861  vlib_node_t * gre_input_node = 0;
862  u8 * name;
863
864  /* Init the main structures from VPP */
865  nm->vlib_main = vm;
866  nm->vnet_main = vnet_get_main();
867
868  /* Various state maintenance mappings */
869  nm->nsh_mapping_by_key
870    = hash_create_mem (0, sizeof(u32), sizeof (uword));
871
872  nm->nsh_mapping_by_mapped_key
873    = hash_create_mem (0, sizeof(u32), sizeof (uword));
874
875  nm->nsh_entry_by_key
876    = hash_create_mem (0, sizeof(u32), sizeof (uword));
877
878  name = format (0, "nsh_%08x%c", api_version, 0);
879
880  /* Set up the API */
881  nm->msg_id_base = vl_msg_api_get_msg_ids
882    ((char *) name, VL_MSG_FIRST_AVAILABLE);
883
884  error = nsh_plugin_api_hookup (vm);
885
886  /* Add dispositions to nodes that feed nsh-input */
887  vxlan4_gpe_input_node = vlib_get_node_by_name (vm, (u8 *)"vxlan4-gpe-input");
888  ASSERT(vxlan4_gpe_input_node);
889  //alagalah - validate we don't really need to use the node value
890  vlib_node_add_next (vm, vxlan4_gpe_input_node->index, nsh_input_node.index);
891
892  vxlan6_gpe_input_node = vlib_get_node_by_name (vm, (u8 *)"vxlan6-gpe-input");
893  ASSERT(vxlan6_gpe_input_node);
894  vlib_node_add_next (vm, vxlan6_gpe_input_node->index, nsh_input_node.index);
895
896  gre_input_node = vlib_get_node_by_name (vm, (u8 *)"gre-input");
897  ASSERT(gre_input_node);
898  vlib_node_add_next (vm, gre_input_node->index, nsh_input_node.index);
899
900  vec_free(name);
901
902  return error;
903}
904
905VLIB_INIT_FUNCTION(nsh_init);
906