2 * Copyright (c) 2015 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 * ip/ip_lookup.c: ip4/6 adjacency and lookup table management
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/ip/ip.h>
41 #include <vnet/adj/adj.h>
42 #include <vnet/fib/fib_table.h>
43 #include <vnet/fib/ip4_fib.h>
44 #include <vnet/fib/ip6_fib.h>
45 #include <vnet/mpls/mpls.h>
46 #include <vnet/mfib/mfib_table.h>
47 #include <vnet/dpo/drop_dpo.h>
48 #include <vnet/dpo/classify_dpo.h>
49 #include <vnet/dpo/punt_dpo.h>
50 #include <vnet/dpo/receive_dpo.h>
51 #include <vnet/dpo/ip_null_dpo.h>
52 #include <vnet/dpo/l3_proxy_dpo.h>
56 * @brief IPv4 and IPv6 adjacency and lookup table management.
61 ip_interface_address_add_del (ip_lookup_main_t * lm,
65 u32 is_del, u32 * result_if_address_index)
67 vnet_main_t *vnm = vnet_get_main ();
68 ip_interface_address_t *a, *prev, *next;
69 uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
71 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
73 a = p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
75 /* Verify given length. */
76 if ((a && (address_length != a->address_length)) ||
77 (address_length == 0) ||
78 (lm->is_ip6 && address_length > 128) ||
79 (!lm->is_ip6 && address_length > 32))
81 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
82 return clib_error_create
83 ("%U wrong length (expected %d) for interface %U",
84 lm->format_address_and_length, addr_fib,
85 address_length, a ? a->address_length : -1,
86 format_vnet_sw_if_index_name, vnm, sw_if_index);
93 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
94 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
95 return clib_error_create ("%U not found for interface %U",
96 lm->format_address_and_length,
97 addr_fib, address_length,
98 format_vnet_sw_interface_name, vnm, si);
101 if (a->prev_this_sw_interface != ~0)
104 pool_elt_at_index (lm->if_address_pool,
105 a->prev_this_sw_interface);
106 prev->next_this_sw_interface = a->next_this_sw_interface;
108 if (a->next_this_sw_interface != ~0)
111 pool_elt_at_index (lm->if_address_pool,
112 a->next_this_sw_interface);
113 next->prev_this_sw_interface = a->prev_this_sw_interface;
115 if (a->prev_this_sw_interface == ~0)
116 lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
117 a->next_this_sw_interface;
120 if ((a->next_this_sw_interface == ~0)
121 && (a->prev_this_sw_interface == ~0))
122 lm->if_address_pool_index_by_sw_if_index[sw_if_index] = ~0;
124 mhash_unset (&lm->address_to_if_address_index, addr_fib,
126 pool_put (lm->if_address_pool, a);
128 if (result_if_address_index)
129 *result_if_address_index = ~0;
134 u32 pi; /* previous index */
136 u32 hi; /* head index */
138 pool_get (lm->if_address_pool, a);
139 clib_memset (a, ~0, sizeof (a[0]));
140 ai = a - lm->if_address_pool;
142 hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
144 while (pi != (u32) ~ 0)
146 prev = pool_elt_at_index (lm->if_address_pool, pi);
147 pi = prev->next_this_sw_interface;
149 pi = prev ? prev - lm->if_address_pool : (u32) ~ 0;
151 a->address_key = mhash_set (&lm->address_to_if_address_index,
152 addr_fib, ai, /* old_value */ 0);
153 a->address_length = address_length;
154 a->sw_if_index = sw_if_index;
156 a->prev_this_sw_interface = pi;
157 a->next_this_sw_interface = ~0;
159 prev->next_this_sw_interface = ai;
161 lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
162 (hi != ~0) ? hi : ai;
163 if (result_if_address_index)
164 *result_if_address_index = ai;
168 if (sw_if_index != a->sw_if_index)
170 if (result_if_address_index)
171 *result_if_address_index = ~0;
172 vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS;
173 return clib_error_create
174 ("Prefix %U already found on interface %U",
175 lm->format_address_and_length, addr_fib, address_length,
176 format_vnet_sw_if_index_name, vnm, a->sw_if_index);
179 if (result_if_address_index)
180 *result_if_address_index = a - lm->if_address_pool;
183 return /* no error */ 0;
186 static clib_error_t *
187 ip_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
189 vec_validate_init_empty (ip4_main.
190 lookup_main.if_address_pool_index_by_sw_if_index,
192 vec_validate_init_empty (ip6_main.
193 lookup_main.if_address_pool_index_by_sw_if_index,
199 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_sw_interface_add_del);
202 ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
204 if (!lm->fib_result_n_bytes)
205 lm->fib_result_n_bytes = sizeof (uword);
208 mhash_init (&lm->prefix_to_if_prefix_index, sizeof (uword),
209 sizeof (ip_interface_prefix_key_t));
212 lm->format_address_and_length = format_ip6_address_and_length;
213 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
214 sizeof (ip6_address_fib_t));
218 lm->format_address_and_length = format_ip4_address_and_length;
219 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
220 sizeof (ip4_address_fib_t));
226 /* Setup all IP protocols to be punted and builtin-unknown. */
227 for (i = 0; i < 256; i++)
229 lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
230 lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
233 lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
234 lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 :
235 IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
236 lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] =
237 IP_BUILTIN_PROTOCOL_UDP;
238 lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 :
240 IP_BUILTIN_PROTOCOL_ICMP;
245 format_ip_flow_hash_config (u8 * s, va_list * args)
247 flow_hash_config_t flow_hash_config = va_arg (*args, u32);
249 #define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
250 foreach_flow_hash_bit;
257 format_ip_adjacency_packet_data (u8 * s, va_list * args)
259 u8 *packet_data = va_arg (*args, u8 *);
260 u32 n_packet_data_bytes = va_arg (*args, u32);
262 s = format (s, "%U", format_hex_bytes, packet_data, n_packet_data_bytes);
268 unformat_dpo (unformat_input_t * input, va_list * args)
270 dpo_id_t *dpo = va_arg (*args, dpo_id_t *);
271 fib_protocol_t fp = va_arg (*args, int);
274 proto = fib_proto_to_dpo (fp);
276 if (unformat (input, "drop"))
277 dpo_copy (dpo, drop_dpo_get (proto));
278 else if (unformat (input, "punt"))
279 dpo_copy (dpo, punt_dpo_get (proto));
280 else if (unformat (input, "local"))
281 receive_dpo_add_or_lock (proto, ~0, NULL, dpo);
282 else if (unformat (input, "null-send-unreach"))
283 ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_UNREACH, dpo);
284 else if (unformat (input, "null-send-prohibit"))
285 ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_PROHIBIT, dpo);
286 else if (unformat (input, "null"))
287 ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_NONE, dpo);
288 else if (unformat (input, "classify"))
290 u32 classify_table_index;
292 if (!unformat (input, "%d", &classify_table_index))
294 clib_warning ("classify adj must specify table index");
298 dpo_set (dpo, DPO_CLASSIFY, proto,
299 classify_dpo_create (proto, classify_table_index));
307 const ip46_address_t zero_addr = {
312 static clib_error_t *
313 vnet_ip_route_cmd (vlib_main_t * vm,
314 unformat_input_t * main_input, vlib_cli_command_t * cmd)
316 unformat_input_t _line_input, *line_input = &_line_input;
317 u32 table_id, is_del, fib_index, payload_proto;
318 dpo_id_t dpo = DPO_INVALID, *dpos = NULL;
319 fib_route_path_t *rpaths = NULL, rpath;
320 fib_prefix_t *prefixs = NULL, pfx;
321 clib_error_t *error = NULL;
328 clib_memset (&pfx, 0, sizeof (pfx));
330 /* Get a line of input. */
331 if (!unformat_user (main_input, unformat_line_input, line_input))
334 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
336 clib_memset (&rpath, 0, sizeof (rpath));
338 if (unformat (line_input, "table %d", &table_id))
340 else if (unformat (line_input, "count %f", &count))
343 else if (unformat (line_input, "%U/%d",
344 unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
346 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP4;
347 vec_add1 (prefixs, pfx);
349 else if (unformat (line_input, "%U/%d",
350 unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len))
352 payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP6;
353 vec_add1 (prefixs, pfx);
355 else if (unformat (line_input, "via %U",
356 unformat_fib_route_path, &rpath, &payload_proto))
358 vec_add1 (rpaths, rpath);
360 else if (vec_len (prefixs) > 0 &&
361 unformat (line_input, "via %U",
362 unformat_dpo, &dpo, prefixs[0].fp_proto))
364 vec_add1 (dpos, dpo);
366 else if (unformat (line_input, "del"))
368 else if (unformat (line_input, "add"))
372 error = unformat_parse_error (line_input);
377 if (vec_len (prefixs) == 0)
380 clib_error_return (0, "expected ip4/ip6 destination address/length.");
384 if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0)
386 error = clib_error_return (0, "expected paths.");
393 * if no table_id is passed we will manipulate the default
399 fib_index = fib_table_find (prefixs[0].fp_proto, table_id);
403 error = clib_error_return (0, "Nonexistent table id %d", table_id);
408 for (i = 0; i < vec_len (prefixs); i++)
410 if (is_del && 0 == vec_len (rpaths))
412 fib_table_entry_delete (fib_index, &prefixs[i], FIB_SOURCE_CLI);
414 else if (!is_del && 1 == vec_len (dpos))
416 fib_table_entry_special_dpo_add (fib_index,
419 FIB_ENTRY_FLAG_EXCLUSIVE,
421 dpo_reset (&dpos[0]);
423 else if (vec_len (dpos) > 0)
426 clib_error_return (0,
427 "Load-balancing over multiple special adjacencies is unsupported");
430 else if (0 < vec_len (rpaths))
433 ip46_address_t dst = prefixs[i].fp_addr;
436 t[0] = vlib_time_now (vm);
437 incr = 1 << ((FIB_PROTOCOL_IP4 == prefixs[0].fp_proto ? 32 : 128) -
440 for (k = 0; k < n; k++)
442 fib_prefix_t rpfx = {
443 .fp_len = prefixs[i].fp_len,
444 .fp_proto = prefixs[i].fp_proto,
449 fib_table_entry_path_remove2 (fib_index,
450 &rpfx, FIB_SOURCE_CLI, rpaths);
452 fib_table_entry_path_add2 (fib_index,
455 FIB_ENTRY_FLAG_NONE, rpaths);
457 if (FIB_PROTOCOL_IP4 == prefixs[0].fp_proto)
460 clib_host_to_net_u32 (incr +
461 clib_net_to_host_u32 (dst.
466 int bucket = (incr < 64 ? 0 : 1);
467 dst.ip6.as_u64[bucket] =
468 clib_host_to_net_u64 (incr +
469 clib_net_to_host_u64 (dst.ip6.as_u64
474 t[1] = vlib_time_now (vm);
476 vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
480 error = clib_error_return (0, "Don't understand what you want...");
489 unformat_free (line_input);
494 vnet_ip_table_cmd (vlib_main_t * vm,
495 unformat_input_t * main_input,
496 vlib_cli_command_t * cmd, fib_protocol_t fproto)
498 unformat_input_t _line_input, *line_input = &_line_input;
499 clib_error_t *error = NULL;
500 u32 table_id, is_add;
506 /* Get a line of input. */
507 if (!unformat_user (main_input, unformat_line_input, line_input))
510 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
512 if (unformat (line_input, "%d", &table_id))
514 else if (unformat (line_input, "del"))
516 else if (unformat (line_input, "add"))
518 else if (unformat (line_input, "name %s", &name))
522 error = unformat_parse_error (line_input);
529 error = clib_error_return (0, "No table id");
532 else if (0 == table_id)
534 error = clib_error_return (0, "Can't change the default table");
541 ip_table_create (fproto, table_id, 0, name);
545 ip_table_delete (fproto, table_id, 0);
550 unformat_free (line_input);
555 vnet_ip4_table_cmd (vlib_main_t * vm,
556 unformat_input_t * main_input, vlib_cli_command_t * cmd)
558 return (vnet_ip_table_cmd (vm, main_input, cmd, FIB_PROTOCOL_IP4));
562 vnet_ip6_table_cmd (vlib_main_t * vm,
563 unformat_input_t * main_input, vlib_cli_command_t * cmd)
565 return (vnet_ip_table_cmd (vm, main_input, cmd, FIB_PROTOCOL_IP6));
569 VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
571 .short_help = "Internet protocol (IP) commands",
576 VLIB_CLI_COMMAND (vlib_cli_ip6_command, static) = {
578 .short_help = "Internet protocol version 6 (IPv6) commands",
583 VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
585 .short_help = "Internet protocol (IP) show commands",
590 VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
592 .short_help = "Internet protocol version 6 (IPv6) show commands",
597 * This command is used to add or delete IPv4 or IPv6 routes. All
598 * IP Addresses ('<em><dst-ip-addr>/<width></em>',
599 * '<em><next-hop-ip-addr></em>' and '<em><adj-hop-ip-addr></em>')
600 * can be IPv4 or IPv6, but all must be of the same form in a single
601 * command. To display the current set of routes, use the commands
602 * '<em>show ip fib</em>' and '<em>show ip6 fib</em>'.
605 * Example of how to add a straight forward static route:
606 * @cliexcmd{ip route add 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0}
607 * Example of how to delete a straight forward static route:
608 * @cliexcmd{ip route del 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0}
609 * Mainly for route add/del performance testing, one can add or delete
610 * multiple routes by adding 'count N' to the previous item:
611 * @cliexcmd{ip route add count 10 7.0.0.0/24 via 6.0.0.1 GigabitEthernet2/0/0}
612 * Add multiple routes for the same destination to create equal-cost multipath:
613 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0}
614 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0}
615 * For unequal-cost multipath, specify the desired weights. This
616 * combination of weights results in 3/4 of the traffic following the
617 * second path, 1/4 following the first path:
618 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0 weight 1}
619 * @cliexcmd{ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0 weight 3}
620 * To add a route to a particular FIB table (VRF), use:
621 * @cliexcmd{ip route add 172.16.24.0/24 table 7 via GigabitEthernet2/0/0}
624 VLIB_CLI_COMMAND (ip_route_command, static) = {
626 .short_help = "ip route [add|del] [count <n>] <dst-ip-addr>/<width> [table <table-id>] via [next-hop-address] [next-hop-interface] [next-hop-table <value>] [weight <value>] [preference <value>] [udp-encap-id <value>] [ip4-lookup-in-table <value>] [ip6-lookup-in-table <value>] [mpls-lookup-in-table <value>] [resolve-via-host] [resolve-via-connected] [rx-ip4 <interface>] [out-labels <value value value>]",
627 .function = vnet_ip_route_cmd,
633 * This command is used to add or delete IPv4 Tables. All
634 * Tables must be explicitly added before that can be used. Creating a
635 * table will add both unicast and multicast FIBs
639 VLIB_CLI_COMMAND (ip4_table_command, static) = {
641 .short_help = "ip table [add|del] <table-id>",
642 .function = vnet_ip4_table_cmd,
648 * This command is used to add or delete IPv4 Tables. All
649 * Tables must be explicitly added before that can be used. Creating a
650 * table will add both unicast and multicast FIBs
654 VLIB_CLI_COMMAND (ip6_table_command, static) = {
656 .short_help = "ip6 table [add|del] <table-id>",
657 .function = vnet_ip6_table_cmd,
660 static clib_error_t *
661 ip_table_bind_cmd (vlib_main_t * vm,
662 unformat_input_t * input,
663 vlib_cli_command_t * cmd,
664 fib_protocol_t fproto)
666 vnet_main_t *vnm = vnet_get_main ();
667 clib_error_t *error = 0;
668 u32 sw_if_index, table_id;
673 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
675 error = clib_error_return (0, "unknown interface `%U'",
676 format_unformat_error, input);
680 if (unformat (input, "%d", &table_id))
684 error = clib_error_return (0, "expected table id `%U'",
685 format_unformat_error, input);
689 rv = ip_table_bind (fproto, sw_if_index, table_id, 0);
691 if (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE == rv)
693 error = clib_error_return (0, "IP addresses are still present on %U",
694 format_vnet_sw_if_index_name,
698 else if (VNET_API_ERROR_NO_SUCH_FIB == rv)
700 error = clib_error_return (0, "no such table %d", table_id);
704 error = clib_error_return (0, "unknown error");
711 static clib_error_t *
712 ip4_table_bind_cmd (vlib_main_t * vm,
713 unformat_input_t * input,
714 vlib_cli_command_t * cmd)
716 return (ip_table_bind_cmd (vm , input, cmd, FIB_PROTOCOL_IP4));
719 static clib_error_t *
720 ip6_table_bind_cmd (vlib_main_t * vm,
721 unformat_input_t * input,
722 vlib_cli_command_t * cmd)
724 return (ip_table_bind_cmd (vm , input, cmd, FIB_PROTOCOL_IP6));
728 * Place the indicated interface into the supplied IPv4 FIB table (also known
729 * as a VRF). The FIB table must be created using "ip table add" already. To
730 * display the current IPv4 FIB table, use the command '<em>show ip fib</em>'.
731 * FIB table will only be displayed if a route has been added to the table, or
732 * an IP Address is assigned to an interface in the table (which adds a route
735 * @note IP addresses added after setting the interface IP table are added to
736 * the indicated FIB table. If an IP address is added prior to changing the
737 * table then this is an error. The control plane must remove these addresses
738 * first and then change the table. VPP will not automatically move the
739 * addresses from the old to the new table as it does not know the validity
743 * Example of how to add an interface to an IPv4 FIB table (where 2 is the table-id):
744 * @cliexcmd{set interface ip table GigabitEthernet2/0/0 2}
747 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) =
749 .path = "set interface ip table",
750 .function = ip4_table_bind_cmd,
751 .short_help = "set interface ip table <interface> <table-id>",
756 * Place the indicated interface into the supplied IPv6 FIB table (also known
757 * as a VRF). The FIB table must be created using "ip6 table add" already. To
758 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
759 * FIB table will only be displayed if a route has been added to the table, or
760 * an IP Address is assigned to an interface in the table (which adds a route
763 * @note IP addresses added after setting the interface IP table are added to
764 * the indicated FIB table. If an IP address is added prior to changing the
765 * table then this is an error. The control plane must remove these addresses
766 * first and then change the table. VPP will not automatically move the
767 * addresses from the old to the new table as it does not know the validity
771 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
772 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
775 VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
777 .path = "set interface ip6 table",
778 .function = ip6_table_bind_cmd,
779 .short_help = "set interface ip6 table <interface> <table-id>"
784 vnet_ip_mroute_cmd (vlib_main_t * vm,
785 unformat_input_t * main_input, vlib_cli_command_t * cmd)
787 unformat_input_t _line_input, *line_input = &_line_input;
788 fib_route_path_t rpath, *rpaths = NULL;
789 clib_error_t *error = NULL;
790 u32 table_id, is_del, payload_proto;
793 mfib_entry_flags_t eflags = 0;
794 u32 gcount, scount, ss, gg, incr;
796 u32 rpf_id = MFIB_RPF_ID_NONE;
801 clib_memset (&pfx, 0, sizeof (pfx));
802 clib_memset (&rpath, 0, sizeof (rpath));
803 rpath.frp_sw_if_index = ~0;
805 /* Get a line of input. */
806 if (!unformat_user (main_input, unformat_line_input, line_input))
809 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
811 if (unformat (line_input, "table %d", &table_id))
813 else if (unformat (line_input, "del"))
815 else if (unformat (line_input, "add"))
817 else if (unformat (line_input, "rpf-id %d", &rpf_id))
819 else if (unformat (line_input, "scount %d", &scount))
821 else if (unformat (line_input, "gcount %d", &gcount))
823 else if (unformat (line_input, "%U %U",
824 unformat_ip4_address,
825 &pfx.fp_src_addr.ip4,
826 unformat_ip4_address, &pfx.fp_grp_addr.ip4))
828 pfx.fp_proto = FIB_PROTOCOL_IP4;
831 else if (unformat (line_input, "%U %U",
832 unformat_ip6_address,
833 &pfx.fp_src_addr.ip6,
834 unformat_ip6_address, &pfx.fp_grp_addr.ip6))
836 pfx.fp_proto = FIB_PROTOCOL_IP6;
839 else if (unformat (line_input, "%U/%d",
840 unformat_ip4_address,
841 &pfx.fp_grp_addr.ip4, &pfx.fp_len))
843 clib_memset (&pfx.fp_src_addr.ip4, 0, sizeof (pfx.fp_src_addr.ip4));
844 pfx.fp_proto = FIB_PROTOCOL_IP4;
846 else if (unformat (line_input, "%U/%d",
847 unformat_ip6_address,
848 &pfx.fp_grp_addr.ip6, &pfx.fp_len))
850 clib_memset (&pfx.fp_src_addr.ip6, 0, sizeof (pfx.fp_src_addr.ip6));
851 pfx.fp_proto = FIB_PROTOCOL_IP6;
853 else if (unformat (line_input, "%U",
854 unformat_ip4_address, &pfx.fp_grp_addr.ip4))
856 clib_memset (&pfx.fp_src_addr.ip4, 0, sizeof (pfx.fp_src_addr.ip4));
857 pfx.fp_proto = FIB_PROTOCOL_IP4;
860 else if (unformat (line_input, "%U",
861 unformat_ip6_address, &pfx.fp_grp_addr.ip6))
863 clib_memset (&pfx.fp_src_addr.ip6, 0, sizeof (pfx.fp_src_addr.ip6));
864 pfx.fp_proto = FIB_PROTOCOL_IP6;
867 else if (unformat (line_input, "via local Forward"))
869 clib_memset (&rpath.frp_addr, 0, sizeof (rpath.frp_addr));
870 rpath.frp_sw_if_index = ~0;
871 rpath.frp_weight = 1;
872 rpath.frp_flags |= FIB_ROUTE_PATH_LOCAL;
874 * set the path proto appropriately for the prefix
876 rpath.frp_proto = fib_proto_to_dpo (pfx.fp_proto);
877 rpath.frp_mitf_flags = MFIB_ITF_FLAG_FORWARD;
879 vec_add1 (rpaths, rpath);
881 else if (unformat (line_input, "via %U",
882 unformat_fib_route_path, &rpath, &payload_proto))
884 vec_add1 (rpaths, rpath);
886 else if (unformat (line_input, "%U",
887 unformat_mfib_entry_flags, &eflags))
891 error = unformat_parse_error (line_input);
899 * if no table_id is passed we will manipulate the default
905 fib_index = mfib_table_find (pfx.fp_proto, table_id);
909 error = clib_error_return (0, "Nonexistent table id %d", table_id);
914 timet[0] = vlib_time_now (vm);
916 if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
918 incr = 1 << (32 - (pfx.fp_len % 32));
922 incr = 1 << (128 - (pfx.fp_len % 128));
925 for (ss = 0; ss < scount; ss++)
927 for (gg = 0; gg < gcount; gg++)
929 if (is_del && 0 == vec_len (rpaths))
931 /* no path provided => route delete */
932 mfib_table_entry_delete (fib_index, &pfx, MFIB_SOURCE_CLI);
934 else if (eflags || (MFIB_RPF_ID_NONE != rpf_id))
936 mfib_table_entry_update (fib_index, &pfx, MFIB_SOURCE_CLI,
942 mfib_table_entry_path_remove (fib_index,
943 &pfx, MFIB_SOURCE_CLI, rpaths);
945 mfib_table_entry_path_update (fib_index,
946 &pfx, MFIB_SOURCE_CLI, rpaths);
949 if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
951 pfx.fp_grp_addr.ip4.as_u32 =
952 clib_host_to_net_u32 (incr +
953 clib_net_to_host_u32 (pfx.
959 int bucket = (incr < 64 ? 0 : 1);
960 pfx.fp_grp_addr.ip6.as_u64[bucket] =
961 clib_host_to_net_u64 (incr +
962 clib_net_to_host_u64 (pfx.
963 fp_grp_addr.ip6.as_u64
968 if (FIB_PROTOCOL_IP4 == pfx.fp_proto)
970 pfx.fp_src_addr.ip4.as_u32 =
971 clib_host_to_net_u32 (1 +
972 clib_net_to_host_u32 (pfx.fp_src_addr.
977 pfx.fp_src_addr.ip6.as_u64[1] =
978 clib_host_to_net_u64 (1 +
979 clib_net_to_host_u64 (pfx.fp_src_addr.
984 timet[1] = vlib_time_now (vm);
986 if (scount > 1 || gcount > 1)
987 vlib_cli_output (vm, "%.6e routes/sec",
988 (scount * gcount) / (timet[1] - timet[0]));
992 unformat_free (line_input);
998 * This command is used to add or delete IPv4 or IPv6 multicast routes. All
999 * IP Addresses ('<em><dst-ip-addr>/<width></em>',
1000 * '<em><next-hop-ip-addr></em>' and '<em><adj-hop-ip-addr></em>')
1001 * can be IPv4 or IPv6, but all must be of the same form in a single
1002 * command. To display the current set of routes, use the commands
1003 * '<em>show ip mfib</em>' and '<em>show ip6 mfib</em>'.
1004 * The full set of support flags for interfaces and route is shown via;
1005 * '<em>show mfib route flags</em>' and '<em>show mfib itf flags</em>'
1008 * Example of how to add a forwarding interface to a route (and create the
1009 * route if it does not exist)
1010 * @cliexcmd{ip mroute add 232.1.1.1 via GigabitEthernet2/0/0 Forward}
1011 * Example of how to add an accepting interface to a route (and create the
1012 * route if it does not exist)
1013 * @cliexcmd{ip mroute add 232.1.1.1 via GigabitEthernet2/0/1 Accept}
1014 * Example of changing the route's flags to send signals via the API
1015 * @cliexcmd{ip mroute add 232.1.1.1 Signal}
1019 VLIB_CLI_COMMAND (ip_mroute_command, static) =
1021 .path = "ip mroute",
1022 .short_help = "ip mroute [add|del] <dst-ip-addr>/<width> [table <table-id>] [rpf-id <ID>] [via <next-hop-ip-addr> [<interface>],",
1023 .function = vnet_ip_mroute_cmd,
1029 vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t * args)
1033 if (!vnet_sw_interface_is_api_valid (vnet_get_main (), args->sw_if_index))
1034 return clib_error_return_code (0, VNET_API_ERROR_INVALID_INTERFACE, 0,
1035 "invalid sw_if_index");
1037 fib_index = fib_table_get_table_id_for_sw_if_index (args->prefix.fp_proto,
1041 dpo_id_t proxy_dpo = DPO_INVALID;
1042 l3_proxy_dpo_add_or_lock (fib_proto_to_dpo (args->prefix.fp_proto),
1043 args->sw_if_index, &proxy_dpo);
1044 fib_table_entry_special_dpo_add (fib_index,
1047 FIB_ENTRY_FLAG_EXCLUSIVE, &proxy_dpo);
1048 dpo_reset (&proxy_dpo);
1052 fib_table_entry_special_remove (fib_index, &args->prefix,
1059 ip_container_proxy_is_set (fib_prefix_t * pfx, u32 sw_if_index)
1062 fib_node_index_t fei;
1063 const dpo_id_t *dpo;
1064 l3_proxy_dpo_t *l3p;
1065 load_balance_t *lb0;
1067 fib_index = fib_table_get_table_id_for_sw_if_index (pfx->fp_proto,
1069 if (fib_index == ~0)
1072 fei = fib_table_lookup_exact_match (fib_index, pfx);
1073 if (fei == FIB_NODE_INDEX_INVALID)
1076 dpo = fib_entry_contribute_ip_forwarding (fei);
1077 lb0 = load_balance_get (dpo->dpoi_index);
1078 dpo = load_balance_get_bucket_i (lb0, 0);
1079 if (dpo->dpoi_type != DPO_L3_PROXY)
1082 l3p = l3_proxy_dpo_get (dpo->dpoi_index);
1083 return (l3p->l3p_sw_if_index == sw_if_index);
1086 typedef struct ip_container_proxy_walk_ctx_t_
1088 ip_container_proxy_cb_t cb;
1090 } ip_container_proxy_walk_ctx_t;
1092 static fib_table_walk_rc_t
1093 ip_container_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1095 ip_container_proxy_walk_ctx_t *ctx = arg;
1096 const fib_prefix_t *pfx;
1097 const dpo_id_t *dpo;
1099 l3_proxy_dpo_t *l3p;
1101 pfx = fib_entry_get_prefix (fei);
1102 if (fib_entry_is_sourced (fei, FIB_SOURCE_PROXY))
1104 dpo = fib_entry_contribute_ip_forwarding (fei);
1105 lb = load_balance_get (dpo->dpoi_index);
1106 dpo = load_balance_get_bucket_i (lb, 0);
1107 l3p = l3_proxy_dpo_get (dpo->dpoi_index);
1108 ctx->cb (pfx, l3p->l3p_sw_if_index, ctx->ctx);
1111 return FIB_TABLE_WALK_CONTINUE;
1115 ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx)
1117 fib_table_t *fib_table;
1118 ip_container_proxy_walk_ctx_t wctx = {
1124 pool_foreach (fib_table, ip4_main.fibs,
1126 fib_table_walk(fib_table->ft_index,
1128 ip_container_proxy_fib_table_walk,
1131 pool_foreach (fib_table, ip6_main.fibs,
1133 fib_table_walk(fib_table->ft_index,
1135 ip_container_proxy_fib_table_walk,
1142 ip_container_cmd (vlib_main_t * vm,
1143 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1145 unformat_input_t _line_input, *line_input = &_line_input;
1147 u32 is_del, addr_set = 0;
1151 vnm = vnet_get_main ();
1154 clib_memset (&pfx, 0, sizeof (pfx));
1156 /* Get a line of input. */
1157 if (!unformat_user (main_input, unformat_line_input, line_input))
1160 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1162 if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1164 pfx.fp_proto = FIB_PROTOCOL_IP4;
1168 else if (unformat (line_input, "%U",
1169 unformat_ip6_address, &pfx.fp_addr.ip6))
1171 pfx.fp_proto = FIB_PROTOCOL_IP6;
1175 else if (unformat (line_input, "%U",
1176 unformat_vnet_sw_interface, vnm, &sw_if_index))
1178 else if (unformat (line_input, "del"))
1182 unformat_free (line_input);
1183 return (clib_error_return (0, "unknown input '%U'",
1184 format_unformat_error, line_input));
1188 if (~0 == sw_if_index || !addr_set)
1190 unformat_free (line_input);
1191 vlib_cli_output (vm, "interface and address must be set");
1195 vnet_ip_container_proxy_args_t args = {
1197 .sw_if_index = sw_if_index,
1200 vnet_ip_container_proxy_add_del (&args);
1201 unformat_free (line_input);
1206 VLIB_CLI_COMMAND (ip_container_command_node, static) = {
1207 .path = "ip container",
1208 .function = ip_container_cmd,
1209 .short_help = "ip container <address> <interface>",
1215 show_ip_container_cmd_fn (vlib_main_t * vm, unformat_input_t * main_input,
1216 vlib_cli_command_t * cmd)
1218 unformat_input_t _line_input, *line_input = &_line_input;
1219 vnet_main_t *vnm = vnet_get_main ();
1221 u32 sw_if_index = ~0;
1224 if (!unformat_user (main_input, unformat_line_input, line_input))
1226 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1228 if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1230 pfx.fp_proto = FIB_PROTOCOL_IP4;
1233 else if (unformat (line_input, "%U",
1234 unformat_ip6_address, &pfx.fp_addr.ip6))
1236 pfx.fp_proto = FIB_PROTOCOL_IP6;
1239 else if (unformat (line_input, "%U",
1240 unformat_vnet_sw_interface, vnm, &sw_if_index))
1244 unformat_free (line_input);
1245 return (clib_error_return (0, "unknown input '%U'",
1246 format_unformat_error, line_input));
1250 if (~0 == sw_if_index)
1252 unformat_free (line_input);
1253 vlib_cli_output (vm, "no interface");
1254 return (clib_error_return (0, "no interface"));
1257 has_proxy = ip_container_proxy_is_set (&pfx, sw_if_index);
1258 vlib_cli_output (vm, "ip container proxy is: %s", has_proxy ? "on" : "off");
1260 unformat_free (line_input);
1265 VLIB_CLI_COMMAND (show_ip_container_command, static) = {
1266 .path = "show ip container",
1267 .function = show_ip_container_cmd_fn,
1268 .short_help = "show ip container <address> <interface>",
1274 * fd.io coding-style-patch-verification: ON
1277 * eval: (c-set-style "gnu")