2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/udp/udp_encap.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/dpo/drop_dpo.h>
22 * Registered DPO types for the IP header encapsulated, v4 or v6.
24 dpo_type_t udp_encap_dpo_types[FIB_PROTOCOL_MAX];
27 * Hash DB to map from client ID to VPP index.
34 udp_encap_t *udp_encap_pool;
37 * Stats for each UDP encap object
39 vlib_combined_counter_main_t udp_encap_counters;
42 udp_encap_get_w_id (u32 id)
44 udp_encap_t *ue = NULL;
47 uei = udp_encap_find (id);
49 if (INDEX_INVALID != uei)
51 ue = udp_encap_get (uei);
58 udp_encap_restack (udp_encap_t * ue)
60 dpo_stack (udp_encap_dpo_types[ue->ue_ip_proto],
61 fib_proto_to_dpo (ue->ue_ip_proto),
63 fib_entry_contribute_ip_forwarding (ue->ue_fib_entry_index));
67 udp_encap_add_and_lock (u32 id,
70 const ip46_address_t * src_ip,
71 const ip46_address_t * dst_ip,
73 u16 dst_port, udp_encap_fixup_flags_t flags)
78 uei = udp_encap_find (id);
80 if (INDEX_INVALID == uei)
84 pool_get_aligned (udp_encap_pool, ue, CLIB_CACHE_LINE_BYTES);
85 uei = ue - udp_encap_pool;
87 vlib_validate_combined_counter (&(udp_encap_counters), uei);
88 vlib_zero_combined_counter (&(udp_encap_counters), uei);
90 hash_set (udp_encap_db, id, uei);
92 fib_node_init (&ue->ue_fib_node, FIB_NODE_TYPE_UDP_ENCAP);
93 fib_node_lock (&ue->ue_fib_node);
94 ue->ue_fib_index = fib_index;
97 ue->ue_ip_proto = proto;
101 case FIB_PROTOCOL_IP4:
103 ue->ue_hdrs.ip4.ue_ip4.ip_version_and_header_length = 0x45;
104 ue->ue_hdrs.ip4.ue_ip4.ttl = 254;
105 ue->ue_hdrs.ip4.ue_ip4.protocol = IP_PROTOCOL_UDP;
106 ue->ue_hdrs.ip4.ue_ip4.src_address.as_u32 = src_ip->ip4.as_u32;
107 ue->ue_hdrs.ip4.ue_ip4.dst_address.as_u32 = dst_ip->ip4.as_u32;
108 ue->ue_hdrs.ip4.ue_ip4.checksum =
109 ip4_header_checksum (&ue->ue_hdrs.ip4.ue_ip4);
110 ue->ue_hdrs.ip4.ue_udp.src_port = clib_host_to_net_u16 (src_port);
111 ue->ue_hdrs.ip4.ue_udp.dst_port = clib_host_to_net_u16 (dst_port);
114 case FIB_PROTOCOL_IP6:
116 ue->ue_hdrs.ip6.ue_ip6.ip_version_traffic_class_and_flow_label =
117 clib_host_to_net_u32 (6 << 28);
118 ue->ue_hdrs.ip6.ue_ip6.hop_limit = 255;
119 ue->ue_hdrs.ip6.ue_ip6.protocol = IP_PROTOCOL_UDP;
120 ue->ue_hdrs.ip6.ue_ip6.src_address.as_u64[0] =
121 src_ip->ip6.as_u64[0];
122 ue->ue_hdrs.ip6.ue_ip6.src_address.as_u64[1] =
123 src_ip->ip6.as_u64[1];
124 ue->ue_hdrs.ip6.ue_ip6.dst_address.as_u64[0] =
125 dst_ip->ip6.as_u64[0];
126 ue->ue_hdrs.ip6.ue_ip6.dst_address.as_u64[1] =
127 dst_ip->ip6.as_u64[1];
128 ue->ue_hdrs.ip6.ue_udp.src_port = clib_host_to_net_u16 (src_port);
129 ue->ue_hdrs.ip6.ue_udp.dst_port = clib_host_to_net_u16 (dst_port);
137 * track the destination address
139 fib_prefix_t dst_pfx = {
145 ue->ue_fib_entry_index =
146 fib_table_entry_special_add (fib_index,
148 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
150 fib_entry_child_add (ue->ue_fib_entry_index,
151 FIB_NODE_TYPE_UDP_ENCAP, uei);
153 udp_encap_restack (ue);
158 * existing entry. updates not supported yet
166 udp_encap_contribute_forwarding (u32 id, dpo_proto_t proto, dpo_id_t * dpo)
170 uei = udp_encap_find (id);
172 if (INDEX_INVALID == uei)
174 dpo_copy (dpo, drop_dpo_get (proto));
180 ue = udp_encap_get (uei);
182 dpo_set (dpo, udp_encap_dpo_types[ue->ue_ip_proto], proto, uei);
187 udp_encap_find (u32 id)
191 p = hash_get (udp_encap_db, id);
196 return INDEX_INVALID;
200 udp_encap_lock (u32 id)
204 ue = udp_encap_get_w_id (id);
208 fib_node_lock (&ue->ue_fib_node);
213 udp_encap_unlock_w_index (index_t uei)
217 if (INDEX_INVALID == uei)
222 ue = udp_encap_get (uei);
226 fib_node_unlock (&ue->ue_fib_node);
231 udp_encap_unlock (u32 id)
235 ue = udp_encap_get_w_id (id);
239 fib_node_unlock (&ue->ue_fib_node);
244 udp_encap_dpo_lock (dpo_id_t * dpo)
248 ue = udp_encap_get (dpo->dpoi_index);
250 fib_node_lock (&ue->ue_fib_node);
254 udp_encap_dpo_unlock (dpo_id_t * dpo)
258 ue = udp_encap_get (dpo->dpoi_index);
260 fib_node_unlock (&ue->ue_fib_node);
264 format_udp_encap_i (u8 * s, va_list * args)
266 index_t uei = va_arg (*args, index_t);
267 u32 indent = va_arg (*args, u32);
268 u32 details = va_arg (*args, u32);
272 ue = udp_encap_get (uei);
275 s = format (s, "udp-ecap:[%d]: id:%d ip-fib-index:%d ",
276 uei, ue->ue_id, ue->ue_fib_index);
277 if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
279 s = format (s, "ip:[src:%U, dst:%U] udp:[src:%d, dst:%d]",
281 &ue->ue_hdrs.ip4.ue_ip4.src_address,
283 &ue->ue_hdrs.ip4.ue_ip4.dst_address,
284 clib_net_to_host_u16 (ue->ue_hdrs.ip4.ue_udp.src_port),
285 clib_net_to_host_u16 (ue->ue_hdrs.ip4.ue_udp.dst_port));
289 s = format (s, "ip:[src:%U, dst:%U] udp:[src:%d dst:%d]",
291 &ue->ue_hdrs.ip6.ue_ip6.src_address,
293 &ue->ue_hdrs.ip6.ue_ip6.dst_address,
294 clib_net_to_host_u16 (ue->ue_hdrs.ip6.ue_udp.src_port),
295 clib_net_to_host_u16 (ue->ue_hdrs.ip6.ue_udp.dst_port));
297 vlib_get_combined_counter (&(udp_encap_counters), uei, &to);
298 s = format (s, " to:[%Ld:%Ld]]", to.packets, to.bytes);
302 s = format (s, " locks:%d", ue->ue_fib_node.fn_locks);
303 s = format (s, "\n%UStacked on:", format_white_space, indent + 1);
304 s = format (s, "\n%U%U",
305 format_white_space, indent + 2,
306 format_dpo_id, &ue->ue_dpo, indent + 3);
312 udp_encap_get_stats (index_t uei, u64 * packets, u64 * bytes)
316 vlib_get_combined_counter (&(udp_encap_counters), uei, &to);
318 *packets = to.packets;
323 format_udp_encap_dpo (u8 * s, va_list * args)
325 index_t uei = va_arg (*args, index_t);
326 u32 indent = va_arg (*args, u32);
328 return (format (s, "%U", format_udp_encap_i, uei, indent, 1));
332 format_udp_encap (u8 * s, va_list * args)
334 u32 id = va_arg (*args, u32);
335 u32 details = va_arg (*args, u32);
338 uei = udp_encap_find (id);
340 if (INDEX_INVALID == uei)
342 return (format (s, "Invalid udp-encap ID: %d", id));
345 return (format (s, "%U", format_udp_encap_i, uei, 0, details));
349 udp_encap_from_fib_node (fib_node_t * node)
351 ASSERT (FIB_NODE_TYPE_UDP_ENCAP == node->fn_type);
352 return ((udp_encap_t *) (((char *) node) -
353 STRUCT_OFFSET_OF (udp_encap_t, ue_fib_node)));
357 * Function definition to backwalk a FIB node
359 static fib_node_back_walk_rc_t
360 udp_encap_fib_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
362 udp_encap_restack (udp_encap_from_fib_node (node));
364 return (FIB_NODE_BACK_WALK_CONTINUE);
368 * Function definition to get a FIB node from its index
371 udp_encap_fib_node_get (fib_node_index_t index)
375 ue = pool_elt_at_index (udp_encap_pool, index);
377 return (&ue->ue_fib_node);
381 * Function definition to inform the FIB node that its last lock has gone.
384 udp_encap_fib_last_lock_gone (fib_node_t * node)
388 ue = udp_encap_from_fib_node (node);
391 * reset the stacked DPO to unlock it
393 dpo_reset (&ue->ue_dpo);
394 hash_unset (udp_encap_db, ue->ue_id);
396 fib_entry_child_remove (ue->ue_fib_entry_index, ue->ue_fib_sibling);
397 fib_table_entry_delete_index (ue->ue_fib_entry_index, FIB_SOURCE_RR);
400 pool_put (udp_encap_pool, ue);
403 const static char *const udp4_encap_ip4_nodes[] = {
408 const static char *const udp4_encap_ip6_nodes[] = {
413 const static char *const udp4_encap_mpls_nodes[] = {
418 const static char *const udp4_encap_bier_nodes[] = {
423 const static char *const udp6_encap_ip4_nodes[] = {
428 const static char *const udp6_encap_ip6_nodes[] = {
433 const static char *const udp6_encap_mpls_nodes[] = {
438 const static char *const udp6_encap_bier_nodes[] = {
443 const static char *const *const udp4_encap_nodes[DPO_PROTO_NUM] = {
444 [DPO_PROTO_IP4] = udp4_encap_ip4_nodes,
445 [DPO_PROTO_IP6] = udp4_encap_ip6_nodes,
446 [DPO_PROTO_MPLS] = udp4_encap_mpls_nodes,
447 [DPO_PROTO_BIER] = udp4_encap_bier_nodes,
450 const static char *const *const udp6_encap_nodes[DPO_PROTO_NUM] = {
451 [DPO_PROTO_IP4] = udp6_encap_ip4_nodes,
452 [DPO_PROTO_IP6] = udp6_encap_ip6_nodes,
453 [DPO_PROTO_MPLS] = udp6_encap_mpls_nodes,
454 [DPO_PROTO_BIER] = udp6_encap_bier_nodes,
458 * Virtual function table registered by UDP encaps
459 * for participation in the FIB object graph.
461 const static fib_node_vft_t udp_encap_fib_vft = {
462 .fnv_get = udp_encap_fib_node_get,
463 .fnv_last_lock = udp_encap_fib_last_lock_gone,
464 .fnv_back_walk = udp_encap_fib_back_walk,
467 const static dpo_vft_t udp_encap_dpo_vft = {
468 .dv_lock = udp_encap_dpo_lock,
469 .dv_unlock = udp_encap_dpo_unlock,
470 .dv_format = format_udp_encap_dpo,
474 udp_encap_init (vlib_main_t * vm)
476 udp_encap_db = hash_create (0, sizeof (index_t));
478 fib_node_register_type (FIB_NODE_TYPE_UDP_ENCAP, &udp_encap_fib_vft);
480 udp_encap_dpo_types[FIB_PROTOCOL_IP4] =
481 dpo_register_new_type (&udp_encap_dpo_vft, udp4_encap_nodes);
482 udp_encap_dpo_types[FIB_PROTOCOL_IP6] =
483 dpo_register_new_type (&udp_encap_dpo_vft, udp6_encap_nodes);
488 VLIB_INIT_FUNCTION (udp_encap_init);
491 udp_encap_cli (vlib_main_t * vm,
492 unformat_input_t * main_input, vlib_cli_command_t * cmd)
494 unformat_input_t _line_input, *line_input = &_line_input;
495 clib_error_t *error = NULL;
496 ip46_address_t src_ip, dst_ip;
498 u32 src_port, dst_port;
499 udp_encap_fixup_flags_t flags;
500 fib_protocol_t fproto;
505 flags = UDP_ENCAP_FIXUP_NONE;
506 fproto = FIB_PROTOCOL_MAX;
510 /* Get a line of input. */
511 if (!unformat_user (main_input, unformat_line_input, line_input))
514 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
516 if (unformat (line_input, "id %d", &ue_id))
518 else if (unformat (line_input, "add"))
520 else if (unformat (line_input, "del"))
522 else if (unformat (line_input, "%U %U",
523 unformat_ip4_address,
524 &src_ip.ip4, unformat_ip4_address, &dst_ip.ip4))
525 fproto = FIB_PROTOCOL_IP4;
526 else if (unformat (line_input, "%U %U",
527 unformat_ip6_address,
528 &src_ip.ip6, unformat_ip6_address, &dst_ip.ip6))
529 fproto = FIB_PROTOCOL_IP6;
530 else if (unformat (line_input, "%d %d", &src_port, &dst_port))
532 else if (unformat (line_input, "%d", &dst_port))
534 else if (unformat (line_input, "table-id %d", &table_id))
536 else if (unformat (line_input, "src-port-is-entropy"))
537 flags |= UDP_ENCAP_FIXUP_UDP_SRC_PORT_ENTROPY;
540 error = unformat_parse_error (line_input);
548 clib_error_return (0, "An ID for the UDP encap instance is required");
552 if (!is_del && fproto != FIB_PROTOCOL_MAX)
557 fib_index = fib_table_find (fproto, table_id);
561 error = clib_error_return (0, "Nonexistent table id %d", table_id);
565 uei = udp_encap_add_and_lock (ue_id, fproto, fib_index,
567 src_port, dst_port, flags);
569 if (INDEX_INVALID == uei)
572 clib_error_return (0, "update to existing encap not supported %d",
579 udp_encap_unlock (ue_id);
584 clib_error_return (0,
585 "Some IP addresses would be usefull, don't you think?",
590 unformat_free (line_input);
595 udp_encap_walk (udp_encap_walk_cb_t cb, void *ctx)
600 pool_foreach_index(uei, udp_encap_pool,
602 if (WALK_STOP == cb(uei, ctx))
609 udp_encap_show (vlib_main_t * vm,
610 unformat_input_t * input, vlib_cli_command_t * cmd)
616 /* Get a line of input. */
617 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
619 if (unformat (input, "%d", &ue_id))
622 return clib_error_return (0, "unknown input `%U'",
623 format_unformat_error, input);
631 pool_foreach(ue, udp_encap_pool,
633 vlib_cli_output(vm, "%U", format_udp_encap, ue->ue_id, 0);
639 vlib_cli_output (vm, "%U", format_udp_encap, ue_id, 1);
646 VLIB_CLI_COMMAND (udp_encap_add_command, static) = {
648 .short_help = "udp encap [add|del] <id ID> <src-ip> <dst-ip> [<src-port>] <dst-port> [src-port-is-entropy] [table-id <table>]",
649 .function = udp_encap_cli,
652 VLIB_CLI_COMMAND (udp_encap_show_command, static) = {
653 .path = "show udp encap",
654 .short_help = "show udp encap [ID]",
655 .function = udp_encap_show,
661 * fd.io coding-style-patch-verification: ON
664 * eval: (c-set-style "gnu")