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 udp_encap_get_w_id (u32 id)
39 udp_encap_t *ue = NULL;
42 uei = udp_encap_find (id);
44 if (INDEX_INVALID != uei)
46 ue = udp_encap_get (uei);
53 udp_encap_restack (udp_encap_t * ue)
55 dpo_stack (udp_encap_dpo_types[ue->ue_ip_proto],
56 fib_proto_to_dpo (ue->ue_ip_proto),
58 fib_entry_contribute_ip_forwarding (ue->ue_fib_entry_index));
62 udp_encap_add_and_lock (u32 id,
65 const ip46_address_t * src_ip,
66 const ip46_address_t * dst_ip,
68 u16 dst_port, udp_encap_fixup_flags_t flags)
73 uei = udp_encap_find (id);
75 if (INDEX_INVALID == uei)
79 pool_get (udp_encap_pool, ue);
80 uei = ue - udp_encap_pool;
82 hash_set (udp_encap_db, id, uei);
84 fib_node_init (&ue->ue_fib_node, FIB_NODE_TYPE_UDP_ENCAP);
85 fib_node_lock (&ue->ue_fib_node);
86 ue->ue_fib_index = fib_index;
89 ue->ue_ip_proto = proto;
93 case FIB_PROTOCOL_IP4:
95 ue->ue_hdrs.ip4.ue_ip4.ip_version_and_header_length = 0x45;
96 ue->ue_hdrs.ip4.ue_ip4.ttl = 254;
97 ue->ue_hdrs.ip4.ue_ip4.protocol = IP_PROTOCOL_UDP;
98 ue->ue_hdrs.ip4.ue_ip4.src_address.as_u32 = src_ip->ip4.as_u32;
99 ue->ue_hdrs.ip4.ue_ip4.dst_address.as_u32 = dst_ip->ip4.as_u32;
100 ue->ue_hdrs.ip4.ue_ip4.checksum =
101 ip4_header_checksum (&ue->ue_hdrs.ip4.ue_ip4);
102 ue->ue_hdrs.ip4.ue_udp.src_port = clib_host_to_net_u16 (src_port);
103 ue->ue_hdrs.ip4.ue_udp.dst_port = clib_host_to_net_u16 (dst_port);
106 case FIB_PROTOCOL_IP6:
108 ue->ue_hdrs.ip6.ue_ip6.ip_version_traffic_class_and_flow_label =
109 clib_host_to_net_u32 (6 << 28);
110 ue->ue_hdrs.ip6.ue_ip6.hop_limit = 255;
111 ue->ue_hdrs.ip6.ue_ip6.protocol = IP_PROTOCOL_UDP;
112 ue->ue_hdrs.ip6.ue_ip6.src_address.as_u64[0] =
113 src_ip->ip6.as_u64[0];
114 ue->ue_hdrs.ip6.ue_ip6.src_address.as_u64[1] =
115 src_ip->ip6.as_u64[1];
116 ue->ue_hdrs.ip6.ue_ip6.dst_address.as_u64[0] =
117 dst_ip->ip6.as_u64[0];
118 ue->ue_hdrs.ip6.ue_ip6.dst_address.as_u64[1] =
119 dst_ip->ip6.as_u64[1];
120 ue->ue_hdrs.ip6.ue_udp.src_port = clib_host_to_net_u16 (src_port);
121 ue->ue_hdrs.ip6.ue_udp.dst_port = clib_host_to_net_u16 (dst_port);
129 * track the destination address
131 fib_prefix_t dst_pfx = {
137 ue->ue_fib_entry_index =
138 fib_table_entry_special_add (fib_index,
140 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
142 fib_entry_child_add (ue->ue_fib_entry_index,
143 FIB_NODE_TYPE_UDP_ENCAP, uei);
145 udp_encap_restack (ue);
150 * existing entry. updates not supported yet
158 udp_encap_contribute_forwarding (u32 id, dpo_proto_t proto, dpo_id_t * dpo)
162 uei = udp_encap_find (id);
164 if (INDEX_INVALID == uei)
166 dpo_copy (dpo, drop_dpo_get (proto));
172 ue = udp_encap_get (uei);
174 dpo_set (dpo, udp_encap_dpo_types[ue->ue_ip_proto], proto, uei);
179 udp_encap_find (u32 id)
183 p = hash_get (udp_encap_db, id);
188 return INDEX_INVALID;
192 udp_encap_lock (u32 id)
196 ue = udp_encap_get_w_id (id);
200 fib_node_lock (&ue->ue_fib_node);
205 udp_encap_unlock_w_index (index_t uei)
209 if (INDEX_INVALID == uei)
214 ue = udp_encap_get (uei);
218 fib_node_unlock (&ue->ue_fib_node);
223 udp_encap_unlock (u32 id)
227 ue = udp_encap_get_w_id (id);
231 fib_node_unlock (&ue->ue_fib_node);
236 udp_encap_dpo_lock (dpo_id_t * dpo)
240 ue = udp_encap_get (dpo->dpoi_index);
242 fib_node_lock (&ue->ue_fib_node);
246 udp_encap_dpo_unlock (dpo_id_t * dpo)
250 ue = udp_encap_get (dpo->dpoi_index);
252 fib_node_unlock (&ue->ue_fib_node);
256 format_udp_encap_i (u8 * s, va_list * args)
258 index_t uei = va_arg (*args, index_t);
259 u32 indent = va_arg (*args, u32);
260 u32 details = va_arg (*args, u32);
263 ue = udp_encap_get (uei);
266 s = format (s, "udp-ecap:[%d]: id:%d ip-fib-index:%d",
267 uei, ue->ue_id, ue->ue_fib_index);
268 if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
270 s = format (s, "ip:[src:%U, dst:%U] udp:[src:%d, dst:%d]",
272 &ue->ue_hdrs.ip4.ue_ip4.src_address,
274 &ue->ue_hdrs.ip4.ue_ip4.dst_address,
275 clib_net_to_host_u16 (ue->ue_hdrs.ip4.ue_udp.src_port),
276 clib_net_to_host_u16 (ue->ue_hdrs.ip4.ue_udp.dst_port));
280 s = format (s, "ip:[src:%U, dst:%U] udp:[src:%d dst:%d]",
282 &ue->ue_hdrs.ip6.ue_ip6.src_address,
284 &ue->ue_hdrs.ip6.ue_ip6.dst_address,
285 clib_net_to_host_u16 (ue->ue_hdrs.ip6.ue_udp.src_port),
286 clib_net_to_host_u16 (ue->ue_hdrs.ip6.ue_udp.dst_port));
290 s = format (s, " locks:%d", ue->ue_fib_node.fn_locks);
291 s = format (s, "\n%UStacked on:", format_white_space, indent + 1);
292 s = format (s, "\n%U%U",
293 format_white_space, indent + 2,
294 format_dpo_id, &ue->ue_dpo, indent + 3);
300 format_udp_encap_dpo (u8 * s, va_list * args)
302 index_t uei = va_arg (*args, index_t);
303 u32 indent = va_arg (*args, u32);
305 return (format (s, "%U", format_udp_encap_i, uei, indent, 1));
309 format_udp_encap (u8 * s, va_list * args)
311 u32 id = va_arg (*args, u32);
312 u32 details = va_arg (*args, u32);
315 uei = udp_encap_find (id);
317 if (INDEX_INVALID == uei)
319 return (format (s, "Invalid udp-encap ID: %d", id));
322 return (format (s, "%U", format_udp_encap_i, uei, 0, details));
326 udp_encap_from_fib_node (fib_node_t * node)
328 ASSERT (FIB_NODE_TYPE_UDP_ENCAP == node->fn_type);
329 return ((udp_encap_t *) (((char *) node) -
330 STRUCT_OFFSET_OF (udp_encap_t, ue_fib_node)));
334 * Function definition to backwalk a FIB node
336 static fib_node_back_walk_rc_t
337 udp_encap_fib_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
339 udp_encap_restack (udp_encap_from_fib_node (node));
341 return (FIB_NODE_BACK_WALK_CONTINUE);
345 * Function definition to get a FIB node from its index
348 udp_encap_fib_node_get (fib_node_index_t index)
352 ue = pool_elt_at_index (udp_encap_pool, index);
354 return (&ue->ue_fib_node);
358 * Function definition to inform the FIB node that its last lock has gone.
361 udp_encap_fib_last_lock_gone (fib_node_t * node)
365 ue = udp_encap_from_fib_node (node);
368 * reset the stacked DPO to unlock it
370 dpo_reset (&ue->ue_dpo);
371 hash_unset (udp_encap_db, ue->ue_id);
373 fib_entry_child_remove (ue->ue_fib_entry_index, ue->ue_fib_sibling);
374 fib_table_entry_delete_index (ue->ue_fib_entry_index, FIB_SOURCE_RR);
377 pool_put (udp_encap_pool, ue);
380 const static char *const udp4_encap_ip4_nodes[] = {
385 const static char *const udp4_encap_ip6_nodes[] = {
390 const static char *const udp4_encap_mpls_nodes[] = {
395 const static char *const udp4_encap_bier_nodes[] = {
400 const static char *const udp6_encap_ip4_nodes[] = {
405 const static char *const udp6_encap_ip6_nodes[] = {
410 const static char *const udp6_encap_mpls_nodes[] = {
415 const static char *const udp6_encap_bier_nodes[] = {
420 const static char *const *const udp4_encap_nodes[DPO_PROTO_NUM] = {
421 [DPO_PROTO_IP4] = udp4_encap_ip4_nodes,
422 [DPO_PROTO_IP6] = udp4_encap_ip6_nodes,
423 [DPO_PROTO_MPLS] = udp4_encap_mpls_nodes,
424 [DPO_PROTO_BIER] = udp4_encap_bier_nodes,
427 const static char *const *const udp6_encap_nodes[DPO_PROTO_NUM] = {
428 [DPO_PROTO_IP4] = udp6_encap_ip4_nodes,
429 [DPO_PROTO_IP6] = udp6_encap_ip6_nodes,
430 [DPO_PROTO_MPLS] = udp6_encap_mpls_nodes,
431 [DPO_PROTO_BIER] = udp6_encap_bier_nodes,
435 * Virtual function table registered by UDP encaps
436 * for participation in the FIB object graph.
438 const static fib_node_vft_t udp_encap_fib_vft = {
439 .fnv_get = udp_encap_fib_node_get,
440 .fnv_last_lock = udp_encap_fib_last_lock_gone,
441 .fnv_back_walk = udp_encap_fib_back_walk,
444 const static dpo_vft_t udp_encap_dpo_vft = {
445 .dv_lock = udp_encap_dpo_lock,
446 .dv_unlock = udp_encap_dpo_unlock,
447 .dv_format = format_udp_encap_dpo,
451 udp_encap_init (vlib_main_t * vm)
453 udp_encap_db = hash_create (0, sizeof (index_t));
455 fib_node_register_type (FIB_NODE_TYPE_UDP_ENCAP, &udp_encap_fib_vft);
457 udp_encap_dpo_types[FIB_PROTOCOL_IP4] =
458 dpo_register_new_type (&udp_encap_dpo_vft, udp4_encap_nodes);
459 udp_encap_dpo_types[FIB_PROTOCOL_IP6] =
460 dpo_register_new_type (&udp_encap_dpo_vft, udp6_encap_nodes);
465 VLIB_INIT_FUNCTION (udp_encap_init);
468 udp_encap_cli (vlib_main_t * vm,
469 unformat_input_t * main_input, vlib_cli_command_t * cmd)
471 unformat_input_t _line_input, *line_input = &_line_input;
472 clib_error_t *error = NULL;
473 ip46_address_t src_ip, dst_ip;
475 u32 src_port, dst_port;
476 udp_encap_fixup_flags_t flags;
477 fib_protocol_t fproto;
482 flags = UDP_ENCAP_FIXUP_NONE;
483 fproto = FIB_PROTOCOL_MAX;
487 /* Get a line of input. */
488 if (!unformat_user (main_input, unformat_line_input, line_input))
491 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
493 if (unformat (line_input, "id %d", &ue_id))
495 else if (unformat (line_input, "add"))
497 else if (unformat (line_input, "del"))
499 else if (unformat (line_input, "%U %U",
500 unformat_ip4_address,
501 &src_ip.ip4, unformat_ip4_address, &dst_ip.ip4))
502 fproto = FIB_PROTOCOL_IP4;
503 else if (unformat (line_input, "%U %U",
504 unformat_ip6_address,
505 &src_ip.ip6, unformat_ip6_address, &dst_ip.ip6))
506 fproto = FIB_PROTOCOL_IP6;
507 else if (unformat (line_input, "%d %d", &src_port, &dst_port))
509 else if (unformat (line_input, "%d", &dst_port))
511 else if (unformat (line_input, "table-id %d", &table_id))
513 else if (unformat (line_input, "src-port-is-entropy"))
514 flags |= UDP_ENCAP_FIXUP_UDP_SRC_PORT_ENTROPY;
517 error = unformat_parse_error (line_input);
525 clib_error_return (0, "An ID for the UDP encap instance is required");
529 if (!is_del && fproto != FIB_PROTOCOL_MAX)
534 fib_index = fib_table_find (fproto, table_id);
538 error = clib_error_return (0, "Nonexistent table id %d", table_id);
542 uei = udp_encap_add_and_lock (ue_id, fproto, fib_index,
544 src_port, dst_port, flags);
546 if (INDEX_INVALID == uei)
549 clib_error_return (0, "update to existing encap not supported %d",
556 udp_encap_unlock (ue_id);
561 clib_error_return (0,
562 "Some IP addresses would be usefull, don't you think?",
567 unformat_free (line_input);
572 udp_encap_show (vlib_main_t * vm,
573 unformat_input_t * input, vlib_cli_command_t * cmd)
579 /* Get a line of input. */
580 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
582 if (unformat (input, "%d", &ue_id))
585 return clib_error_return (0, "unknown input `%U'",
586 format_unformat_error, input);
594 pool_foreach(ue, udp_encap_pool,
596 vlib_cli_output(vm, "%U", format_udp_encap, ue->ue_id, 0);
602 vlib_cli_output (vm, "%U", format_udp_encap, ue_id, 1);
609 VLIB_CLI_COMMAND (udp_encap_add_command, static) = {
611 .short_help = "udp encap [add|del] <id ID> <src-ip> <dst-ip> [<src-port>] <dst-port> [src-port-is-entropy] [table-id <table>]",
612 .function = udp_encap_cli,
615 VLIB_CLI_COMMAND (udp_encap_show_command, static) = {
616 .path = "show udp encap",
617 .short_help = "show udp encap [ID]",
618 .function = udp_encap_show,
624 * fd.io coding-style-patch-verification: ON
627 * eval: (c-set-style "gnu")