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/ip4_forward.c: IP v4 forwarding
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/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
43 #include <vnet/ethernet/arp_packet.h> /* for ethernet_arp_header_t */
44 #include <vnet/ppp/ppp.h>
45 #include <vnet/srp/srp.h> /* for srp_hw_interface_class */
46 #include <vnet/api_errno.h> /* for API error numbers */
48 /* This is really, really simple but stupid fib. */
50 ip4_fib_lookup_with_table (ip4_main_t * im, u32 fib_index,
52 u32 disable_default_route)
54 ip_lookup_main_t * lm = &im->lookup_main;
55 ip4_fib_t * fib = vec_elt_at_index (im->fibs, fib_index);
56 uword * p, * hash, key;
57 i32 i, i_min, dst_address, ai;
59 i_min = disable_default_route ? 1 : 0;
60 dst_address = clib_mem_unaligned (&dst->data_u32, u32);
61 for (i = ARRAY_LEN (fib->adj_index_by_dst_address) - 1; i >= i_min; i--)
63 hash = fib->adj_index_by_dst_address[i];
67 key = dst_address & im->fib_masks[i];
68 if ((p = hash_get (hash, key)) != 0)
75 /* Nothing matches in table. */
76 ai = lm->miss_adj_index;
83 create_fib_with_table_id (ip4_main_t * im, u32 table_id)
86 hash_set (im->fib_index_by_table_id, table_id, vec_len (im->fibs));
87 vec_add2 (im->fibs, fib, 1);
88 fib->table_id = table_id;
89 fib->index = fib - im->fibs;
90 fib->flow_hash_config = IP_FLOW_HASH_DEFAULT;
91 fib->fwd_classify_table_index = ~0;
92 fib->rev_classify_table_index = ~0;
93 ip4_mtrie_init (&fib->mtrie);
98 find_ip4_fib_by_table_index_or_id (ip4_main_t * im,
99 u32 table_index_or_id, u32 flags)
101 uword * p, fib_index;
103 fib_index = table_index_or_id;
104 if (! (flags & IP4_ROUTE_FLAG_FIB_INDEX))
106 if (table_index_or_id == ~0) {
107 table_index_or_id = 0;
108 while ((p = hash_get (im->fib_index_by_table_id, table_index_or_id))) {
111 return create_fib_with_table_id (im, table_index_or_id);
114 p = hash_get (im->fib_index_by_table_id, table_index_or_id);
116 return create_fib_with_table_id (im, table_index_or_id);
119 return vec_elt_at_index (im->fibs, fib_index);
123 ip4_fib_init_adj_index_by_dst_address (ip_lookup_main_t * lm,
130 ASSERT (lm->fib_result_n_bytes >= sizeof (uword));
131 lm->fib_result_n_words = round_pow2 (lm->fib_result_n_bytes, sizeof (uword)) / sizeof (uword);
133 fib->adj_index_by_dst_address[address_length] =
134 hash_create (32 /* elts */, lm->fib_result_n_words * sizeof (uword));
136 hash_set_flags (fib->adj_index_by_dst_address[address_length],
137 HASH_FLAG_NO_AUTO_SHRINK);
139 h = hash_header (fib->adj_index_by_dst_address[address_length]);
140 max_index = (hash_value_bytes (h) / sizeof (fib->new_hash_values[0])) - 1;
142 /* Initialize new/old hash value vectors. */
143 vec_validate_init_empty (fib->new_hash_values, max_index, ~0);
144 vec_validate_init_empty (fib->old_hash_values, max_index, ~0);
148 ip4_fib_set_adj_index (ip4_main_t * im,
152 u32 dst_address_length,
155 ip_lookup_main_t * lm = &im->lookup_main;
158 if (vec_bytes(fib->old_hash_values))
159 memset (fib->old_hash_values, ~0, vec_bytes (fib->old_hash_values));
160 if (vec_bytes(fib->new_hash_values))
161 memset (fib->new_hash_values, ~0, vec_bytes (fib->new_hash_values));
162 fib->new_hash_values[0] = adj_index;
164 /* Make sure adj index is valid. */
166 (void) ip_get_adjacency (lm, adj_index);
168 hash = fib->adj_index_by_dst_address[dst_address_length];
170 hash = _hash_set3 (hash, dst_address_u32,
171 fib->new_hash_values,
172 fib->old_hash_values);
174 fib->adj_index_by_dst_address[dst_address_length] = hash;
176 if (vec_len (im->add_del_route_callbacks) > 0)
178 ip4_add_del_route_callback_t * cb;
182 d.data_u32 = dst_address_u32;
183 vec_foreach (cb, im->add_del_route_callbacks)
184 if ((flags & cb->required_flags) == cb->required_flags)
185 cb->function (im, cb->function_opaque,
187 &d, dst_address_length,
188 fib->old_hash_values,
189 fib->new_hash_values);
191 p = hash_get (hash, dst_address_u32);
192 clib_memcpy (p, fib->new_hash_values, vec_bytes (fib->new_hash_values));
196 void ip4_add_del_route (ip4_main_t * im, ip4_add_del_route_args_t * a)
198 ip_lookup_main_t * lm = &im->lookup_main;
200 u32 dst_address, dst_address_length, adj_index, old_adj_index;
201 uword * hash, is_del;
202 ip4_add_del_route_callback_t * cb;
204 /* Either create new adjacency or use given one depending on arguments. */
205 if (a->n_add_adj > 0)
207 ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
208 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
211 adj_index = a->adj_index;
213 dst_address = a->dst_address.data_u32;
214 dst_address_length = a->dst_address_length;
215 fib = find_ip4_fib_by_table_index_or_id (im, a->table_index_or_table_id, a->flags);
217 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
218 dst_address &= im->fib_masks[dst_address_length];
220 if (! fib->adj_index_by_dst_address[dst_address_length])
221 ip4_fib_init_adj_index_by_dst_address (lm, fib, dst_address_length);
223 hash = fib->adj_index_by_dst_address[dst_address_length];
225 is_del = (a->flags & IP4_ROUTE_FLAG_DEL) != 0;
229 fib->old_hash_values[0] = ~0;
230 hash = _hash_unset (hash, dst_address, fib->old_hash_values);
231 fib->adj_index_by_dst_address[dst_address_length] = hash;
233 if (vec_len (im->add_del_route_callbacks) > 0
234 && fib->old_hash_values[0] != ~0) /* make sure destination was found in hash */
236 fib->new_hash_values[0] = ~0;
237 vec_foreach (cb, im->add_del_route_callbacks)
238 if ((a->flags & cb->required_flags) == cb->required_flags)
239 cb->function (im, cb->function_opaque,
241 &a->dst_address, dst_address_length,
242 fib->old_hash_values,
243 fib->new_hash_values);
247 ip4_fib_set_adj_index (im, fib, a->flags, dst_address, dst_address_length,
250 old_adj_index = fib->old_hash_values[0];
252 /* Avoid spurious reference count increments */
253 if (old_adj_index == adj_index
255 && !(a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
257 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
258 if (adj->share_count > 0)
262 ip4_fib_mtrie_add_del_route (fib, a->dst_address, dst_address_length,
263 is_del ? old_adj_index : adj_index,
266 /* Delete old adjacency index if present and changed. */
267 if (! (a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
268 && old_adj_index != ~0
269 && old_adj_index != adj_index)
270 ip_del_adjacency (lm, old_adj_index);
274 ip4_add_del_route_next_hop (ip4_main_t * im,
276 ip4_address_t * dst_address,
277 u32 dst_address_length,
278 ip4_address_t * next_hop,
279 u32 next_hop_sw_if_index,
280 u32 next_hop_weight, u32 adj_index,
281 u32 explicit_fib_index)
283 vnet_main_t * vnm = vnet_get_main();
284 ip_lookup_main_t * lm = &im->lookup_main;
287 u32 dst_address_u32, old_mp_adj_index, new_mp_adj_index;
288 u32 dst_adj_index, nh_adj_index;
289 uword * dst_hash, * dst_result;
290 uword * nh_hash, * nh_result;
291 ip_adjacency_t * dst_adj;
292 ip_multipath_adjacency_t * old_mp, * new_mp;
293 int is_del = (flags & IP4_ROUTE_FLAG_DEL) != 0;
294 int is_interface_next_hop;
295 clib_error_t * error = 0;
297 if (explicit_fib_index == (u32)~0)
298 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
300 fib_index = explicit_fib_index;
302 fib = vec_elt_at_index (im->fibs, fib_index);
304 /* Lookup next hop to be added or deleted. */
305 is_interface_next_hop = next_hop->data_u32 == 0;
306 if (adj_index == (u32)~0)
308 if (is_interface_next_hop)
310 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index, next_hop_sw_if_index);
312 nh_adj_index = *nh_result;
315 ip_adjacency_t * adj;
316 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
318 ip4_adjacency_set_interface_route (vnm, adj, next_hop_sw_if_index, /* if_address_index */ ~0);
319 ip_call_add_del_adjacency_callbacks (lm, nh_adj_index, /* is_del */ 0);
320 hash_set (im->interface_route_adj_index_by_sw_if_index, next_hop_sw_if_index, nh_adj_index);
325 nh_hash = fib->adj_index_by_dst_address[32];
326 nh_result = hash_get (nh_hash, next_hop->data_u32);
328 /* Next hop must be known. */
331 ip_adjacency_t * adj;
333 nh_adj_index = ip4_fib_lookup_with_table (im, fib_index,
335 adj = ip_get_adjacency (lm, nh_adj_index);
336 /* if ARP interface adjacencty is present, we need to
337 install ARP adjaceny for specific next hop */
338 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
339 adj->arp.next_hop.ip4.as_u32 == 0)
341 nh_adj_index = vnet_arp_glean_add(fib_index, next_hop);
345 /* Next hop is not known, so create indirect adj */
346 ip_adjacency_t add_adj;
347 add_adj.lookup_next_index = IP_LOOKUP_NEXT_INDIRECT;
348 add_adj.indirect.next_hop.ip4.as_u32 = next_hop->as_u32;
349 add_adj.explicit_fib_index = explicit_fib_index;
350 ip_add_adjacency (lm, &add_adj, 1, &nh_adj_index);
354 nh_adj_index = *nh_result;
359 nh_adj_index = adj_index;
361 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
362 dst_address_u32 = dst_address->data_u32 & im->fib_masks[dst_address_length];
364 dst_hash = fib->adj_index_by_dst_address[dst_address_length];
365 dst_result = hash_get (dst_hash, dst_address_u32);
368 dst_adj_index = dst_result[0];
369 dst_adj = ip_get_adjacency (lm, dst_adj_index);
373 /* For deletes destination must be known. */
376 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
377 error = clib_error_return (0, "unknown destination %U/%d",
378 format_ip4_address, dst_address,
387 /* Ignore adds of X/32 with next hop of X. */
389 && dst_address_length == 32
390 && dst_address->data_u32 == next_hop->data_u32
391 && adj_index != (u32)~0)
393 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
394 error = clib_error_return (0, "prefix matches next hop %U/%d",
395 format_ip4_address, dst_address,
400 /* Destination is not known and default weight is set so add route
401 to existing non-multipath adjacency */
402 if (dst_adj_index == ~0 && next_hop_weight == 1 && next_hop_sw_if_index == ~0)
404 /* create new adjacency */
405 ip4_add_del_route_args_t a;
406 a.table_index_or_table_id = fib_index;
407 a.flags = ((is_del ? IP4_ROUTE_FLAG_DEL : IP4_ROUTE_FLAG_ADD)
408 | IP4_ROUTE_FLAG_FIB_INDEX
409 | IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY
410 | (flags & (IP4_ROUTE_FLAG_NO_REDISTRIBUTE
411 | IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
412 a.dst_address = dst_address[0];
413 a.dst_address_length = dst_address_length;
414 a.adj_index = nh_adj_index;
418 ip4_add_del_route (im, &a);
423 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
425 if (! ip_multipath_adjacency_add_del_next_hop
432 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
433 error = clib_error_return (0, "requested deleting next-hop %U not found in multi-path",
434 format_ip4_address, next_hop);
439 if (old_mp_adj_index != ~0)
440 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
441 if (new_mp_adj_index != ~0)
442 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
444 if (old_mp != new_mp)
446 ip4_add_del_route_args_t a;
447 a.table_index_or_table_id = fib_index;
448 a.flags = ((is_del && ! new_mp ? IP4_ROUTE_FLAG_DEL : IP4_ROUTE_FLAG_ADD)
449 | IP4_ROUTE_FLAG_FIB_INDEX
450 | IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY
451 | (flags & (IP4_ROUTE_FLAG_NO_REDISTRIBUTE | IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
452 a.dst_address = dst_address[0];
453 a.dst_address_length = dst_address_length;
454 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
458 ip4_add_del_route (im, &a);
463 clib_error_report (error);
467 ip4_get_route (ip4_main_t * im,
468 u32 table_index_or_table_id,
473 ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
474 u32 dst_address = * (u32 *) address;
477 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
478 dst_address &= im->fib_masks[address_length];
480 hash = fib->adj_index_by_dst_address[address_length];
481 p = hash_get (hash, dst_address);
486 ip4_foreach_matching_route (ip4_main_t * im,
487 u32 table_index_or_table_id,
489 ip4_address_t * address,
491 ip4_address_t ** results,
492 u8 ** result_lengths)
494 ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
495 u32 dst_address = address->data_u32;
496 u32 this_length = address_length;
499 _vec_len (*results) = 0;
501 _vec_len (*result_lengths) = 0;
503 while (this_length <= 32 && vec_len (results) == 0)
506 hash_foreach (k, v, fib->adj_index_by_dst_address[this_length], ({
507 if (0 == ((k ^ dst_address) & im->fib_masks[address_length]))
511 vec_add1 (*results, a);
512 vec_add1 (*result_lengths, this_length);
520 void ip4_maybe_remap_adjacencies (ip4_main_t * im,
521 u32 table_index_or_table_id,
524 ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
525 ip_lookup_main_t * lm = &im->lookup_main;
528 ip4_add_del_route_callback_t * cb;
529 static ip4_address_t * to_delete;
531 if (lm->n_adjacency_remaps == 0)
534 for (l = 0; l <= 32; l++)
537 uword * hash = fib->adj_index_by_dst_address[l];
539 if (hash_elts (hash) == 0)
543 _vec_len (to_delete) = 0;
545 hash_foreach_pair (p, hash, ({
546 u32 adj_index = p->value[0];
547 u32 m = vec_elt (lm->adjacency_remap_table, adj_index);
551 /* Record destination address from hash key. */
554 /* New adjacency points to nothing: so delete prefix. */
556 vec_add1 (to_delete, a);
559 /* Remap to new adjacency. */
560 clib_memcpy (fib->old_hash_values, p->value, vec_bytes (fib->old_hash_values));
562 /* Set new adjacency value. */
563 fib->new_hash_values[0] = p->value[0] = m - 1;
565 vec_foreach (cb, im->add_del_route_callbacks)
566 if ((flags & cb->required_flags) == cb->required_flags)
567 cb->function (im, cb->function_opaque,
568 fib, flags | IP4_ROUTE_FLAG_ADD,
570 fib->old_hash_values,
571 fib->new_hash_values);
576 fib->new_hash_values[0] = ~0;
577 for (i = 0; i < vec_len (to_delete); i++)
579 hash = _hash_unset (hash, to_delete[i].data_u32, fib->old_hash_values);
580 vec_foreach (cb, im->add_del_route_callbacks)
581 if ((flags & cb->required_flags) == cb->required_flags)
582 cb->function (im, cb->function_opaque,
583 fib, flags | IP4_ROUTE_FLAG_DEL,
585 fib->old_hash_values,
586 fib->new_hash_values);
590 /* Also remap adjacencies in mtrie. */
591 ip4_mtrie_maybe_remap_adjacencies (lm, &fib->mtrie);
593 /* Reset mapping table. */
594 vec_zero (lm->adjacency_remap_table);
596 /* All remaps have been performed. */
597 lm->n_adjacency_remaps = 0;
600 void ip4_delete_matching_routes (ip4_main_t * im,
601 u32 table_index_or_table_id,
603 ip4_address_t * address,
606 static ip4_address_t * matching_addresses;
607 static u8 * matching_address_lengths;
609 ip4_add_del_route_args_t a;
611 a.flags = IP4_ROUTE_FLAG_DEL | IP4_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
612 a.table_index_or_table_id = table_index_or_table_id;
617 for (l = address_length + 1; l <= 32; l++)
619 ip4_foreach_matching_route (im, table_index_or_table_id, flags,
623 &matching_address_lengths);
624 for (i = 0; i < vec_len (matching_addresses); i++)
626 a.dst_address = matching_addresses[i];
627 a.dst_address_length = matching_address_lengths[i];
628 ip4_add_del_route (im, &a);
632 ip4_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
636 ip4_lookup_inline (vlib_main_t * vm,
637 vlib_node_runtime_t * node,
638 vlib_frame_t * frame,
639 int lookup_for_responses_to_locally_received_packets,
642 ip4_main_t * im = &ip4_main;
643 ip_lookup_main_t * lm = &im->lookup_main;
644 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
645 u32 n_left_from, n_left_to_next, * from, * to_next;
646 ip_lookup_next_t next;
647 u32 cpu_index = os_get_cpu_number();
649 from = vlib_frame_vector_args (frame);
650 n_left_from = frame->n_vectors;
651 next = node->cached_next_index;
653 while (n_left_from > 0)
655 vlib_get_next_frame (vm, node, next,
656 to_next, n_left_to_next);
658 while (n_left_from >= 4 && n_left_to_next >= 2)
660 vlib_buffer_t * p0, * p1;
661 ip4_header_t * ip0, * ip1;
662 __attribute__((unused)) tcp_header_t * tcp0, * tcp1;
663 ip_lookup_next_t next0, next1;
664 ip_adjacency_t * adj0, * adj1;
665 ip4_fib_mtrie_t * mtrie0, * mtrie1;
666 ip4_fib_mtrie_leaf_t leaf0, leaf1;
667 ip4_address_t * dst_addr0, *dst_addr1;
668 __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
669 __attribute__((unused)) u32 pi1, fib_index1, adj_index1, is_tcp_udp1;
670 u32 flow_hash_config0, flow_hash_config1;
671 u32 hash_c0, hash_c1;
674 /* Prefetch next iteration. */
676 vlib_buffer_t * p2, * p3;
678 p2 = vlib_get_buffer (vm, from[2]);
679 p3 = vlib_get_buffer (vm, from[3]);
681 vlib_prefetch_buffer_header (p2, LOAD);
682 vlib_prefetch_buffer_header (p3, LOAD);
684 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
685 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
688 pi0 = to_next[0] = from[0];
689 pi1 = to_next[1] = from[1];
691 p0 = vlib_get_buffer (vm, pi0);
692 p1 = vlib_get_buffer (vm, pi1);
694 ip0 = vlib_buffer_get_current (p0);
695 ip1 = vlib_buffer_get_current (p1);
699 ip_adjacency_t * iadj0, * iadj1;
700 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
701 iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
702 dst_addr0 = &iadj0->indirect.next_hop.ip4;
703 dst_addr1 = &iadj1->indirect.next_hop.ip4;
707 dst_addr0 = &ip0->dst_address;
708 dst_addr1 = &ip1->dst_address;
711 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
712 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
713 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
714 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
715 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
716 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
719 if (! lookup_for_responses_to_locally_received_packets)
721 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
722 mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie;
724 leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
726 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
727 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 0);
730 tcp0 = (void *) (ip0 + 1);
731 tcp1 = (void *) (ip1 + 1);
733 is_tcp_udp0 = (ip0->protocol == IP_PROTOCOL_TCP
734 || ip0->protocol == IP_PROTOCOL_UDP);
735 is_tcp_udp1 = (ip1->protocol == IP_PROTOCOL_TCP
736 || ip1->protocol == IP_PROTOCOL_UDP);
738 if (! lookup_for_responses_to_locally_received_packets)
740 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
741 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 1);
744 if (! lookup_for_responses_to_locally_received_packets)
746 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
747 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 2);
750 if (! lookup_for_responses_to_locally_received_packets)
752 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
753 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 3);
756 if (lookup_for_responses_to_locally_received_packets)
758 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
759 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_RX];
763 /* Handle default route. */
764 leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
765 leaf1 = (leaf1 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie1->default_leaf : leaf1);
767 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
768 adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
771 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
773 /* no_default_route */ 0));
774 ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
776 /* no_default_route */ 0));
777 adj0 = ip_get_adjacency (lm, adj_index0);
778 adj1 = ip_get_adjacency (lm, adj_index1);
780 next0 = adj0->lookup_next_index;
781 next1 = adj1->lookup_next_index;
783 /* Use flow hash to compute multipath adjacency. */
784 hash_c0 = vnet_buffer (p0)->ip.flow_hash = 0;
785 hash_c1 = vnet_buffer (p1)->ip.flow_hash = 0;
786 if (PREDICT_FALSE (adj0->n_adj > 1))
789 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
790 hash_c0 = vnet_buffer (p0)->ip.flow_hash =
791 ip4_compute_flow_hash (ip0, flow_hash_config0);
793 if (PREDICT_FALSE(adj1->n_adj > 1))
796 vec_elt_at_index (im->fibs, fib_index1)->flow_hash_config;
797 hash_c1 = vnet_buffer (p1)->ip.flow_hash =
798 ip4_compute_flow_hash (ip1, flow_hash_config1);
801 ASSERT (adj0->n_adj > 0);
802 ASSERT (adj1->n_adj > 0);
803 ASSERT (is_pow2 (adj0->n_adj));
804 ASSERT (is_pow2 (adj1->n_adj));
805 adj_index0 += (hash_c0 & (adj0->n_adj - 1));
806 adj_index1 += (hash_c1 & (adj1->n_adj - 1));
808 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
809 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
811 vlib_increment_combined_counter
812 (cm, cpu_index, adj_index0, 1,
813 vlib_buffer_length_in_chain (vm, p0)
814 + sizeof(ethernet_header_t));
815 vlib_increment_combined_counter
816 (cm, cpu_index, adj_index1, 1,
817 vlib_buffer_length_in_chain (vm, p1)
818 + sizeof(ethernet_header_t));
825 wrong_next = (next0 != next) + 2*(next1 != next);
826 if (PREDICT_FALSE (wrong_next != 0))
835 vlib_set_next_frame_buffer (vm, node, next0, pi0);
842 vlib_set_next_frame_buffer (vm, node, next1, pi1);
849 vlib_set_next_frame_buffer (vm, node, next0, pi0);
850 vlib_set_next_frame_buffer (vm, node, next1, pi1);
854 vlib_put_next_frame (vm, node, next, n_left_to_next);
856 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
862 while (n_left_from > 0 && n_left_to_next > 0)
866 __attribute__((unused)) tcp_header_t * tcp0;
867 ip_lookup_next_t next0;
868 ip_adjacency_t * adj0;
869 ip4_fib_mtrie_t * mtrie0;
870 ip4_fib_mtrie_leaf_t leaf0;
871 ip4_address_t * dst_addr0;
872 __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
873 u32 flow_hash_config0, hash_c0;
878 p0 = vlib_get_buffer (vm, pi0);
880 ip0 = vlib_buffer_get_current (p0);
884 ip_adjacency_t * iadj0;
885 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
886 dst_addr0 = &iadj0->indirect.next_hop.ip4;
890 dst_addr0 = &ip0->dst_address;
893 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
894 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
895 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
897 if (! lookup_for_responses_to_locally_received_packets)
899 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
901 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
903 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
906 tcp0 = (void *) (ip0 + 1);
908 is_tcp_udp0 = (ip0->protocol == IP_PROTOCOL_TCP
909 || ip0->protocol == IP_PROTOCOL_UDP);
911 if (! lookup_for_responses_to_locally_received_packets)
912 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
914 if (! lookup_for_responses_to_locally_received_packets)
915 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
917 if (! lookup_for_responses_to_locally_received_packets)
918 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
920 if (lookup_for_responses_to_locally_received_packets)
921 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
924 /* Handle default route. */
925 leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
926 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
929 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
931 /* no_default_route */ 0));
933 adj0 = ip_get_adjacency (lm, adj_index0);
935 next0 = adj0->lookup_next_index;
937 /* Use flow hash to compute multipath adjacency. */
938 hash_c0 = vnet_buffer (p0)->ip.flow_hash = 0;
939 if (PREDICT_FALSE(adj0->n_adj > 1))
942 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
944 hash_c0 = vnet_buffer (p0)->ip.flow_hash =
945 ip4_compute_flow_hash (ip0, flow_hash_config0);
948 ASSERT (adj0->n_adj > 0);
949 ASSERT (is_pow2 (adj0->n_adj));
950 adj_index0 += (hash_c0 & (adj0->n_adj - 1));
952 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
954 vlib_increment_combined_counter
955 (cm, cpu_index, adj_index0, 1,
956 vlib_buffer_length_in_chain (vm, p0)
957 + sizeof(ethernet_header_t));
964 if (PREDICT_FALSE (next0 != next))
967 vlib_put_next_frame (vm, node, next, n_left_to_next);
969 vlib_get_next_frame (vm, node, next,
970 to_next, n_left_to_next);
977 vlib_put_next_frame (vm, node, next, n_left_to_next);
980 return frame->n_vectors;
984 ip4_lookup (vlib_main_t * vm,
985 vlib_node_runtime_t * node,
986 vlib_frame_t * frame)
988 return ip4_lookup_inline (vm, node, frame,
989 /* lookup_for_responses_to_locally_received_packets */ 0,
990 /* is_indirect */ 0);
994 void ip4_adjacency_set_interface_route (vnet_main_t * vnm,
995 ip_adjacency_t * adj,
997 u32 if_address_index)
999 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1001 vnet_l3_packet_type_t packet_type;
1004 if (hw->hw_class_index == ethernet_hw_interface_class.index
1005 || hw->hw_class_index == srp_hw_interface_class.index)
1008 * We have a bit of a problem in this case. ip4-arp uses
1009 * the rewrite_header.next_index to hand pkts to the
1010 * indicated inteface output node. We can end up in
1011 * ip4_rewrite_local, too, which also pays attention to
1012 * rewrite_header.next index. Net result: a hack in
1013 * ip4_rewrite_local...
1015 n = IP_LOOKUP_NEXT_ARP;
1016 node_index = ip4_arp_node.index;
1017 adj->if_address_index = if_address_index;
1018 adj->arp.next_hop.ip4.as_u32 = 0;
1019 ip46_address_reset(&adj->arp.next_hop);
1020 packet_type = VNET_L3_PACKET_TYPE_ARP;
1024 n = IP_LOOKUP_NEXT_REWRITE;
1025 node_index = ip4_rewrite_node.index;
1026 packet_type = VNET_L3_PACKET_TYPE_IP4;
1029 adj->lookup_next_index = n;
1030 vnet_rewrite_for_sw_interface
1035 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
1036 &adj->rewrite_header,
1037 sizeof (adj->rewrite_data));
1041 ip4_add_interface_routes (u32 sw_if_index,
1042 ip4_main_t * im, u32 fib_index,
1043 ip_interface_address_t * a)
1045 vnet_main_t * vnm = vnet_get_main();
1046 ip_lookup_main_t * lm = &im->lookup_main;
1047 ip_adjacency_t * adj;
1048 ip4_address_t * address = ip_interface_address_get_address (lm, a);
1049 ip4_add_del_route_args_t x;
1050 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1051 u32 classify_table_index;
1053 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1054 x.table_index_or_table_id = fib_index;
1055 x.flags = (IP4_ROUTE_FLAG_ADD
1056 | IP4_ROUTE_FLAG_FIB_INDEX
1057 | IP4_ROUTE_FLAG_NO_REDISTRIBUTE);
1058 x.dst_address = address[0];
1059 x.dst_address_length = a->address_length;
1063 a->neighbor_probe_adj_index = ~0;
1064 if (a->address_length < 32)
1066 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1068 ip4_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
1069 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1070 ip4_add_del_route (im, &x);
1071 a->neighbor_probe_adj_index = x.adj_index;
1074 /* Add e.g. 1.1.1.1/32 as local to this host. */
1075 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1078 classify_table_index = ~0;
1079 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1080 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1081 if (classify_table_index != (u32) ~0)
1083 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1084 adj->classify.table_index = classify_table_index;
1087 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1089 adj->if_address_index = a - lm->if_address_pool;
1090 adj->rewrite_header.sw_if_index = sw_if_index;
1091 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1093 * Local adjs are never to be rewritten. Spoofed pkts w/ src = dst = local
1094 * fail an RPF-ish check, but still go thru the rewrite code...
1096 adj->rewrite_header.data_bytes = 0;
1098 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1099 x.dst_address_length = 32;
1100 ip4_add_del_route (im, &x);
1104 ip4_del_interface_routes (ip4_main_t * im, u32 fib_index, ip4_address_t * address, u32 address_length)
1106 ip4_add_del_route_args_t x;
1108 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1109 x.table_index_or_table_id = fib_index;
1110 x.flags = (IP4_ROUTE_FLAG_DEL
1111 | IP4_ROUTE_FLAG_FIB_INDEX
1112 | IP4_ROUTE_FLAG_NO_REDISTRIBUTE);
1113 x.dst_address = address[0];
1114 x.dst_address_length = address_length;
1119 if (address_length < 32)
1120 ip4_add_del_route (im, &x);
1122 x.dst_address_length = 32;
1123 ip4_add_del_route (im, &x);
1125 ip4_delete_matching_routes (im,
1127 IP4_ROUTE_FLAG_FIB_INDEX,
1134 ip4_address_t address;
1136 } ip4_interface_address_t;
1138 static clib_error_t *
1139 ip4_add_del_interface_address_internal (vlib_main_t * vm,
1141 ip4_address_t * new_address,
1147 static clib_error_t *
1148 ip4_add_del_interface_address_internal (vlib_main_t * vm,
1150 ip4_address_t * address,
1156 vnet_main_t * vnm = vnet_get_main();
1157 ip4_main_t * im = &ip4_main;
1158 ip_lookup_main_t * lm = &im->lookup_main;
1159 clib_error_t * error = 0;
1160 u32 if_address_index, elts_before;
1161 ip4_address_fib_t ip4_af, * addr_fib = 0;
1163 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1164 ip4_addr_fib_init (&ip4_af, address,
1165 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1166 vec_add1 (addr_fib, ip4_af);
1168 /* When adding an address check that it does not conflict with an existing address. */
1171 ip_interface_address_t * ia;
1172 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1173 0 /* honor unnumbered */,
1175 ip4_address_t * x = ip_interface_address_get_address (&im->lookup_main, ia);
1177 if (ip4_destination_matches_route (im, address, x, ia->address_length)
1178 || ip4_destination_matches_route (im, x, address, address_length))
1179 return clib_error_create ("failed to add %U which conflicts with %U for interface %U",
1180 format_ip4_address_and_length, address, address_length,
1181 format_ip4_address_and_length, x, ia->address_length,
1182 format_vnet_sw_if_index_name, vnm, sw_if_index);
1186 elts_before = pool_elts (lm->if_address_pool);
1188 error = ip_interface_address_add_del
1198 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1201 ip4_del_interface_routes (im, ip4_af.fib_index, address,
1205 ip4_add_interface_routes (sw_if_index,
1206 im, ip4_af.fib_index,
1208 (lm->if_address_pool, if_address_index));
1211 /* If pool did not grow/shrink: add duplicate address. */
1212 if (elts_before != pool_elts (lm->if_address_pool))
1214 ip4_add_del_interface_address_callback_t * cb;
1215 vec_foreach (cb, im->add_del_interface_address_callbacks)
1216 cb->function (im, cb->function_opaque, sw_if_index,
1217 address, address_length,
1223 vec_free (addr_fib);
1228 ip4_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1229 ip4_address_t * address, u32 address_length,
1232 return ip4_add_del_interface_address_internal
1233 (vm, sw_if_index, address, address_length,
1234 /* redistribute */ 1,
1235 /* insert_routes */ 1,
1239 static clib_error_t *
1240 ip4_sw_interface_admin_up_down (vnet_main_t * vnm,
1244 ip4_main_t * im = &ip4_main;
1245 ip_interface_address_t * ia;
1247 u32 is_admin_up, fib_index;
1249 /* Fill in lookup tables with default table (0). */
1250 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1252 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1254 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1256 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1258 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1259 0 /* honor unnumbered */,
1261 a = ip_interface_address_get_address (&im->lookup_main, ia);
1263 ip4_add_interface_routes (sw_if_index,
1267 ip4_del_interface_routes (im, fib_index,
1268 a, ia->address_length);
1274 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip4_sw_interface_admin_up_down);
1276 static clib_error_t *
1277 ip4_sw_interface_add_del (vnet_main_t * vnm,
1281 vlib_main_t * vm = vnm->vlib_main;
1282 ip4_main_t * im = &ip4_main;
1283 ip_lookup_main_t * lm = &im->lookup_main;
1286 for (cast = 0; cast < VNET_N_CAST; cast++)
1288 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1289 vnet_config_main_t * vcm = &cm->config_main;
1291 if (! vcm->node_index_by_feature_index)
1293 if (cast == VNET_UNICAST)
1295 static char * start_nodes[] = { "ip4-input", "ip4-input-no-checksum", };
1296 static char * feature_nodes[] = {
1297 [IP4_RX_FEATURE_CHECK_ACCESS] = "ip4-inacl",
1298 [IP4_RX_FEATURE_SOURCE_CHECK_REACHABLE_VIA_RX] = "ip4-source-check-via-rx",
1299 [IP4_RX_FEATURE_SOURCE_CHECK_REACHABLE_VIA_ANY] = "ip4-source-check-via-any",
1300 [IP4_RX_FEATURE_IPSEC] = "ipsec-input-ip4",
1301 [IP4_RX_FEATURE_VPATH] = "vpath-input-ip4",
1302 [IP4_RX_FEATURE_LOOKUP] = "ip4-lookup",
1305 vnet_config_init (vm, vcm,
1306 start_nodes, ARRAY_LEN (start_nodes),
1307 feature_nodes, ARRAY_LEN (feature_nodes));
1311 static char * start_nodes[] = { "ip4-input", "ip4-input-no-checksum", };
1312 static char * feature_nodes[] = {
1313 [IP4_RX_FEATURE_VPATH] = "vpath-input-ip4",
1314 [IP4_RX_FEATURE_LOOKUP] = "ip4-lookup-multicast",
1317 vnet_config_init (vm, vcm,
1318 start_nodes, ARRAY_LEN (start_nodes),
1319 feature_nodes, ARRAY_LEN (feature_nodes));
1323 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1324 ci = cm->config_index_by_sw_if_index[sw_if_index];
1327 ci = vnet_config_add_feature (vm, vcm,
1329 IP4_RX_FEATURE_LOOKUP,
1330 /* config data */ 0,
1331 /* # bytes of config data */ 0);
1333 ci = vnet_config_del_feature (vm, vcm,
1335 IP4_RX_FEATURE_LOOKUP,
1336 /* config data */ 0,
1337 /* # bytes of config data */ 0);
1339 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1342 return /* no error */ 0;
1345 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip4_sw_interface_add_del);
1348 VLIB_REGISTER_NODE (ip4_lookup_node) = {
1349 .function = ip4_lookup,
1350 .name = "ip4-lookup",
1351 .vector_size = sizeof (u32),
1353 .n_next_nodes = IP_LOOKUP_N_NEXT,
1354 .next_nodes = IP4_LOOKUP_NEXT_NODES,
1357 VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_node, ip4_lookup)
1360 ip4_indirect (vlib_main_t * vm,
1361 vlib_node_runtime_t * node,
1362 vlib_frame_t * frame)
1364 return ip4_lookup_inline (vm, node, frame,
1365 /* lookup_for_responses_to_locally_received_packets */ 0,
1366 /* is_indirect */ 1);
1369 VLIB_REGISTER_NODE (ip4_indirect_node) = {
1370 .function = ip4_indirect,
1371 .name = "ip4-indirect",
1372 .vector_size = sizeof (u32),
1374 .n_next_nodes = IP_LOOKUP_N_NEXT,
1375 .next_nodes = IP4_LOOKUP_NEXT_NODES,
1378 VLIB_NODE_FUNCTION_MULTIARCH (ip4_indirect_node, ip4_indirect)
1381 /* Global IP4 main. */
1382 ip4_main_t ip4_main;
1385 ip4_lookup_init (vlib_main_t * vm)
1387 ip4_main_t * im = &ip4_main;
1390 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
1395 m = pow2_mask (i) << (32 - i);
1398 im->fib_masks[i] = clib_host_to_net_u32 (m);
1401 /* Create FIB with index 0 and table id of 0. */
1402 find_ip4_fib_by_table_index_or_id (im, /* table id */ 0, IP4_ROUTE_FLAG_TABLE_ID);
1404 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 0);
1408 pn = pg_get_node (ip4_lookup_node.index);
1409 pn->unformat_edit = unformat_pg_ip4_header;
1413 ethernet_arp_header_t h;
1415 memset (&h, 0, sizeof (h));
1417 /* Set target ethernet address to all zeros. */
1418 memset (h.ip4_over_ethernet[1].ethernet, 0, sizeof (h.ip4_over_ethernet[1].ethernet));
1420 #define _16(f,v) h.f = clib_host_to_net_u16 (v);
1421 #define _8(f,v) h.f = v;
1422 _16 (l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet);
1423 _16 (l3_type, ETHERNET_TYPE_IP4);
1424 _8 (n_l2_address_bytes, 6);
1425 _8 (n_l3_address_bytes, 4);
1426 _16 (opcode, ETHERNET_ARP_OPCODE_request);
1430 vlib_packet_template_init (vm,
1431 &im->ip4_arp_request_packet_template,
1434 /* alloc chunk size */ 8,
1441 VLIB_INIT_FUNCTION (ip4_lookup_init);
1444 /* Adjacency taken. */
1449 /* Packet data, possibly *after* rewrite. */
1450 u8 packet_data[64 - 1*sizeof(u32)];
1451 } ip4_forward_next_trace_t;
1453 static u8 * format_ip4_forward_next_trace (u8 * s, va_list * args)
1455 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1456 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1457 ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1458 vnet_main_t * vnm = vnet_get_main();
1459 ip4_main_t * im = &ip4_main;
1460 ip_adjacency_t * adj;
1461 uword indent = format_get_indent (s);
1463 adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1464 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1465 t->fib_index, t->adj_index, format_ip_adjacency,
1466 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1467 switch (adj->lookup_next_index)
1469 case IP_LOOKUP_NEXT_REWRITE:
1470 s = format (s, "\n%U%U",
1471 format_white_space, indent,
1472 format_ip_adjacency_packet_data,
1473 vnm, &im->lookup_main, t->adj_index,
1474 t->packet_data, sizeof (t->packet_data));
1484 /* Common trace function for all ip4-forward next nodes. */
1486 ip4_forward_next_trace (vlib_main_t * vm,
1487 vlib_node_runtime_t * node,
1488 vlib_frame_t * frame,
1489 vlib_rx_or_tx_t which_adj_index)
1492 ip4_main_t * im = &ip4_main;
1494 n_left = frame->n_vectors;
1495 from = vlib_frame_vector_args (frame);
1500 vlib_buffer_t * b0, * b1;
1501 ip4_forward_next_trace_t * t0, * t1;
1503 /* Prefetch next iteration. */
1504 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1505 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1510 b0 = vlib_get_buffer (vm, bi0);
1511 b1 = vlib_get_buffer (vm, bi1);
1513 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1515 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1516 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1517 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1518 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1519 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1520 clib_memcpy (t0->packet_data,
1521 vlib_buffer_get_current (b0),
1522 sizeof (t0->packet_data));
1524 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1526 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1527 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1528 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1529 t1->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1530 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1531 clib_memcpy (t1->packet_data,
1532 vlib_buffer_get_current (b1),
1533 sizeof (t1->packet_data));
1543 ip4_forward_next_trace_t * t0;
1547 b0 = vlib_get_buffer (vm, bi0);
1549 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1551 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1552 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1553 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1554 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1555 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1556 clib_memcpy (t0->packet_data,
1557 vlib_buffer_get_current (b0),
1558 sizeof (t0->packet_data));
1566 ip4_drop_or_punt (vlib_main_t * vm,
1567 vlib_node_runtime_t * node,
1568 vlib_frame_t * frame,
1569 ip4_error_t error_code)
1571 u32 * buffers = vlib_frame_vector_args (frame);
1572 uword n_packets = frame->n_vectors;
1574 vlib_error_drop_buffers (vm, node,
1579 ip4_input_node.index,
1582 if (node->flags & VLIB_NODE_FLAG_TRACE)
1583 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
1589 ip4_drop (vlib_main_t * vm,
1590 vlib_node_runtime_t * node,
1591 vlib_frame_t * frame)
1592 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_ADJACENCY_DROP); }
1595 ip4_punt (vlib_main_t * vm,
1596 vlib_node_runtime_t * node,
1597 vlib_frame_t * frame)
1598 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_ADJACENCY_PUNT); }
1601 ip4_miss (vlib_main_t * vm,
1602 vlib_node_runtime_t * node,
1603 vlib_frame_t * frame)
1604 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_DST_LOOKUP_MISS); }
1606 VLIB_REGISTER_NODE (ip4_drop_node,static) = {
1607 .function = ip4_drop,
1609 .vector_size = sizeof (u32),
1611 .format_trace = format_ip4_forward_next_trace,
1619 VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop)
1621 VLIB_REGISTER_NODE (ip4_punt_node,static) = {
1622 .function = ip4_punt,
1624 .vector_size = sizeof (u32),
1626 .format_trace = format_ip4_forward_next_trace,
1634 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_node, ip4_punt)
1636 VLIB_REGISTER_NODE (ip4_miss_node,static) = {
1637 .function = ip4_miss,
1639 .vector_size = sizeof (u32),
1641 .format_trace = format_ip4_forward_next_trace,
1649 VLIB_NODE_FUNCTION_MULTIARCH (ip4_miss_node, ip4_miss)
1651 /* Compute TCP/UDP/ICMP4 checksum in software. */
1653 ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1657 u32 ip_header_length, payload_length_host_byte_order;
1658 u32 n_this_buffer, n_bytes_left;
1660 void * data_this_buffer;
1662 /* Initialize checksum with ip header. */
1663 ip_header_length = ip4_header_bytes (ip0);
1664 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->length) - ip_header_length;
1665 sum0 = clib_host_to_net_u32 (payload_length_host_byte_order + (ip0->protocol << 16));
1667 if (BITS (uword) == 32)
1669 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u32));
1670 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->dst_address, u32));
1673 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u64));
1675 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1676 data_this_buffer = (void *) ip0 + ip_header_length;
1677 if (n_this_buffer + ip_header_length > p0->current_length)
1678 n_this_buffer = p0->current_length > ip_header_length ? p0->current_length - ip_header_length : 0;
1681 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1682 n_bytes_left -= n_this_buffer;
1683 if (n_bytes_left == 0)
1686 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
1687 p0 = vlib_get_buffer (vm, p0->next_buffer);
1688 data_this_buffer = vlib_buffer_get_current (p0);
1689 n_this_buffer = p0->current_length;
1692 sum16 = ~ ip_csum_fold (sum0);
1698 ip4_tcp_udp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1700 ip4_header_t * ip0 = vlib_buffer_get_current (p0);
1701 udp_header_t * udp0;
1704 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1705 || ip0->protocol == IP_PROTOCOL_UDP);
1707 udp0 = (void *) (ip0 + 1);
1708 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1710 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1711 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1715 sum16 = ip4_tcp_udp_compute_checksum (vm, p0, ip0);
1717 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1718 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1724 ip4_local (vlib_main_t * vm,
1725 vlib_node_runtime_t * node,
1726 vlib_frame_t * frame)
1728 ip4_main_t * im = &ip4_main;
1729 ip_lookup_main_t * lm = &im->lookup_main;
1730 ip_local_next_t next_index;
1731 u32 * from, * to_next, n_left_from, n_left_to_next;
1732 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
1734 from = vlib_frame_vector_args (frame);
1735 n_left_from = frame->n_vectors;
1736 next_index = node->cached_next_index;
1738 if (node->flags & VLIB_NODE_FLAG_TRACE)
1739 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
1741 while (n_left_from > 0)
1743 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1745 while (n_left_from >= 4 && n_left_to_next >= 2)
1747 vlib_buffer_t * p0, * p1;
1748 ip4_header_t * ip0, * ip1;
1749 udp_header_t * udp0, * udp1;
1750 ip4_fib_mtrie_t * mtrie0, * mtrie1;
1751 ip4_fib_mtrie_leaf_t leaf0, leaf1;
1752 ip_adjacency_t * adj0, * adj1;
1753 u32 pi0, ip_len0, udp_len0, flags0, next0, fib_index0, adj_index0;
1754 u32 pi1, ip_len1, udp_len1, flags1, next1, fib_index1, adj_index1;
1755 i32 len_diff0, len_diff1;
1756 u8 error0, is_udp0, is_tcp_udp0, good_tcp_udp0, proto0;
1757 u8 error1, is_udp1, is_tcp_udp1, good_tcp_udp1, proto1;
1760 pi0 = to_next[0] = from[0];
1761 pi1 = to_next[1] = from[1];
1765 n_left_to_next -= 2;
1767 p0 = vlib_get_buffer (vm, pi0);
1768 p1 = vlib_get_buffer (vm, pi1);
1770 ip0 = vlib_buffer_get_current (p0);
1771 ip1 = vlib_buffer_get_current (p1);
1773 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
1774 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
1775 fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
1776 vnet_buffer(p1)->sw_if_index[VLIB_RX]);
1778 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
1779 mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie;
1781 leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
1783 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
1784 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 0);
1786 /* Treat IP frag packets as "experimental" protocol for now
1787 until support of IP frag reassembly is implemented */
1788 proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
1789 proto1 = ip4_is_fragment(ip1) ? 0xfe : ip1->protocol;
1790 is_udp0 = proto0 == IP_PROTOCOL_UDP;
1791 is_udp1 = proto1 == IP_PROTOCOL_UDP;
1792 is_tcp_udp0 = is_udp0 || proto0 == IP_PROTOCOL_TCP;
1793 is_tcp_udp1 = is_udp1 || proto1 == IP_PROTOCOL_TCP;
1798 good_tcp_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1799 good_tcp_udp1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1801 udp0 = ip4_next_header (ip0);
1802 udp1 = ip4_next_header (ip1);
1804 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1805 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1806 good_tcp_udp1 |= is_udp1 && udp1->checksum == 0;
1808 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
1809 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 1);
1811 /* Verify UDP length. */
1812 ip_len0 = clib_net_to_host_u16 (ip0->length);
1813 ip_len1 = clib_net_to_host_u16 (ip1->length);
1814 udp_len0 = clib_net_to_host_u16 (udp0->length);
1815 udp_len1 = clib_net_to_host_u16 (udp1->length);
1817 len_diff0 = ip_len0 - udp_len0;
1818 len_diff1 = ip_len1 - udp_len1;
1820 len_diff0 = is_udp0 ? len_diff0 : 0;
1821 len_diff1 = is_udp1 ? len_diff1 : 0;
1823 if (PREDICT_FALSE (! (is_tcp_udp0 & is_tcp_udp1
1824 & good_tcp_udp0 & good_tcp_udp1)))
1829 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
1830 flags0 = ip4_tcp_udp_validate_checksum (vm, p0);
1832 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1833 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1838 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
1839 flags1 = ip4_tcp_udp_validate_checksum (vm, p1);
1841 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1842 good_tcp_udp1 |= is_udp1 && udp1->checksum == 0;
1846 good_tcp_udp0 &= len_diff0 >= 0;
1847 good_tcp_udp1 &= len_diff1 >= 0;
1849 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
1850 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2);
1852 error0 = error1 = IP4_ERROR_UNKNOWN_PROTOCOL;
1854 error0 = len_diff0 < 0 ? IP4_ERROR_UDP_LENGTH : error0;
1855 error1 = len_diff1 < 0 ? IP4_ERROR_UDP_LENGTH : error1;
1857 ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM);
1858 error0 = (is_tcp_udp0 && ! good_tcp_udp0
1859 ? IP4_ERROR_TCP_CHECKSUM + is_udp0
1861 error1 = (is_tcp_udp1 && ! good_tcp_udp1
1862 ? IP4_ERROR_TCP_CHECKSUM + is_udp1
1865 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
1866 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3);
1868 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
1869 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
1871 vnet_buffer (p1)->ip.adj_index[VLIB_RX] = adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
1872 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
1874 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
1876 /* no_default_route */ 1));
1877 ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
1879 /* no_default_route */ 1));
1881 adj0 = ip_get_adjacency (lm, adj_index0);
1882 adj1 = ip_get_adjacency (lm, adj_index1);
1885 * Must have a route to source otherwise we drop the packet.
1886 * ip4 broadcasts are accepted, e.g. to make dhcp client work
1888 error0 = (error0 == IP4_ERROR_UNKNOWN_PROTOCOL
1889 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
1890 && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
1891 && adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
1892 && ip0->dst_address.as_u32 != 0xFFFFFFFF
1893 ? IP4_ERROR_SRC_LOOKUP_MISS
1895 error1 = (error1 == IP4_ERROR_UNKNOWN_PROTOCOL
1896 && adj1->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
1897 && adj1->lookup_next_index != IP_LOOKUP_NEXT_ARP
1898 && adj1->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
1899 && ip0->dst_address.as_u32 != 0xFFFFFFFF
1900 ? IP4_ERROR_SRC_LOOKUP_MISS
1903 next0 = lm->local_next_by_ip_protocol[proto0];
1904 next1 = lm->local_next_by_ip_protocol[proto1];
1906 next0 = error0 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1907 next1 = error1 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1909 p0->error = error0 ? error_node->errors[error0] : 0;
1910 p1->error = error1 ? error_node->errors[error1] : 0;
1912 enqueue_code = (next0 != next_index) + 2*(next1 != next_index);
1914 if (PREDICT_FALSE (enqueue_code != 0))
1916 switch (enqueue_code)
1922 n_left_to_next += 1;
1923 vlib_set_next_frame_buffer (vm, node, next0, pi0);
1929 n_left_to_next += 1;
1930 vlib_set_next_frame_buffer (vm, node, next1, pi1);
1934 /* A B B or A B C */
1936 n_left_to_next += 2;
1937 vlib_set_next_frame_buffer (vm, node, next0, pi0);
1938 vlib_set_next_frame_buffer (vm, node, next1, pi1);
1941 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1943 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1950 while (n_left_from > 0 && n_left_to_next > 0)
1954 udp_header_t * udp0;
1955 ip4_fib_mtrie_t * mtrie0;
1956 ip4_fib_mtrie_leaf_t leaf0;
1957 ip_adjacency_t * adj0;
1958 u32 pi0, next0, ip_len0, udp_len0, flags0, fib_index0, adj_index0;
1960 u8 error0, is_udp0, is_tcp_udp0, good_tcp_udp0, proto0;
1962 pi0 = to_next[0] = from[0];
1966 n_left_to_next -= 1;
1968 p0 = vlib_get_buffer (vm, pi0);
1970 ip0 = vlib_buffer_get_current (p0);
1972 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
1973 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
1975 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
1977 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
1979 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
1981 /* Treat IP frag packets as "experimental" protocol for now
1982 until support of IP frag reassembly is implemented */
1983 proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
1984 is_udp0 = proto0 == IP_PROTOCOL_UDP;
1985 is_tcp_udp0 = is_udp0 || proto0 == IP_PROTOCOL_TCP;
1989 good_tcp_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1991 udp0 = ip4_next_header (ip0);
1993 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1994 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1996 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
1998 /* Verify UDP length. */
1999 ip_len0 = clib_net_to_host_u16 (ip0->length);
2000 udp_len0 = clib_net_to_host_u16 (udp0->length);
2002 len_diff0 = ip_len0 - udp_len0;
2004 len_diff0 = is_udp0 ? len_diff0 : 0;
2006 if (PREDICT_FALSE (! (is_tcp_udp0 & good_tcp_udp0)))
2011 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
2012 flags0 = ip4_tcp_udp_validate_checksum (vm, p0);
2014 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
2015 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
2019 good_tcp_udp0 &= len_diff0 >= 0;
2021 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
2023 error0 = IP4_ERROR_UNKNOWN_PROTOCOL;
2025 error0 = len_diff0 < 0 ? IP4_ERROR_UDP_LENGTH : error0;
2027 ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM);
2028 error0 = (is_tcp_udp0 && ! good_tcp_udp0
2029 ? IP4_ERROR_TCP_CHECKSUM + is_udp0
2032 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
2034 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
2035 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
2037 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
2039 /* no_default_route */ 1));
2041 adj0 = ip_get_adjacency (lm, adj_index0);
2043 /* Must have a route to source otherwise we drop the packet. */
2044 error0 = (error0 == IP4_ERROR_UNKNOWN_PROTOCOL
2045 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
2046 && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
2047 && adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
2048 && ip0->dst_address.as_u32 != 0xFFFFFFFF
2049 ? IP4_ERROR_SRC_LOOKUP_MISS
2052 next0 = lm->local_next_by_ip_protocol[proto0];
2054 next0 = error0 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
2056 p0->error = error0? error_node->errors[error0] : 0;
2058 if (PREDICT_FALSE (next0 != next_index))
2060 n_left_to_next += 1;
2061 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2064 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2067 n_left_to_next -= 1;
2071 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2074 return frame->n_vectors;
2077 VLIB_REGISTER_NODE (ip4_local_node,static) = {
2078 .function = ip4_local,
2079 .name = "ip4-local",
2080 .vector_size = sizeof (u32),
2082 .format_trace = format_ip4_forward_next_trace,
2084 .n_next_nodes = IP_LOCAL_N_NEXT,
2086 [IP_LOCAL_NEXT_DROP] = "error-drop",
2087 [IP_LOCAL_NEXT_PUNT] = "error-punt",
2088 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip4-udp-lookup",
2089 [IP_LOCAL_NEXT_ICMP] = "ip4-icmp-input",
2093 VLIB_NODE_FUNCTION_MULTIARCH (ip4_local_node, ip4_local)
2095 void ip4_register_protocol (u32 protocol, u32 node_index)
2097 vlib_main_t * vm = vlib_get_main();
2098 ip4_main_t * im = &ip4_main;
2099 ip_lookup_main_t * lm = &im->lookup_main;
2101 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
2102 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip4_local_node.index, node_index);
2105 static clib_error_t *
2106 show_ip_local_command_fn (vlib_main_t * vm,
2107 unformat_input_t * input,
2108 vlib_cli_command_t * cmd)
2110 ip4_main_t * im = &ip4_main;
2111 ip_lookup_main_t * lm = &im->lookup_main;
2114 vlib_cli_output (vm, "Protocols handled by ip4_local");
2115 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2117 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2118 vlib_cli_output (vm, "%d", i);
2125 VLIB_CLI_COMMAND (show_ip_local, static) = {
2126 .path = "show ip local",
2127 .function = show_ip_local_command_fn,
2128 .short_help = "Show ip local protocol table",
2132 ip4_arp (vlib_main_t * vm,
2133 vlib_node_runtime_t * node,
2134 vlib_frame_t * frame)
2136 vnet_main_t * vnm = vnet_get_main();
2137 ip4_main_t * im = &ip4_main;
2138 ip_lookup_main_t * lm = &im->lookup_main;
2139 u32 * from, * to_next_drop;
2140 uword n_left_from, n_left_to_next_drop, next_index;
2141 static f64 time_last_seed_change = -1e100;
2142 static u32 hash_seeds[3];
2143 static uword hash_bitmap[256 / BITS (uword)];
2146 if (node->flags & VLIB_NODE_FLAG_TRACE)
2147 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
2149 time_now = vlib_time_now (vm);
2150 if (time_now - time_last_seed_change > 1e-3)
2153 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
2154 sizeof (hash_seeds));
2155 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
2156 hash_seeds[i] = r[i];
2158 /* Mark all hash keys as been no-seen before. */
2159 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
2162 time_last_seed_change = time_now;
2165 from = vlib_frame_vector_args (frame);
2166 n_left_from = frame->n_vectors;
2167 next_index = node->cached_next_index;
2168 if (next_index == IP4_ARP_NEXT_DROP)
2169 next_index = IP4_ARP_N_NEXT; /* point to first interface */
2171 while (n_left_from > 0)
2173 vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP,
2174 to_next_drop, n_left_to_next_drop);
2176 while (n_left_from > 0 && n_left_to_next_drop > 0)
2180 ethernet_header_t * eh0;
2181 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
2183 ip_adjacency_t * adj0;
2187 p0 = vlib_get_buffer (vm, pi0);
2189 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2190 adj0 = ip_get_adjacency (lm, adj_index0);
2191 ip0 = vlib_buffer_get_current (p0);
2193 /* If packet destination is not local, send ARP to next hop */
2194 if (adj0->arp.next_hop.ip4.as_u32)
2195 ip0->dst_address.data_u32 = adj0->arp.next_hop.ip4.as_u32;
2198 * if ip4_rewrite_local applied the IP_LOOKUP_NEXT_ARP
2199 * rewrite to this packet, we need to skip it here.
2200 * Note, to distinguish from src IP addr *.8.6.*, we
2201 * check for a bcast eth dest instead of IPv4 version.
2203 eh0 = (ethernet_header_t*)ip0;
2204 if ((ip0->ip_version_and_header_length & 0xF0) != 0x40)
2207 u16 * etype = &eh0->type;
2208 while ((*etype == clib_host_to_net_u16 (0x8100)) //dot1q
2209 || (*etype == clib_host_to_net_u16 (0x88a8)))//dot1ad
2212 etype += 2; //vlan tag also 16 bits, same as etype
2214 if (*etype == clib_host_to_net_u16 (0x0806)) //arp
2216 vlib_buffer_advance (
2217 p0, sizeof(ethernet_header_t) + (4*vlan_num));
2218 ip0 = vlib_buffer_get_current (p0);
2226 sw_if_index0 = adj0->rewrite_header.sw_if_index;
2227 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
2229 a0 ^= ip0->dst_address.data_u32;
2232 hash_v3_finalize32 (a0, b0, c0);
2234 c0 &= BITS (hash_bitmap) - 1;
2235 c0 = c0 / BITS (uword);
2236 m0 = (uword) 1 << (c0 % BITS (uword));
2238 bm0 = hash_bitmap[c0];
2239 drop0 = (bm0 & m0) != 0;
2241 /* Mark it as seen. */
2242 hash_bitmap[c0] = bm0 | m0;
2246 to_next_drop[0] = pi0;
2248 n_left_to_next_drop -= 1;
2250 p0->error = node->errors[drop0 ? IP4_ARP_ERROR_DROP : IP4_ARP_ERROR_REQUEST_SENT];
2256 * Can happen if the control-plane is programming tables
2257 * with traffic flowing; at least that's today's lame excuse.
2259 if (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP)
2261 p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ];
2264 /* Send ARP request. */
2268 ethernet_arp_header_t * h0;
2269 vnet_hw_interface_t * hw_if0;
2271 h0 = vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, &bi0);
2273 /* Add rewrite/encap string for ARP packet. */
2274 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
2276 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2278 /* Src ethernet address in ARP header. */
2279 clib_memcpy (h0->ip4_over_ethernet[0].ethernet, hw_if0->hw_address,
2280 sizeof (h0->ip4_over_ethernet[0].ethernet));
2282 ip4_src_address_for_packet (im, p0, &h0->ip4_over_ethernet[0].ip4, sw_if_index0);
2284 /* Copy in destination address we are requesting. */
2285 h0->ip4_over_ethernet[1].ip4.data_u32 = ip0->dst_address.data_u32;
2287 vlib_buffer_copy_trace_flag (vm, p0, bi0);
2288 b0 = vlib_get_buffer (vm, bi0);
2289 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
2291 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2293 vlib_set_next_frame_buffer (vm, node, adj0->rewrite_header.next_index, bi0);
2297 vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
2300 return frame->n_vectors;
2303 static char * ip4_arp_error_strings[] = {
2304 [IP4_ARP_ERROR_DROP] = "address overflow drops",
2305 [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
2306 [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
2307 [IP4_ARP_ERROR_REPLICATE_DROP] = "ARP replication completed",
2308 [IP4_ARP_ERROR_REPLICATE_FAIL] = "ARP replication failed",
2311 VLIB_REGISTER_NODE (ip4_arp_node) = {
2312 .function = ip4_arp,
2314 .vector_size = sizeof (u32),
2316 .format_trace = format_ip4_forward_next_trace,
2318 .n_errors = ARRAY_LEN (ip4_arp_error_strings),
2319 .error_strings = ip4_arp_error_strings,
2321 .n_next_nodes = IP4_ARP_N_NEXT,
2323 [IP4_ARP_NEXT_DROP] = "error-drop",
2327 #define foreach_notrace_ip4_arp_error \
2333 clib_error_t * arp_notrace_init (vlib_main_t * vm)
2335 vlib_node_runtime_t *rt =
2336 vlib_node_get_runtime (vm, ip4_arp_node.index);
2338 /* don't trace ARP request packets */
2340 vnet_pcap_drop_trace_filter_add_del \
2341 (rt->errors[IP4_ARP_ERROR_##a], \
2343 foreach_notrace_ip4_arp_error;
2348 VLIB_INIT_FUNCTION(arp_notrace_init);
2351 /* Send an ARP request to see if given destination is reachable on given interface. */
2353 ip4_probe_neighbor (vlib_main_t * vm, ip4_address_t * dst, u32 sw_if_index)
2355 vnet_main_t * vnm = vnet_get_main();
2356 ip4_main_t * im = &ip4_main;
2357 ethernet_arp_header_t * h;
2358 ip4_address_t * src;
2359 ip_interface_address_t * ia;
2360 ip_adjacency_t * adj;
2361 vnet_hw_interface_t * hi;
2362 vnet_sw_interface_t * si;
2366 si = vnet_get_sw_interface (vnm, sw_if_index);
2368 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2370 return clib_error_return (0, "%U: interface %U down",
2371 format_ip4_address, dst,
2372 format_vnet_sw_if_index_name, vnm,
2376 src = ip4_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2379 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2380 return clib_error_return
2381 (0, "no matching interface address for destination %U (interface %U)",
2382 format_ip4_address, dst,
2383 format_vnet_sw_if_index_name, vnm, sw_if_index);
2386 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2388 h = vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, &bi);
2390 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2392 clib_memcpy (h->ip4_over_ethernet[0].ethernet, hi->hw_address, sizeof (h->ip4_over_ethernet[0].ethernet));
2394 h->ip4_over_ethernet[0].ip4 = src[0];
2395 h->ip4_over_ethernet[1].ip4 = dst[0];
2397 b = vlib_get_buffer (vm, bi);
2398 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2400 /* Add encapsulation string for software interface (e.g. ethernet header). */
2401 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2402 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2405 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2406 u32 * to_next = vlib_frame_vector_args (f);
2409 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2412 return /* no error */ 0;
2416 IP4_REWRITE_NEXT_DROP,
2417 IP4_REWRITE_NEXT_ARP,
2418 } ip4_rewrite_next_t;
2421 ip4_rewrite_inline (vlib_main_t * vm,
2422 vlib_node_runtime_t * node,
2423 vlib_frame_t * frame,
2424 int rewrite_for_locally_received_packets)
2426 ip_lookup_main_t * lm = &ip4_main.lookup_main;
2427 u32 * from = vlib_frame_vector_args (frame);
2428 u32 n_left_from, n_left_to_next, * to_next, next_index;
2429 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
2430 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2432 n_left_from = frame->n_vectors;
2433 next_index = node->cached_next_index;
2434 u32 cpu_index = os_get_cpu_number();
2436 while (n_left_from > 0)
2438 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2440 while (n_left_from >= 4 && n_left_to_next >= 2)
2442 ip_adjacency_t * adj0, * adj1;
2443 vlib_buffer_t * p0, * p1;
2444 ip4_header_t * ip0, * ip1;
2445 u32 pi0, rw_len0, next0, error0, checksum0, adj_index0;
2446 u32 pi1, rw_len1, next1, error1, checksum1, adj_index1;
2447 u32 next0_override, next1_override;
2449 if (rewrite_for_locally_received_packets)
2450 next0_override = next1_override = 0;
2452 /* Prefetch next iteration. */
2454 vlib_buffer_t * p2, * p3;
2456 p2 = vlib_get_buffer (vm, from[2]);
2457 p3 = vlib_get_buffer (vm, from[3]);
2459 vlib_prefetch_buffer_header (p2, STORE);
2460 vlib_prefetch_buffer_header (p3, STORE);
2462 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2463 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2466 pi0 = to_next[0] = from[0];
2467 pi1 = to_next[1] = from[1];
2472 n_left_to_next -= 2;
2474 p0 = vlib_get_buffer (vm, pi0);
2475 p1 = vlib_get_buffer (vm, pi1);
2477 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2478 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2480 /* We should never rewrite a pkt using the MISS adjacency */
2481 ASSERT(adj_index0 && adj_index1);
2483 ip0 = vlib_buffer_get_current (p0);
2484 ip1 = vlib_buffer_get_current (p1);
2486 error0 = error1 = IP4_ERROR_NONE;
2488 /* Decrement TTL & update checksum.
2489 Works either endian, so no need for byte swap. */
2490 if (! rewrite_for_locally_received_packets)
2492 i32 ttl0 = ip0->ttl, ttl1 = ip1->ttl;
2494 /* Input node should have reject packets with ttl 0. */
2495 ASSERT (ip0->ttl > 0);
2496 ASSERT (ip1->ttl > 0);
2498 checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
2499 checksum1 = ip1->checksum + clib_host_to_net_u16 (0x0100);
2501 checksum0 += checksum0 >= 0xffff;
2502 checksum1 += checksum1 >= 0xffff;
2504 ip0->checksum = checksum0;
2505 ip1->checksum = checksum1;
2513 error0 = ttl0 <= 0 ? IP4_ERROR_TIME_EXPIRED : error0;
2514 error1 = ttl1 <= 0 ? IP4_ERROR_TIME_EXPIRED : error1;
2516 /* Verify checksum. */
2517 ASSERT (ip0->checksum == ip4_header_checksum (ip0));
2518 ASSERT (ip1->checksum == ip4_header_checksum (ip1));
2521 /* Rewrite packet header and updates lengths. */
2522 adj0 = ip_get_adjacency (lm, adj_index0);
2523 adj1 = ip_get_adjacency (lm, adj_index1);
2525 if (rewrite_for_locally_received_packets)
2528 * If someone sends e.g. an icmp4 w/ src = dst = interface addr,
2529 * we end up here with a local adjacency in hand
2530 * The local adj rewrite data is 0xfefe on purpose.
2531 * Bad engineer, no donut for you.
2533 if (PREDICT_FALSE(adj0->lookup_next_index
2534 == IP_LOOKUP_NEXT_LOCAL))
2535 error0 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2536 if (PREDICT_FALSE(adj0->lookup_next_index
2537 == IP_LOOKUP_NEXT_ARP))
2538 next0_override = IP4_REWRITE_NEXT_ARP;
2539 if (PREDICT_FALSE(adj1->lookup_next_index
2540 == IP_LOOKUP_NEXT_LOCAL))
2541 error1 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2542 if (PREDICT_FALSE(adj1->lookup_next_index
2543 == IP_LOOKUP_NEXT_ARP))
2544 next1_override = IP4_REWRITE_NEXT_ARP;
2547 /* Worth pipelining. No guarantee that adj0,1 are hot... */
2548 rw_len0 = adj0[0].rewrite_header.data_bytes;
2549 rw_len1 = adj1[0].rewrite_header.data_bytes;
2550 next0 = (error0 == IP4_ERROR_NONE)
2551 ? adj0[0].rewrite_header.next_index : 0;
2553 if (rewrite_for_locally_received_packets)
2554 next0 = next0 && next0_override ? next0_override : next0;
2556 next1 = (error1 == IP4_ERROR_NONE)
2557 ? adj1[0].rewrite_header.next_index : 0;
2559 if (rewrite_for_locally_received_packets)
2560 next1 = next1 && next1_override ? next1_override : next1;
2563 * We've already accounted for an ethernet_header_t elsewhere
2565 if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
2566 vlib_increment_combined_counter
2567 (&lm->adjacency_counters,
2568 cpu_index, adj_index0,
2569 /* packet increment */ 0,
2570 /* byte increment */ rw_len0-sizeof(ethernet_header_t));
2572 if (PREDICT_FALSE (rw_len1 > sizeof(ethernet_header_t)))
2573 vlib_increment_combined_counter
2574 (&lm->adjacency_counters,
2575 cpu_index, adj_index1,
2576 /* packet increment */ 0,
2577 /* byte increment */ rw_len1-sizeof(ethernet_header_t));
2579 /* Check MTU of outgoing interface. */
2580 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2581 ? IP4_ERROR_MTU_EXCEEDED
2583 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2584 ? IP4_ERROR_MTU_EXCEEDED
2587 p0->current_data -= rw_len0;
2588 p1->current_data -= rw_len1;
2590 p0->current_length += rw_len0;
2591 p1->current_length += rw_len1;
2593 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2594 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2596 p0->error = error_node->errors[error0];
2597 p1->error = error_node->errors[error1];
2599 /* Guess we are only writing on simple Ethernet header. */
2600 vnet_rewrite_two_headers (adj0[0], adj1[0],
2602 sizeof (ethernet_header_t));
2604 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2605 to_next, n_left_to_next,
2606 pi0, pi1, next0, next1);
2609 while (n_left_from > 0 && n_left_to_next > 0)
2611 ip_adjacency_t * adj0;
2614 u32 pi0, rw_len0, adj_index0, next0, error0, checksum0;
2617 if (rewrite_for_locally_received_packets)
2620 pi0 = to_next[0] = from[0];
2622 p0 = vlib_get_buffer (vm, pi0);
2624 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2626 /* We should never rewrite a pkt using the MISS adjacency */
2629 adj0 = ip_get_adjacency (lm, adj_index0);
2631 ip0 = vlib_buffer_get_current (p0);
2633 error0 = IP4_ERROR_NONE;
2634 next0 = 0; /* drop on error */
2636 /* Decrement TTL & update checksum. */
2637 if (! rewrite_for_locally_received_packets)
2639 i32 ttl0 = ip0->ttl;
2641 checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
2643 checksum0 += checksum0 >= 0xffff;
2645 ip0->checksum = checksum0;
2647 ASSERT (ip0->ttl > 0);
2653 ASSERT (ip0->checksum == ip4_header_checksum (ip0));
2655 error0 = ttl0 <= 0 ? IP4_ERROR_TIME_EXPIRED : error0;
2658 if (rewrite_for_locally_received_packets)
2661 * If someone sends e.g. an icmp4 w/ src = dst = interface addr,
2662 * we end up here with a local adjacency in hand
2663 * The local adj rewrite data is 0xfefe on purpose.
2664 * Bad engineer, no donut for you.
2666 if (PREDICT_FALSE(adj0->lookup_next_index
2667 == IP_LOOKUP_NEXT_LOCAL))
2668 error0 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2670 * We have to override the next_index in ARP adjacencies,
2671 * because they're set up for ip4-arp, not this node...
2673 if (PREDICT_FALSE(adj0->lookup_next_index
2674 == IP_LOOKUP_NEXT_ARP))
2675 next0_override = IP4_REWRITE_NEXT_ARP;
2678 /* Guess we are only writing on simple Ethernet header. */
2679 vnet_rewrite_one_header (adj0[0], ip0,
2680 sizeof (ethernet_header_t));
2682 /* Update packet buffer attributes/set output interface. */
2683 rw_len0 = adj0[0].rewrite_header.data_bytes;
2685 if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
2686 vlib_increment_combined_counter
2687 (&lm->adjacency_counters,
2688 cpu_index, adj_index0,
2689 /* packet increment */ 0,
2690 /* byte increment */ rw_len0-sizeof(ethernet_header_t));
2692 /* Check MTU of outgoing interface. */
2693 error0 = (vlib_buffer_length_in_chain (vm, p0)
2694 > adj0[0].rewrite_header.max_l3_packet_bytes
2695 ? IP4_ERROR_MTU_EXCEEDED
2698 p0->error = error_node->errors[error0];
2699 p0->current_data -= rw_len0;
2700 p0->current_length += rw_len0;
2701 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
2702 adj0[0].rewrite_header.sw_if_index;
2704 next0 = (error0 == IP4_ERROR_NONE)
2705 ? adj0[0].rewrite_header.next_index : 0;
2707 if (rewrite_for_locally_received_packets)
2708 next0 = next0 && next0_override ? next0_override : next0;
2713 n_left_to_next -= 1;
2715 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2716 to_next, n_left_to_next,
2720 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2723 /* Need to do trace after rewrites to pick up new packet data. */
2724 if (node->flags & VLIB_NODE_FLAG_TRACE)
2725 ip4_forward_next_trace (vm, node, frame, adj_rx_tx);
2727 return frame->n_vectors;
2731 ip4_rewrite_transit (vlib_main_t * vm,
2732 vlib_node_runtime_t * node,
2733 vlib_frame_t * frame)
2735 return ip4_rewrite_inline (vm, node, frame,
2736 /* rewrite_for_locally_received_packets */ 0);
2740 ip4_rewrite_local (vlib_main_t * vm,
2741 vlib_node_runtime_t * node,
2742 vlib_frame_t * frame)
2744 return ip4_rewrite_inline (vm, node, frame,
2745 /* rewrite_for_locally_received_packets */ 1);
2748 VLIB_REGISTER_NODE (ip4_rewrite_node) = {
2749 .function = ip4_rewrite_transit,
2750 .name = "ip4-rewrite-transit",
2751 .vector_size = sizeof (u32),
2753 .format_trace = format_ip4_forward_next_trace,
2757 [IP4_REWRITE_NEXT_DROP] = "error-drop",
2758 [IP4_REWRITE_NEXT_ARP] = "ip4-arp",
2762 VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_node, ip4_rewrite_transit)
2764 VLIB_REGISTER_NODE (ip4_rewrite_local_node,static) = {
2765 .function = ip4_rewrite_local,
2766 .name = "ip4-rewrite-local",
2767 .vector_size = sizeof (u32),
2769 .sibling_of = "ip4-rewrite-transit",
2771 .format_trace = format_ip4_forward_next_trace,
2775 [IP4_REWRITE_NEXT_DROP] = "error-drop",
2776 [IP4_REWRITE_NEXT_ARP] = "ip4-arp",
2780 VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_local_node, ip4_rewrite_local)
2782 static clib_error_t *
2783 add_del_interface_table (vlib_main_t * vm,
2784 unformat_input_t * input,
2785 vlib_cli_command_t * cmd)
2787 vnet_main_t * vnm = vnet_get_main();
2788 clib_error_t * error = 0;
2789 u32 sw_if_index, table_id;
2793 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2795 error = clib_error_return (0, "unknown interface `%U'",
2796 format_unformat_error, input);
2800 if (unformat (input, "%d", &table_id))
2804 error = clib_error_return (0, "expected table id `%U'",
2805 format_unformat_error, input);
2810 ip4_main_t * im = &ip4_main;
2811 ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
2815 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2816 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2824 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2825 .path = "set interface ip table",
2826 .function = add_del_interface_table,
2827 .short_help = "Add/delete FIB table id for interface",
2832 ip4_lookup_multicast (vlib_main_t * vm,
2833 vlib_node_runtime_t * node,
2834 vlib_frame_t * frame)
2836 ip4_main_t * im = &ip4_main;
2837 ip_lookup_main_t * lm = &im->lookup_main;
2838 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
2839 u32 n_left_from, n_left_to_next, * from, * to_next;
2840 ip_lookup_next_t next;
2841 u32 cpu_index = os_get_cpu_number();
2843 from = vlib_frame_vector_args (frame);
2844 n_left_from = frame->n_vectors;
2845 next = node->cached_next_index;
2847 while (n_left_from > 0)
2849 vlib_get_next_frame (vm, node, next,
2850 to_next, n_left_to_next);
2852 while (n_left_from >= 4 && n_left_to_next >= 2)
2854 vlib_buffer_t * p0, * p1;
2855 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
2856 ip_lookup_next_t next0, next1;
2857 ip4_header_t * ip0, * ip1;
2858 ip_adjacency_t * adj0, * adj1;
2859 u32 fib_index0, fib_index1;
2860 u32 flow_hash_config0, flow_hash_config1;
2862 /* Prefetch next iteration. */
2864 vlib_buffer_t * p2, * p3;
2866 p2 = vlib_get_buffer (vm, from[2]);
2867 p3 = vlib_get_buffer (vm, from[3]);
2869 vlib_prefetch_buffer_header (p2, LOAD);
2870 vlib_prefetch_buffer_header (p3, LOAD);
2872 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
2873 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
2876 pi0 = to_next[0] = from[0];
2877 pi1 = to_next[1] = from[1];
2879 p0 = vlib_get_buffer (vm, pi0);
2880 p1 = vlib_get_buffer (vm, pi1);
2882 ip0 = vlib_buffer_get_current (p0);
2883 ip1 = vlib_buffer_get_current (p1);
2885 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
2886 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
2887 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
2888 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
2889 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
2890 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
2892 adj_index0 = ip4_fib_lookup_buffer (im, fib_index0,
2893 &ip0->dst_address, p0);
2894 adj_index1 = ip4_fib_lookup_buffer (im, fib_index1,
2895 &ip1->dst_address, p1);
2897 adj0 = ip_get_adjacency (lm, adj_index0);
2898 adj1 = ip_get_adjacency (lm, adj_index1);
2900 next0 = adj0->lookup_next_index;
2901 next1 = adj1->lookup_next_index;
2904 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
2907 vec_elt_at_index (im->fibs, fib_index1)->flow_hash_config;
2909 vnet_buffer (p0)->ip.flow_hash = ip4_compute_flow_hash
2910 (ip0, flow_hash_config0);
2912 vnet_buffer (p1)->ip.flow_hash = ip4_compute_flow_hash
2913 (ip1, flow_hash_config1);
2915 ASSERT (adj0->n_adj > 0);
2916 ASSERT (adj1->n_adj > 0);
2917 ASSERT (is_pow2 (adj0->n_adj));
2918 ASSERT (is_pow2 (adj1->n_adj));
2919 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
2920 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
2922 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
2923 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
2925 if (1) /* $$$$$$ HACK FIXME */
2926 vlib_increment_combined_counter
2927 (cm, cpu_index, adj_index0, 1,
2928 vlib_buffer_length_in_chain (vm, p0));
2929 if (1) /* $$$$$$ HACK FIXME */
2930 vlib_increment_combined_counter
2931 (cm, cpu_index, adj_index1, 1,
2932 vlib_buffer_length_in_chain (vm, p1));
2936 n_left_to_next -= 2;
2939 wrong_next = (next0 != next) + 2*(next1 != next);
2940 if (PREDICT_FALSE (wrong_next != 0))
2948 n_left_to_next += 1;
2949 vlib_set_next_frame_buffer (vm, node, next0, pi0);
2955 n_left_to_next += 1;
2956 vlib_set_next_frame_buffer (vm, node, next1, pi1);
2962 n_left_to_next += 2;
2963 vlib_set_next_frame_buffer (vm, node, next0, pi0);
2964 vlib_set_next_frame_buffer (vm, node, next1, pi1);
2968 vlib_put_next_frame (vm, node, next, n_left_to_next);
2970 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
2976 while (n_left_from > 0 && n_left_to_next > 0)
2980 u32 pi0, adj_index0;
2981 ip_lookup_next_t next0;
2982 ip_adjacency_t * adj0;
2984 u32 flow_hash_config0;
2989 p0 = vlib_get_buffer (vm, pi0);
2991 ip0 = vlib_buffer_get_current (p0);
2993 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
2994 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
2995 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
2996 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
2998 adj_index0 = ip4_fib_lookup_buffer (im, fib_index0,
2999 &ip0->dst_address, p0);
3001 adj0 = ip_get_adjacency (lm, adj_index0);
3003 next0 = adj0->lookup_next_index;
3006 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
3008 vnet_buffer (p0)->ip.flow_hash =
3009 ip4_compute_flow_hash (ip0, flow_hash_config0);
3011 ASSERT (adj0->n_adj > 0);
3012 ASSERT (is_pow2 (adj0->n_adj));
3013 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
3015 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
3017 if (1) /* $$$$$$ HACK FIXME */
3018 vlib_increment_combined_counter
3019 (cm, cpu_index, adj_index0, 1,
3020 vlib_buffer_length_in_chain (vm, p0));
3024 n_left_to_next -= 1;
3027 if (PREDICT_FALSE (next0 != next))
3029 n_left_to_next += 1;
3030 vlib_put_next_frame (vm, node, next, n_left_to_next);
3032 vlib_get_next_frame (vm, node, next,
3033 to_next, n_left_to_next);
3036 n_left_to_next -= 1;
3040 vlib_put_next_frame (vm, node, next, n_left_to_next);
3043 return frame->n_vectors;
3046 VLIB_REGISTER_NODE (ip4_lookup_multicast_node,static) = {
3047 .function = ip4_lookup_multicast,
3048 .name = "ip4-lookup-multicast",
3049 .vector_size = sizeof (u32),
3051 .n_next_nodes = IP_LOOKUP_N_NEXT,
3052 .next_nodes = IP4_LOOKUP_NEXT_NODES,
3055 VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_multicast_node, ip4_lookup_multicast)
3057 VLIB_REGISTER_NODE (ip4_multicast_node,static) = {
3058 .function = ip4_drop,
3059 .name = "ip4-multicast",
3060 .vector_size = sizeof (u32),
3062 .format_trace = format_ip4_forward_next_trace,
3070 int ip4_lookup_validate (ip4_address_t *a, u32 fib_index0)
3072 ip4_main_t * im = &ip4_main;
3073 ip4_fib_mtrie_t * mtrie0;
3074 ip4_fib_mtrie_leaf_t leaf0;
3077 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
3079 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
3080 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 0);
3081 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 1);
3082 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 2);
3083 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 3);
3085 /* Handle default route. */
3086 leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
3088 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
3090 return adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
3092 /* no_default_route */ 0);
3095 static clib_error_t *
3096 test_lookup_command_fn (vlib_main_t * vm,
3097 unformat_input_t * input,
3098 vlib_cli_command_t * cmd)
3104 ip4_address_t ip4_base_address;
3107 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3108 if (unformat (input, "table %d", &table_id))
3110 else if (unformat (input, "count %f", &count))
3113 else if (unformat (input, "%U",
3114 unformat_ip4_address, &ip4_base_address))
3117 return clib_error_return (0, "unknown input `%U'",
3118 format_unformat_error, input);
3123 for (i = 0; i < n; i++)
3125 if (!ip4_lookup_validate (&ip4_base_address, table_id))
3128 ip4_base_address.as_u32 =
3129 clib_host_to_net_u32 (1 +
3130 clib_net_to_host_u32 (ip4_base_address.as_u32));
3134 vlib_cli_output (vm, "%llu errors out of %d lookups\n", errors, n);
3136 vlib_cli_output (vm, "No errors in %d lookups\n", n);
3141 VLIB_CLI_COMMAND (lookup_test_command, static) = {
3142 .path = "test lookup",
3143 .short_help = "test lookup",
3144 .function = test_lookup_command_fn,
3147 int vnet_set_ip4_flow_hash (u32 table_id, u32 flow_hash_config)
3149 ip4_main_t * im4 = &ip4_main;
3151 uword * p = hash_get (im4->fib_index_by_table_id, table_id);
3154 return VNET_API_ERROR_NO_SUCH_FIB;
3156 fib = vec_elt_at_index (im4->fibs, p[0]);
3158 fib->flow_hash_config = flow_hash_config;
3162 static clib_error_t *
3163 set_ip_flow_hash_command_fn (vlib_main_t * vm,
3164 unformat_input_t * input,
3165 vlib_cli_command_t * cmd)
3169 u32 flow_hash_config = 0;
3172 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3173 if (unformat (input, "table %d", &table_id))
3176 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3177 foreach_flow_hash_bit
3183 return clib_error_return (0, "unknown input `%U'",
3184 format_unformat_error, input);
3186 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3192 case VNET_API_ERROR_NO_SUCH_FIB:
3193 return clib_error_return (0, "no such FIB table %d", table_id);
3196 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3203 VLIB_CLI_COMMAND (set_ip_flow_hash_command, static) = {
3204 .path = "set ip flow-hash",
3206 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
3207 .function = set_ip_flow_hash_command_fn,
3210 int vnet_set_ip4_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3213 vnet_main_t * vnm = vnet_get_main();
3214 vnet_interface_main_t * im = &vnm->interface_main;
3215 ip4_main_t * ipm = &ip4_main;
3216 ip_lookup_main_t * lm = &ipm->lookup_main;
3217 vnet_classify_main_t * cm = &vnet_classify_main;
3219 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3220 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3222 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3223 return VNET_API_ERROR_NO_SUCH_ENTRY;
3225 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3226 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
3231 static clib_error_t *
3232 set_ip_classify_command_fn (vlib_main_t * vm,
3233 unformat_input_t * input,
3234 vlib_cli_command_t * cmd)
3236 u32 table_index = ~0;
3237 int table_index_set = 0;
3238 u32 sw_if_index = ~0;
3241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3242 if (unformat (input, "table-index %d", &table_index))
3243 table_index_set = 1;
3244 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3245 vnet_get_main(), &sw_if_index))
3251 if (table_index_set == 0)
3252 return clib_error_return (0, "classify table-index must be specified");
3254 if (sw_if_index == ~0)
3255 return clib_error_return (0, "interface / subif must be specified");
3257 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3264 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3265 return clib_error_return (0, "No such interface");
3267 case VNET_API_ERROR_NO_SUCH_ENTRY:
3268 return clib_error_return (0, "No such classifier table");
3273 VLIB_CLI_COMMAND (set_ip_classify_command, static) = {
3274 .path = "set ip classify",
3276 "set ip classify intfc <int> table-index <index>",
3277 .function = set_ip_classify_command_fn,