nsh.c revision b2808445
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
627    rmp = vl_msg_api_alloc (sizeof (*rmp));
628    memset (rmp, 0, sizeof (*rmp));
629
630    rmp->_vl_msg_id = ntohs(VL_API_NSH_ENTRY_DETAILS);
631    rmp->ver_o_c = t->ver_o_c;
632    rmp->length = t->length;
633    rmp->md_type = t->md_type;
634    rmp->next_protocol = t->next_protocol;
635    rmp->nsp_nsi = t->nsp_nsi;
636    rmp->c1 = t->c1;
637    rmp->c2 = t->c2;
638    rmp->c3 = t->c3;
639    rmp->c4 = t->c4;
640
641    rmp->context = context;
642
643    vl_msg_api_send_shmem (q, (u8 *)&rmp);
644}
645
646static void vl_api_nsh_entry_dump_t_handler
647(vl_api_nsh_entry_dump_t * mp)
648{
649    unix_shared_memory_queue_t * q;
650    nsh_main_t * nm = &nsh_main;
651    nsh_header_t * t;
652
653    q = vl_api_client_index_to_input_queue (mp->client_index);
654    if (q == 0) {
655        return;
656    }
657
658    pool_foreach (t, nm->nsh_entries,
659    ({
660	send_nsh_entry_details(t, q, mp->context);
661    }));
662}
663
664static void send_nsh_map_details
665(nsh_map_t * t, unix_shared_memory_queue_t * q, u32 context)
666{
667    vl_api_nsh_map_details_t * rmp;
668
669    rmp = vl_msg_api_alloc (sizeof (*rmp));
670    memset (rmp, 0, sizeof (*rmp));
671
672    rmp->_vl_msg_id = ntohs(VL_API_NSH_MAP_DETAILS);
673    rmp->nsp_nsi = t->nsp_nsi;
674    rmp->mapped_nsp_nsi = t->mapped_nsp_nsi;
675    rmp->sw_if_index = t->sw_if_index;
676    rmp->next_node = t->next_node;
677
678    rmp->context = context;
679
680    vl_msg_api_send_shmem (q, (u8 *)&rmp);
681}
682
683static void vl_api_nsh_map_dump_t_handler
684(vl_api_nsh_map_dump_t * mp)
685{
686    unix_shared_memory_queue_t * q;
687    nsh_main_t * nm = &nsh_main;
688    nsh_map_t * t;
689
690    q = vl_api_client_index_to_input_queue (mp->client_index);
691    if (q == 0) {
692        return;
693    }
694
695    pool_foreach (t, nm->nsh_mappings,
696    ({
697	send_nsh_map_details(t, q, mp->context);
698    }));
699}
700
701static clib_error_t *
702show_nsh_entry_command_fn (vlib_main_t * vm,
703			   unformat_input_t * input,
704			   vlib_cli_command_t * cmd)
705{
706  nsh_main_t * nm = &nsh_main;
707  nsh_header_t * hdr;
708
709  if (pool_elts (nm->nsh_entries) == 0)
710    vlib_cli_output (vm, "No nsh entries configured.");
711
712  pool_foreach (hdr, nm->nsh_entries,
713		({
714		  vlib_cli_output (vm, "%U", format_nsh_header, hdr);
715		}));
716
717  return 0;
718}
719
720VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
721  .path = "show nsh entry",
722  .function = show_nsh_entry_command_fn,
723};
724
725
726/* Set up the API message handling tables */
727static clib_error_t *
728nsh_plugin_api_hookup (vlib_main_t *vm)
729{
730  nsh_main_t * nm = &nsh_main;
731#define _(N,n)                                                  \
732  vl_msg_api_set_handlers((VL_API_##N + nm->msg_id_base),	\
733			  #n,					\
734			  vl_api_##n##_t_handler,		\
735			  vl_noop_handler,			\
736			  vl_api_##n##_t_endian,		\
737			  vl_api_##n##_t_print,			\
738			  sizeof(vl_api_##n##_t), 1);
739  foreach_nsh_plugin_api_msg;
740#undef _
741
742  return 0;
743}
744
745
746
747
748static uword
749nsh_input_map (vlib_main_t * vm,
750               vlib_node_runtime_t * node,
751               vlib_frame_t * from_frame)
752{
753  u32 n_left_from, next_index, *from, *to_next;
754  nsh_main_t * nm = &nsh_main;
755
756  from = vlib_frame_vector_args(from_frame);
757  n_left_from = from_frame->n_vectors;
758
759  next_index = node->cached_next_index;
760
761  while (n_left_from > 0)
762    {
763      u32 n_left_to_next;
764
765      vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
766
767      while (n_left_from >= 4 && n_left_to_next >= 2)
768	{
769	  u32 bi0, bi1;
770	  vlib_buffer_t * b0, *b1;
771	  u32 next0 = NSH_INPUT_NEXT_DROP, next1 = NSH_INPUT_NEXT_DROP;
772	  uword * entry0, *entry1;
773	  nsh_header_t * hdr0 = 0, *hdr1 = 0;
774	  u32 nsp_nsi0, nsp_nsi1;
775	  u32 error0, error1;
776	  nsh_map_t * map0 = 0, *map1 = 0;
777
778	  /* Prefetch next iteration. */
779	  {
780	    vlib_buffer_t * p2, *p3;
781
782	    p2 = vlib_get_buffer(vm, from[2]);
783	    p3 = vlib_get_buffer(vm, from[3]);
784
785	    vlib_prefetch_buffer_header(p2, LOAD);
786	    vlib_prefetch_buffer_header(p3, LOAD);
787
788	    CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
789	    CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
790	  }
791
792	  bi0 = from[0];
793	  bi1 = from[1];
794	  to_next[0] = bi0;
795	  to_next[1] = bi1;
796	  from += 2;
797	  to_next += 2;
798	  n_left_from -= 2;
799	  n_left_to_next -= 2;
800
801	  error0 = 0;
802	  error1 = 0;
803
804	  b0 = vlib_get_buffer(vm, bi0);
805	  hdr0 = vlib_buffer_get_current(b0);
806	  nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
807	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
808
809	  b1 = vlib_get_buffer(vm, bi1);
810	  hdr1 = vlib_buffer_get_current(b1);
811	  nsp_nsi1 = clib_net_to_host_u32(hdr1->nsp_nsi);
812	  entry1 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi1);
813
814	  if (PREDICT_FALSE(entry0 == 0))
815	    {
816	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
817	      goto trace0;
818	    }
819
820	  if (PREDICT_FALSE(entry1 == 0))
821	    {
822	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
823	      goto trace1;
824	    }
825
826	  /* Entry should point to a mapping ...*/
827	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
828	  map1 = pool_elt_at_index(nm->nsh_mappings, entry1[0]);
829
830	  if (PREDICT_FALSE(map0 == 0))
831	    {
832	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
833	      goto trace0;
834	    }
835
836	  if (PREDICT_FALSE(map1 == 0))
837	    {
838	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
839	      goto trace1;
840	    }
841
842	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
843	  entry1 = hash_get_mem(nm->nsh_entry_by_key, &map1->mapped_nsp_nsi);
844
845	  if (PREDICT_FALSE(entry0 == 0))
846	    {
847	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
848	      goto trace0;
849	    }
850	  if (PREDICT_FALSE(entry1 == 0))
851	    {
852	      error1 = NSH_INPUT_ERROR_NO_MAPPING;
853	      goto trace1;
854	    }
855
856	  hdr0 = pool_elt_at_index(nm->nsh_entries, entry0[0]);
857	  hdr1 = pool_elt_at_index(nm->nsh_entries, entry1[0]);
858
859	  /* set up things for next node to transmit ie which node to handle it and where */
860	  next0 = map0->next_node;
861	  next1 = map1->next_node;
862	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
863	  vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index;
864
865	trace0: b0->error = error0 ? node->errors[error0] : 0;
866
867	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
868	    {
869	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
870	      tr->nsh_header = *hdr0;
871	    }
872
873	trace1: b1->error = error1 ? node->errors[error1] : 0;
874
875	  if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
876	    {
877	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
878	      tr->nsh_header = *hdr1;
879	    }
880
881	  vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
882					  n_left_to_next, bi0, bi1, next0, next1);
883
884	}
885
886      while (n_left_from > 0 && n_left_to_next > 0)
887	{
888	  u32 bi0;
889	  vlib_buffer_t * b0;
890	  u32 next0 = NSH_INPUT_NEXT_DROP;
891	  uword * entry0;
892	  nsh_header_t * hdr0 = 0;
893	  u32 nsp_nsi0;
894	  u32 error0;
895	  nsh_map_t * map0 = 0;
896
897	  bi0 = from[0];
898	  to_next[0] = bi0;
899	  from += 1;
900	  to_next += 1;
901	  n_left_from -= 1;
902	  n_left_to_next -= 1;
903	  error0 = 0;
904
905	  b0 = vlib_get_buffer(vm, bi0);
906	  hdr0 = vlib_buffer_get_current(b0);
907	  nsp_nsi0 = clib_net_to_host_u32(hdr0->nsp_nsi);
908	  entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
909
910	  if (PREDICT_FALSE(entry0 == 0))
911	    {
912	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
913	      goto trace00;
914	    }
915
916	  /* Entry should point to a mapping ...*/
917	  map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
918
919	  if (PREDICT_FALSE(map0 == 0))
920	    {
921	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
922	      goto trace00;
923	    }
924
925	  entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
926
927	  if (PREDICT_FALSE(entry0 == 0))
928	    {
929	      error0 = NSH_INPUT_ERROR_NO_MAPPING;
930	      goto trace00;
931	    }
932
933	  hdr0 = pool_elt_at_index(nm->nsh_entries, entry0[0]);
934
935	  /* set up things for next node to transmit ie which node to handle it and where */
936	  next0 = map0->next_node;
937	  vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
938
939	trace00: b0->error = error0 ? node->errors[error0] : 0;
940
941	  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
942	    {
943	      nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
944	      tr->nsh_header = *hdr0;
945	    }
946
947	  vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
948					  n_left_to_next, bi0, next0);
949	}
950
951      vlib_put_next_frame(vm, node, next_index, n_left_to_next);
952
953    }
954
955  return from_frame->n_vectors;
956}
957
958
959static char * nsh_input_error_strings[] = {
960#define _(sym,string) string,
961  foreach_nsh_input_error
962#undef _
963};
964
965VLIB_REGISTER_NODE (nsh_input_node) = {
966  .function = nsh_input_map,
967  .name = "nsh-input",
968  .vector_size = sizeof (u32),
969  .format_trace = format_nsh_input_map_trace,
970  .format_buffer = format_nsh_header_with_length,
971  .type = VLIB_NODE_TYPE_INTERNAL,
972
973  .n_errors = ARRAY_LEN(nsh_input_error_strings),
974  .error_strings = nsh_input_error_strings,
975
976  .n_next_nodes = NSH_INPUT_N_NEXT,
977
978  .next_nodes = {
979#define _(s,n) [NSH_INPUT_NEXT_##s] = n,
980    foreach_nsh_input_next
981#undef _
982  },
983};
984
985clib_error_t *nsh_init (vlib_main_t *vm)
986{
987  nsh_main_t *nm = &nsh_main;
988  clib_error_t * error = 0;
989  vlib_node_t * vxlan4_gpe_input_node = 0;
990  vlib_node_t * vxlan6_gpe_input_node = 0;
991  vlib_node_t * gre_input_node = 0;
992  u8 * name;
993
994  /* Init the main structures from VPP */
995  nm->vlib_main = vm;
996  nm->vnet_main = vnet_get_main();
997
998  /* Various state maintenance mappings */
999  nm->nsh_mapping_by_key
1000    = hash_create_mem (0, sizeof(u32), sizeof (uword));
1001
1002  nm->nsh_mapping_by_mapped_key
1003    = hash_create_mem (0, sizeof(u32), sizeof (uword));
1004
1005  nm->nsh_entry_by_key
1006    = hash_create_mem (0, sizeof(u32), sizeof (uword));
1007
1008  name = format (0, "nsh_%08x%c", api_version, 0);
1009
1010  /* Set up the API */
1011  nm->msg_id_base = vl_msg_api_get_msg_ids
1012    ((char *) name, VL_MSG_FIRST_AVAILABLE);
1013
1014  error = nsh_plugin_api_hookup (vm);
1015
1016  /* Add dispositions to nodes that feed nsh-input */
1017  vxlan4_gpe_input_node = vlib_get_node_by_name (vm, (u8 *)"vxlan4-gpe-input");
1018  ASSERT(vxlan4_gpe_input_node);
1019  //alagalah - validate we don't really need to use the node value
1020  vlib_node_add_next (vm, vxlan4_gpe_input_node->index, nsh_input_node.index);
1021
1022  vxlan6_gpe_input_node = vlib_get_node_by_name (vm, (u8 *)"vxlan6-gpe-input");
1023  ASSERT(vxlan6_gpe_input_node);
1024  vlib_node_add_next (vm, vxlan6_gpe_input_node->index, nsh_input_node.index);
1025
1026  gre_input_node = vlib_get_node_by_name (vm, (u8 *)"gre-input");
1027  ASSERT(gre_input_node);
1028  vlib_node_add_next (vm, gre_input_node->index, nsh_input_node.index);
1029
1030  vec_free(name);
1031
1032  return error;
1033}
1034
1035VLIB_INIT_FUNCTION(nsh_init);
1036