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