2 * nsh_api.c - nsh mapping api
4 * Copyright (c) 2019 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.
17 #include <vnet/vnet.h>
18 #include <vnet/plugin/plugin.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23 #include <vpp/app/version.h>
25 /* define message IDs */
26 #define vl_msg_id(n,h) n,
29 #include <nsh/nsh.api.h>
30 /* We'll want to know how many messages IDs we need... */
31 VL_MSG_FIRST_AVAILABLE,
35 /* define message structures */
37 #include <nsh/nsh.api.h>
40 /* define generated endian-swappers */
42 #include <nsh/nsh.api.h>
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48 #include <nsh/nsh.api.h>
51 /* Get the API version number */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <nsh/nsh.api.h>
56 #define vl_msg_name_crc_list
57 #include <nsh/nsh.api.h>
58 #undef vl_msg_name_crc_list
60 #define REPLY_MSG_ID_BASE nm->msg_id_base
61 #include <vlibapi/api_helper_macros.h>
63 /* List of message types that this plugin understands */
65 #define foreach_nsh_plugin_api_msg \
66 _(NSH_ADD_DEL_ENTRY, nsh_add_del_entry) \
67 _(NSH_ENTRY_DUMP, nsh_entry_dump) \
68 _(NSH_ADD_DEL_MAP, nsh_add_del_map) \
69 _(NSH_MAP_DUMP, nsh_map_dump)
72 * @brief CLI function for NSH admin up/down
82 nsh_interface_admin_up_down (vnet_main_t * vnm, u32 nsh_hw_if, u32 flags)
84 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
85 vnet_hw_interface_set_flags (vnm, nsh_hw_if,
86 VNET_HW_INTERFACE_FLAG_LINK_UP);
88 vnet_hw_interface_set_flags (vnm, nsh_hw_if, 0);
94 dummy_interface_tx (vlib_main_t * vm,
95 vlib_node_runtime_t * node, vlib_frame_t * frame)
97 clib_warning ("you shouldn't be here, leaking buffers...");
98 return frame->n_vectors;
102 * @brief Naming for NSH tunnel
104 * @param *s formatting string
107 * @return *s formatted string
111 format_nsh_name (u8 * s, va_list * args)
113 u32 dev_instance = va_arg (*args, u32);
114 return format (s, "nsh_tunnel%d", dev_instance);
118 VNET_DEVICE_CLASS (nsh_device_class, static) = {
120 .format_device_name = format_nsh_name,
121 .tx_function = dummy_interface_tx,
122 .admin_up_down_function = nsh_interface_admin_up_down,
126 static void send_nsh_entry_details
127 (nsh_entry_t * t, unix_shared_memory_queue_t * q, u32 context)
129 vl_api_nsh_entry_details_t *rmp;
130 nsh_main_t *nm = &nsh_main;
132 rmp = vl_msg_api_alloc (sizeof (*rmp));
133 clib_memset (rmp, 0, sizeof (*rmp));
135 rmp->_vl_msg_id = ntohs ((VL_API_NSH_ENTRY_DETAILS) + nm->msg_id_base);
136 rmp->ver_o_c = t->nsh_base.ver_o_c;
137 rmp->ttl = (t->nsh_base.ver_o_c & NSH_TTL_H4_MASK) << 2 |
138 (t->nsh_base.length & NSH_TTL_L2_MASK) >> 6;
139 rmp->length = t->nsh_base.length & NSH_LEN_MASK;
140 rmp->md_type = t->nsh_base.md_type;
141 rmp->next_protocol = t->nsh_base.next_protocol;
142 rmp->nsp_nsi = htonl (t->nsh_base.nsp_nsi);
144 if (t->nsh_base.md_type == 1)
147 rmp->c1 = htonl (t->md.md1_data.c1);
148 rmp->c2 = htonl (t->md.md1_data.c2);
149 rmp->c3 = htonl (t->md.md1_data.c3);
150 rmp->c4 = htonl (t->md.md1_data.c4);
152 else if (t->nsh_base.md_type == 2)
154 rmp->tlv_length = t->tlvs_len;
155 clib_memcpy (rmp->tlv, t->tlvs_data, t->tlvs_len);
158 rmp->context = context;
160 vl_msg_api_send_shmem (q, (u8 *) & rmp);
163 static void send_nsh_map_details
164 (nsh_map_t * t, unix_shared_memory_queue_t * q, u32 context)
166 vl_api_nsh_map_details_t *rmp;
167 nsh_main_t *nm = &nsh_main;
169 rmp = vl_msg_api_alloc (sizeof (*rmp));
170 clib_memset (rmp, 0, sizeof (*rmp));
172 rmp->_vl_msg_id = ntohs ((VL_API_NSH_MAP_DETAILS) + nm->msg_id_base);
173 rmp->nsp_nsi = htonl (t->nsp_nsi);
174 rmp->mapped_nsp_nsi = htonl (t->mapped_nsp_nsi);
175 rmp->nsh_action = htonl (t->nsh_action);
176 rmp->sw_if_index = htonl (t->sw_if_index);
177 rmp->rx_sw_if_index = htonl (t->rx_sw_if_index);
178 rmp->next_node = htonl (t->next_node);
180 rmp->context = context;
182 vl_msg_api_send_shmem (q, (u8 *) & rmp);
186 vl_api_nsh_map_dump_t_handler (vl_api_nsh_map_dump_t * mp)
188 unix_shared_memory_queue_t *q;
189 nsh_main_t *nm = &nsh_main;
193 q = vl_api_client_index_to_input_queue (mp->client_index);
199 map_index = ntohl (mp->map_index);
203 pool_foreach (t, nm->nsh_mappings, (
205 send_nsh_map_details (t, q,
212 if (map_index >= vec_len (nm->nsh_mappings))
216 t = &nm->nsh_mappings[map_index];
217 send_nsh_map_details (t, q, mp->context);
221 /** API message handler */
223 vl_api_nsh_add_del_map_t_handler (vl_api_nsh_add_del_map_t * mp)
225 vl_api_nsh_add_del_map_reply_t *rmp;
226 nsh_main_t *nm = &nsh_main;
228 nsh_add_del_map_args_t _a, *a = &_a;
231 a->is_add = mp->is_add;
232 a->map.nsp_nsi = ntohl (mp->nsp_nsi);
233 a->map.mapped_nsp_nsi = ntohl (mp->mapped_nsp_nsi);
234 a->map.nsh_action = ntohl (mp->nsh_action);
235 a->map.sw_if_index = ntohl (mp->sw_if_index);
236 a->map.rx_sw_if_index = ntohl (mp->rx_sw_if_index);
237 a->map.next_node = ntohl (mp->next_node);
239 rv = nsh_add_del_map (a, &map_index);
241 if ((a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN4)
242 | (a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN6))
244 rv = nsh_add_del_proxy_session (a);
247 REPLY_MACRO2 (VL_API_NSH_ADD_DEL_MAP_REPLY, (
256 nsh_header_rewrite (nsh_entry_t * nsh_entry)
260 nsh_base_header_t *nsh_base;
261 nsh_md1_data_t *nsh_md1;
262 nsh_main_t *nm = &nsh_main;
263 nsh_md2_data_t *opt0;
264 nsh_md2_data_t *limit0;
265 nsh_md2_data_t *nsh_md2;
266 nsh_option_map_t _nsh_option, *nsh_option = &_nsh_option;
267 u8 old_option_size = 0;
268 u8 new_option_size = 0;
270 vec_free (nsh_entry->rewrite);
271 if (nsh_entry->nsh_base.md_type == 1)
273 len = sizeof (nsh_base_header_t) + sizeof (nsh_md1_data_t);
275 else if (nsh_entry->nsh_base.md_type == 2)
277 /* set to maxim, maybe dataplane will add more TLVs */
278 len = MAX_NSH_HEADER_LEN;
280 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
281 clib_memset (rw, 0, len);
283 nsh_base = (nsh_base_header_t *) rw;
284 nsh_base->ver_o_c = nsh_entry->nsh_base.ver_o_c;
285 nsh_base->length = nsh_entry->nsh_base.length;
286 nsh_base->md_type = nsh_entry->nsh_base.md_type;
287 nsh_base->next_protocol = nsh_entry->nsh_base.next_protocol;
288 nsh_base->nsp_nsi = clib_host_to_net_u32 (nsh_entry->nsh_base.nsp_nsi);
290 if (nsh_base->md_type == 1)
292 nsh_md1 = (nsh_md1_data_t *) (rw + sizeof (nsh_base_header_t));
293 nsh_md1->c1 = clib_host_to_net_u32 (nsh_entry->md.md1_data.c1);
294 nsh_md1->c2 = clib_host_to_net_u32 (nsh_entry->md.md1_data.c2);
295 nsh_md1->c3 = clib_host_to_net_u32 (nsh_entry->md.md1_data.c3);
296 nsh_md1->c4 = clib_host_to_net_u32 (nsh_entry->md.md1_data.c4);
297 nsh_entry->rewrite_size = 24;
299 else if (nsh_base->md_type == 2)
301 opt0 = (nsh_md2_data_t *) (nsh_entry->tlvs_data);
302 limit0 = (nsh_md2_data_t *) ((u8 *) opt0 + nsh_entry->tlvs_len);
304 nsh_md2 = (nsh_md2_data_t *) (rw + sizeof (nsh_base_header_t));
305 nsh_entry->rewrite_size = sizeof (nsh_base_header_t);
307 while (opt0 < limit0)
309 old_option_size = sizeof (nsh_md2_data_t) + opt0->length;
310 /* round to 4-byte */
311 old_option_size = ((old_option_size + 3) >> 2) << 2;
313 nsh_option = nsh_md2_lookup_option (opt0->class, opt0->type);
314 if (nsh_option == NULL)
319 if (nm->add_options[nsh_option->option_id] != NULL)
321 if (0 != nm->add_options[nsh_option->option_id] ((u8 *) nsh_md2,
327 /* round to 4-byte */
328 new_option_size = ((new_option_size + 3) >> 2) << 2;
330 nsh_entry->rewrite_size += new_option_size;
332 (nsh_md2_data_t *) (((u8 *) nsh_md2) + new_option_size);
333 opt0 = (nsh_md2_data_t *) (((u8 *) opt0) + old_option_size);
338 opt0 = (nsh_md2_data_t *) (((u8 *) opt0) + old_option_size);
344 nsh_entry->rewrite = rw;
345 nsh_base->length = (nsh_base->length & NSH_TTL_L2_MASK) |
346 ((nsh_entry->rewrite_size >> 2) & NSH_LEN_MASK);
351 extern vnet_hw_interface_class_t nsh_hw_class;
354 * Action function to add or del an nsh map.
355 * Shared by both CLI and binary API
358 nsh_add_del_map (nsh_add_del_map_args_t * a, u32 * map_indexp)
360 nsh_main_t *nm = &nsh_main;
361 vnet_main_t *vnm = nm->vnet_main;
367 vnet_hw_interface_t *hi;
371 /* net order, so data plane could use nsh header to lookup directly */
372 key = clib_host_to_net_u32 (a->map.nsp_nsi);
374 entry = hash_get_mem (nm->nsh_mapping_by_key, &key);
378 /* adding an entry, must not already exist */
380 return -1; //TODO API_ERROR_INVALID_VALUE;
382 pool_get_aligned (nm->nsh_mappings, map, CLIB_CACHE_LINE_BYTES);
383 clib_memset (map, 0, sizeof (*map));
385 /* copy from arg structure */
386 map->nsp_nsi = a->map.nsp_nsi;
387 map->mapped_nsp_nsi = a->map.mapped_nsp_nsi;
388 map->nsh_action = a->map.nsh_action;
389 map->sw_if_index = a->map.sw_if_index;
390 map->rx_sw_if_index = a->map.rx_sw_if_index;
391 map->next_node = a->map.next_node;
392 map->adj_index = a->map.adj_index;
395 key_copy = clib_mem_alloc (sizeof (*key_copy));
396 clib_memcpy (key_copy, &key, sizeof (*key_copy));
398 hash_set_mem (nm->nsh_mapping_by_key, key_copy, map - nm->nsh_mappings);
399 map_index = map - nm->nsh_mappings;
401 if (vec_len (nm->free_nsh_tunnel_hw_if_indices) > 0)
403 nsh_hw_if = nm->free_nsh_tunnel_hw_if_indices
404 [vec_len (nm->free_nsh_tunnel_hw_if_indices) - 1];
405 _vec_len (nm->free_nsh_tunnel_hw_if_indices) -= 1;
407 hi = vnet_get_hw_interface (vnm, nsh_hw_if);
408 hi->dev_instance = map_index;
409 hi->hw_instance = hi->dev_instance;
413 nsh_hw_if = vnet_register_interface
414 (vnm, nsh_device_class.index, map_index, nsh_hw_class.index,
416 hi = vnet_get_hw_interface (vnm, nsh_hw_if);
417 hi->output_node_index = nsh_aware_vnf_proxy_node.index;
420 map->nsh_hw_if = nsh_hw_if;
421 map->nsh_sw_if = nsh_sw_if = hi->sw_if_index;
422 vec_validate_init_empty (nm->tunnel_index_by_sw_if_index, nsh_sw_if,
424 nm->tunnel_index_by_sw_if_index[nsh_sw_if] = key;
426 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
427 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
432 return -2; //TODO API_ERROR_NO_SUCH_ENTRY;
434 map = pool_elt_at_index (nm->nsh_mappings, entry[0]);
436 vnet_sw_interface_set_flags (vnm, map->nsh_sw_if,
437 VNET_SW_INTERFACE_FLAG_ADMIN_DOWN);
438 vec_add1 (nm->free_nsh_tunnel_hw_if_indices, map->nsh_sw_if);
439 nm->tunnel_index_by_sw_if_index[map->nsh_sw_if] = ~0;
441 hp = hash_get_pair (nm->nsh_mapping_by_key, &key);
442 key_copy = (void *) (hp->key);
443 hash_unset_mem (nm->nsh_mapping_by_key, &key);
444 clib_mem_free (key_copy);
446 pool_put (nm->nsh_mappings, map);
450 *map_indexp = map_index;
456 * Action function to add or del an nsh-proxy-session.
457 * Shared by both CLI and binary API
460 nsh_add_del_proxy_session (nsh_add_del_map_args_t * a)
462 nsh_main_t *nm = &nsh_main;
463 nsh_proxy_session_t *proxy = 0;
464 nsh_proxy_session_by_key_t key, *key_copy;
467 u32 nsp = 0, nsi = 0;
469 clib_memset (&key, 0, sizeof (key));
470 key.transport_type = a->map.next_node;
471 key.transport_index = a->map.sw_if_index;
473 entry = hash_get_mem (nm->nsh_proxy_session_by_key, &key);
477 /* adding an entry, must not already exist */
479 return -1; //TODO API_ERROR_INVALID_VALUE;
481 pool_get_aligned (nm->nsh_proxy_sessions, proxy, CLIB_CACHE_LINE_BYTES);
482 clib_memset (proxy, 0, sizeof (*proxy));
484 /* Nsi needs to minus 1 within NSH-Proxy */
485 nsp = (a->map.nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK;
486 nsi = a->map.nsp_nsi & NSH_NSI_MASK;
491 /* net order, so could use it to lookup nsh map table directly */
492 proxy->nsp_nsi = clib_host_to_net_u32 ((nsp << NSH_NSP_SHIFT) | nsi);
494 key_copy = clib_mem_alloc (sizeof (*key_copy));
495 clib_memcpy (key_copy, &key, sizeof (*key_copy));
497 hash_set_mem (nm->nsh_proxy_session_by_key, key_copy,
498 proxy - nm->nsh_proxy_sessions);
503 return -2; //TODO API_ERROR_NO_SUCH_ENTRY;
505 proxy = pool_elt_at_index (nm->nsh_proxy_sessions, entry[0]);
506 hp = hash_get_pair (nm->nsh_proxy_session_by_key, &key);
507 key_copy = (void *) (hp->key);
508 hash_unset_mem (nm->nsh_proxy_session_by_key, &key);
509 clib_mem_free (key_copy);
511 pool_put (nm->nsh_proxy_sessions, proxy);
518 * Action function for adding an NSH entry
519 * nsh_add_del_entry_args_t *a: host order
522 nsh_add_del_entry (nsh_add_del_entry_args_t * a, u32 * entry_indexp)
524 nsh_main_t *nm = &nsh_main;
525 nsh_entry_t *nsh_entry = 0;
529 u32 entry_index = ~0;
533 /* host order, because nsh map table stores nsp_nsi in host order */
534 key = a->nsh_entry.nsh_base.nsp_nsi;
536 entry_id = hash_get_mem (nm->nsh_entry_by_key, &key);
540 /* adding an entry, must not already exist */
542 return -1; // TODO VNET_API_ERROR_INVALID_VALUE;
544 pool_get_aligned (nm->nsh_entries, nsh_entry, CLIB_CACHE_LINE_BYTES);
545 clib_memset (nsh_entry, 0, sizeof (*nsh_entry));
547 /* copy from arg structure */
548 #define _(x) nsh_entry->nsh_base.x = a->nsh_entry.nsh_base.x;
549 foreach_copy_nsh_base_hdr_field;
552 if (a->nsh_entry.nsh_base.md_type == 1)
554 nsh_entry->md.md1_data.c1 = a->nsh_entry.md.md1_data.c1;
555 nsh_entry->md.md1_data.c2 = a->nsh_entry.md.md1_data.c2;
556 nsh_entry->md.md1_data.c3 = a->nsh_entry.md.md1_data.c3;
557 nsh_entry->md.md1_data.c4 = a->nsh_entry.md.md1_data.c4;
559 else if (a->nsh_entry.nsh_base.md_type == 2)
561 vec_free (nsh_entry->tlvs_data);
562 tlvs_len = a->nsh_entry.tlvs_len;
563 vec_validate_aligned (data, tlvs_len - 1, CLIB_CACHE_LINE_BYTES);
565 clib_memcpy (data, a->nsh_entry.tlvs_data, tlvs_len);
566 nsh_entry->tlvs_data = data;
567 nsh_entry->tlvs_len = tlvs_len;
568 vec_free (a->nsh_entry.tlvs_data);
571 nsh_header_rewrite (nsh_entry);
573 key_copy = clib_mem_alloc (sizeof (*key_copy));
574 clib_memcpy (key_copy, &key, sizeof (*key_copy));
576 hash_set_mem (nm->nsh_entry_by_key, key_copy,
577 nsh_entry - nm->nsh_entries);
578 entry_index = nsh_entry - nm->nsh_entries;
583 return -2; //TODO API_ERROR_NO_SUCH_ENTRY;
585 nsh_entry = pool_elt_at_index (nm->nsh_entries, entry_id[0]);
586 hp = hash_get_pair (nm->nsh_entry_by_key, &key);
587 key_copy = (void *) (hp->key);
588 hash_unset_mem (nm->nsh_entry_by_key, &key);
589 clib_mem_free (key_copy);
591 vec_free (nsh_entry->tlvs_data);
592 vec_free (nsh_entry->rewrite);
593 pool_put (nm->nsh_entries, nsh_entry);
597 *entry_indexp = entry_index;
603 /** API message handler */
604 static void vl_api_nsh_add_del_entry_t_handler
605 (vl_api_nsh_add_del_entry_t * mp)
607 vl_api_nsh_add_del_entry_reply_t *rmp;
608 nsh_main_t *nm = &nsh_main;
610 nsh_add_del_entry_args_t _a, *a = &_a;
611 u32 entry_index = ~0;
615 a->is_add = mp->is_add;
616 a->nsh_entry.nsh_base.ver_o_c =
617 (mp->ver_o_c & 0xF0) | ((mp->ttl & NSH_LEN_MASK) >> 2);
618 a->nsh_entry.nsh_base.length =
619 (mp->length & NSH_LEN_MASK) | ((mp->ttl & 0x3) << 6);
620 a->nsh_entry.nsh_base.md_type = mp->md_type;
621 a->nsh_entry.nsh_base.next_protocol = mp->next_protocol;
622 a->nsh_entry.nsh_base.nsp_nsi = ntohl (mp->nsp_nsi);
623 if (mp->md_type == 1)
625 a->nsh_entry.md.md1_data.c1 = ntohl (mp->c1);
626 a->nsh_entry.md.md1_data.c2 = ntohl (mp->c2);
627 a->nsh_entry.md.md1_data.c3 = ntohl (mp->c3);
628 a->nsh_entry.md.md1_data.c4 = ntohl (mp->c4);
630 else if (mp->md_type == 2)
632 tlvs_len = mp->tlv_length;
633 vec_validate_aligned (data, tlvs_len - 1, CLIB_CACHE_LINE_BYTES);
635 clib_memcpy (data, mp->tlv, tlvs_len);
636 a->nsh_entry.tlvs_data = data;
637 a->nsh_entry.tlvs_len = tlvs_len;
640 rv = nsh_add_del_entry (a, &entry_index);
642 REPLY_MACRO2 (VL_API_NSH_ADD_DEL_ENTRY_REPLY, (
651 vl_api_nsh_entry_dump_t_handler (vl_api_nsh_entry_dump_t * mp)
653 unix_shared_memory_queue_t *q;
654 nsh_main_t *nm = &nsh_main;
658 q = vl_api_client_index_to_input_queue (mp->client_index);
664 entry_index = ntohl (mp->entry_index);
666 if (~0 == entry_index)
668 pool_foreach (t, nm->nsh_entries, (
670 send_nsh_entry_details (t, q,
677 if (entry_index >= vec_len (nm->nsh_entries))
681 t = &nm->nsh_entries[entry_index];
682 send_nsh_entry_details (t, q, mp->context);
687 setup_message_id_table (nsh_main_t * nm, api_main_t * am)
689 #define _(id,n,crc) \
690 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + nm->msg_id_base);
691 foreach_vl_msg_name_crc_nsh;
695 /* Set up the API message handling tables */
696 static clib_error_t *
697 nsh_plugin_api_hookup (vlib_main_t * vm)
699 nsh_main_t *nm __attribute__ ((unused)) = &nsh_main;
701 vl_msg_api_set_handlers((VL_API_##N + nm->msg_id_base), \
703 vl_api_##n##_t_handler, \
705 vl_api_##n##_t_endian, \
706 vl_api_##n##_t_print, \
707 sizeof(vl_api_##n##_t), 1);
708 foreach_nsh_plugin_api_msg;
715 nsh_api_init (vlib_main_t * vm, nsh_main_t * nm)
720 name = format (0, "nsh_%08x%c", api_version, 0);
723 nm->msg_id_base = vl_msg_api_get_msg_ids
724 ((char *) name, VL_MSG_FIRST_AVAILABLE);
726 error = nsh_plugin_api_hookup (vm);
728 /* Add our API messages to the global name_crc hash table */
729 setup_message_id_table (nm, &api_main);
737 * fd.io coding-style-patch-verification: ON
740 * eval: (c-set-style "gnu")