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_forward_next_trace (vlib_main_t * vm,
637 vlib_node_runtime_t * node,
638 vlib_frame_t * frame,
639 vlib_rx_or_tx_t which_adj_index);
642 ip4_lookup_inline (vlib_main_t * vm,
643 vlib_node_runtime_t * node,
644 vlib_frame_t * frame,
645 int lookup_for_responses_to_locally_received_packets,
648 ip4_main_t * im = &ip4_main;
649 ip_lookup_main_t * lm = &im->lookup_main;
650 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
651 u32 n_left_from, n_left_to_next, * from, * to_next;
652 ip_lookup_next_t next;
653 u32 cpu_index = os_get_cpu_number();
655 from = vlib_frame_vector_args (frame);
656 n_left_from = frame->n_vectors;
657 next = node->cached_next_index;
659 while (n_left_from > 0)
661 vlib_get_next_frame (vm, node, next,
662 to_next, n_left_to_next);
664 while (n_left_from >= 4 && n_left_to_next >= 2)
666 vlib_buffer_t * p0, * p1;
667 ip4_header_t * ip0, * ip1;
668 __attribute__((unused)) tcp_header_t * tcp0, * tcp1;
669 ip_lookup_next_t next0, next1;
670 ip_adjacency_t * adj0, * adj1;
671 ip4_fib_mtrie_t * mtrie0, * mtrie1;
672 ip4_fib_mtrie_leaf_t leaf0, leaf1;
673 ip4_address_t * dst_addr0, *dst_addr1;
674 __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
675 __attribute__((unused)) u32 pi1, fib_index1, adj_index1, is_tcp_udp1;
676 u32 flow_hash_config0, flow_hash_config1;
677 u32 hash_c0, hash_c1;
680 /* Prefetch next iteration. */
682 vlib_buffer_t * p2, * p3;
684 p2 = vlib_get_buffer (vm, from[2]);
685 p3 = vlib_get_buffer (vm, from[3]);
687 vlib_prefetch_buffer_header (p2, LOAD);
688 vlib_prefetch_buffer_header (p3, LOAD);
690 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
691 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
694 pi0 = to_next[0] = from[0];
695 pi1 = to_next[1] = from[1];
697 p0 = vlib_get_buffer (vm, pi0);
698 p1 = vlib_get_buffer (vm, pi1);
700 ip0 = vlib_buffer_get_current (p0);
701 ip1 = vlib_buffer_get_current (p1);
705 ip_adjacency_t * iadj0, * iadj1;
706 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
707 iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
708 dst_addr0 = &iadj0->indirect.next_hop.ip4;
709 dst_addr1 = &iadj1->indirect.next_hop.ip4;
713 dst_addr0 = &ip0->dst_address;
714 dst_addr1 = &ip1->dst_address;
717 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
718 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
719 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
720 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
721 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
722 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
725 if (! lookup_for_responses_to_locally_received_packets)
727 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
728 mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie;
730 leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
732 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
733 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 0);
736 tcp0 = (void *) (ip0 + 1);
737 tcp1 = (void *) (ip1 + 1);
739 is_tcp_udp0 = (ip0->protocol == IP_PROTOCOL_TCP
740 || ip0->protocol == IP_PROTOCOL_UDP);
741 is_tcp_udp1 = (ip1->protocol == IP_PROTOCOL_TCP
742 || ip1->protocol == IP_PROTOCOL_UDP);
744 if (! lookup_for_responses_to_locally_received_packets)
746 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
747 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 1);
750 if (! lookup_for_responses_to_locally_received_packets)
752 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
753 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 2);
756 if (! lookup_for_responses_to_locally_received_packets)
758 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
759 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 3);
762 if (lookup_for_responses_to_locally_received_packets)
764 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
765 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_RX];
769 /* Handle default route. */
770 leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
771 leaf1 = (leaf1 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie1->default_leaf : leaf1);
773 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
774 adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
777 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
779 /* no_default_route */ 0));
780 ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
782 /* no_default_route */ 0));
783 adj0 = ip_get_adjacency (lm, adj_index0);
784 adj1 = ip_get_adjacency (lm, adj_index1);
786 next0 = adj0->lookup_next_index;
787 next1 = adj1->lookup_next_index;
789 /* Use flow hash to compute multipath adjacency. */
790 hash_c0 = vnet_buffer (p0)->ip.flow_hash = 0;
791 hash_c1 = vnet_buffer (p1)->ip.flow_hash = 0;
792 if (PREDICT_FALSE (adj0->n_adj > 1))
795 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
796 hash_c0 = vnet_buffer (p0)->ip.flow_hash =
797 ip4_compute_flow_hash (ip0, flow_hash_config0);
799 if (PREDICT_FALSE(adj1->n_adj > 1))
802 vec_elt_at_index (im->fibs, fib_index1)->flow_hash_config;
803 hash_c1 = vnet_buffer (p1)->ip.flow_hash =
804 ip4_compute_flow_hash (ip1, flow_hash_config1);
807 ASSERT (adj0->n_adj > 0);
808 ASSERT (adj1->n_adj > 0);
809 ASSERT (is_pow2 (adj0->n_adj));
810 ASSERT (is_pow2 (adj1->n_adj));
811 adj_index0 += (hash_c0 & (adj0->n_adj - 1));
812 adj_index1 += (hash_c1 & (adj1->n_adj - 1));
814 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
815 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
817 vlib_increment_combined_counter
818 (cm, cpu_index, adj_index0, 1,
819 vlib_buffer_length_in_chain (vm, p0)
820 + sizeof(ethernet_header_t));
821 vlib_increment_combined_counter
822 (cm, cpu_index, adj_index1, 1,
823 vlib_buffer_length_in_chain (vm, p1)
824 + sizeof(ethernet_header_t));
831 wrong_next = (next0 != next) + 2*(next1 != next);
832 if (PREDICT_FALSE (wrong_next != 0))
841 vlib_set_next_frame_buffer (vm, node, next0, pi0);
848 vlib_set_next_frame_buffer (vm, node, next1, pi1);
855 vlib_set_next_frame_buffer (vm, node, next0, pi0);
856 vlib_set_next_frame_buffer (vm, node, next1, pi1);
860 vlib_put_next_frame (vm, node, next, n_left_to_next);
862 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
868 while (n_left_from > 0 && n_left_to_next > 0)
872 __attribute__((unused)) tcp_header_t * tcp0;
873 ip_lookup_next_t next0;
874 ip_adjacency_t * adj0;
875 ip4_fib_mtrie_t * mtrie0;
876 ip4_fib_mtrie_leaf_t leaf0;
877 ip4_address_t * dst_addr0;
878 __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
879 u32 flow_hash_config0, hash_c0;
884 p0 = vlib_get_buffer (vm, pi0);
886 ip0 = vlib_buffer_get_current (p0);
890 ip_adjacency_t * iadj0;
891 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
892 dst_addr0 = &iadj0->indirect.next_hop.ip4;
896 dst_addr0 = &ip0->dst_address;
899 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
900 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
901 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
903 if (! lookup_for_responses_to_locally_received_packets)
905 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
907 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
909 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
912 tcp0 = (void *) (ip0 + 1);
914 is_tcp_udp0 = (ip0->protocol == IP_PROTOCOL_TCP
915 || ip0->protocol == IP_PROTOCOL_UDP);
917 if (! lookup_for_responses_to_locally_received_packets)
918 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
920 if (! lookup_for_responses_to_locally_received_packets)
921 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
923 if (! lookup_for_responses_to_locally_received_packets)
924 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
926 if (lookup_for_responses_to_locally_received_packets)
927 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
930 /* Handle default route. */
931 leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
932 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
935 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
937 /* no_default_route */ 0));
939 adj0 = ip_get_adjacency (lm, adj_index0);
941 next0 = adj0->lookup_next_index;
943 /* Use flow hash to compute multipath adjacency. */
944 hash_c0 = vnet_buffer (p0)->ip.flow_hash = 0;
945 if (PREDICT_FALSE(adj0->n_adj > 1))
948 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
950 hash_c0 = vnet_buffer (p0)->ip.flow_hash =
951 ip4_compute_flow_hash (ip0, flow_hash_config0);
954 ASSERT (adj0->n_adj > 0);
955 ASSERT (is_pow2 (adj0->n_adj));
956 adj_index0 += (hash_c0 & (adj0->n_adj - 1));
958 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
960 vlib_increment_combined_counter
961 (cm, cpu_index, adj_index0, 1,
962 vlib_buffer_length_in_chain (vm, p0)
963 + sizeof(ethernet_header_t));
970 if (PREDICT_FALSE (next0 != next))
973 vlib_put_next_frame (vm, node, next, n_left_to_next);
975 vlib_get_next_frame (vm, node, next,
976 to_next, n_left_to_next);
983 vlib_put_next_frame (vm, node, next, n_left_to_next);
986 if (node->flags & VLIB_NODE_FLAG_TRACE)
987 ip4_forward_next_trace(vm, node, frame, VLIB_TX);
989 return frame->n_vectors;
993 ip4_lookup (vlib_main_t * vm,
994 vlib_node_runtime_t * node,
995 vlib_frame_t * frame)
997 return ip4_lookup_inline (vm, node, frame,
998 /* lookup_for_responses_to_locally_received_packets */ 0,
999 /* is_indirect */ 0);
1003 void ip4_adjacency_set_interface_route (vnet_main_t * vnm,
1004 ip_adjacency_t * adj,
1006 u32 if_address_index)
1008 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1010 vnet_l3_packet_type_t packet_type;
1013 if (hw->hw_class_index == ethernet_hw_interface_class.index
1014 || hw->hw_class_index == srp_hw_interface_class.index)
1017 * We have a bit of a problem in this case. ip4-arp uses
1018 * the rewrite_header.next_index to hand pkts to the
1019 * indicated inteface output node. We can end up in
1020 * ip4_rewrite_local, too, which also pays attention to
1021 * rewrite_header.next index. Net result: a hack in
1022 * ip4_rewrite_local...
1024 n = IP_LOOKUP_NEXT_ARP;
1025 node_index = ip4_arp_node.index;
1026 adj->if_address_index = if_address_index;
1027 adj->arp.next_hop.ip4.as_u32 = 0;
1028 ip46_address_reset(&adj->arp.next_hop);
1029 packet_type = VNET_L3_PACKET_TYPE_ARP;
1033 n = IP_LOOKUP_NEXT_REWRITE;
1034 node_index = ip4_rewrite_node.index;
1035 packet_type = VNET_L3_PACKET_TYPE_IP4;
1038 adj->lookup_next_index = n;
1039 vnet_rewrite_for_sw_interface
1044 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
1045 &adj->rewrite_header,
1046 sizeof (adj->rewrite_data));
1050 ip4_add_interface_routes (u32 sw_if_index,
1051 ip4_main_t * im, u32 fib_index,
1052 ip_interface_address_t * a)
1054 vnet_main_t * vnm = vnet_get_main();
1055 ip_lookup_main_t * lm = &im->lookup_main;
1056 ip_adjacency_t * adj;
1057 ip4_address_t * address = ip_interface_address_get_address (lm, a);
1058 ip4_add_del_route_args_t x;
1059 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1060 u32 classify_table_index;
1062 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1063 x.table_index_or_table_id = fib_index;
1064 x.flags = (IP4_ROUTE_FLAG_ADD
1065 | IP4_ROUTE_FLAG_FIB_INDEX
1066 | IP4_ROUTE_FLAG_NO_REDISTRIBUTE);
1067 x.dst_address = address[0];
1068 x.dst_address_length = a->address_length;
1072 a->neighbor_probe_adj_index = ~0;
1073 if (a->address_length < 32)
1075 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1077 ip4_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
1078 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1079 ip4_add_del_route (im, &x);
1080 a->neighbor_probe_adj_index = x.adj_index;
1083 /* Add e.g. 1.1.1.1/32 as local to this host. */
1084 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1087 classify_table_index = ~0;
1088 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1089 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1090 if (classify_table_index != (u32) ~0)
1092 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1093 adj->classify.table_index = classify_table_index;
1096 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1098 adj->if_address_index = a - lm->if_address_pool;
1099 adj->rewrite_header.sw_if_index = sw_if_index;
1100 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1102 * Local adjs are never to be rewritten. Spoofed pkts w/ src = dst = local
1103 * fail an RPF-ish check, but still go thru the rewrite code...
1105 adj->rewrite_header.data_bytes = 0;
1107 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1108 x.dst_address_length = 32;
1109 ip4_add_del_route (im, &x);
1113 ip4_del_interface_routes (ip4_main_t * im, u32 fib_index, ip4_address_t * address, u32 address_length)
1115 ip4_add_del_route_args_t x;
1117 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1118 x.table_index_or_table_id = fib_index;
1119 x.flags = (IP4_ROUTE_FLAG_DEL
1120 | IP4_ROUTE_FLAG_FIB_INDEX
1121 | IP4_ROUTE_FLAG_NO_REDISTRIBUTE);
1122 x.dst_address = address[0];
1123 x.dst_address_length = address_length;
1128 if (address_length < 32)
1129 ip4_add_del_route (im, &x);
1131 x.dst_address_length = 32;
1132 ip4_add_del_route (im, &x);
1134 ip4_delete_matching_routes (im,
1136 IP4_ROUTE_FLAG_FIB_INDEX,
1143 ip4_address_t address;
1145 } ip4_interface_address_t;
1147 static clib_error_t *
1148 ip4_add_del_interface_address_internal (vlib_main_t * vm,
1150 ip4_address_t * new_address,
1156 static clib_error_t *
1157 ip4_add_del_interface_address_internal (vlib_main_t * vm,
1159 ip4_address_t * address,
1165 vnet_main_t * vnm = vnet_get_main();
1166 ip4_main_t * im = &ip4_main;
1167 ip_lookup_main_t * lm = &im->lookup_main;
1168 clib_error_t * error = 0;
1169 u32 if_address_index, elts_before;
1170 ip4_address_fib_t ip4_af, * addr_fib = 0;
1172 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1173 ip4_addr_fib_init (&ip4_af, address,
1174 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1175 vec_add1 (addr_fib, ip4_af);
1177 /* When adding an address check that it does not conflict with an existing address. */
1180 ip_interface_address_t * ia;
1181 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1182 0 /* honor unnumbered */,
1184 ip4_address_t * x = ip_interface_address_get_address (&im->lookup_main, ia);
1186 if (ip4_destination_matches_route (im, address, x, ia->address_length)
1187 || ip4_destination_matches_route (im, x, address, address_length))
1188 return clib_error_create ("failed to add %U which conflicts with %U for interface %U",
1189 format_ip4_address_and_length, address, address_length,
1190 format_ip4_address_and_length, x, ia->address_length,
1191 format_vnet_sw_if_index_name, vnm, sw_if_index);
1195 elts_before = pool_elts (lm->if_address_pool);
1197 error = ip_interface_address_add_del
1207 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1210 ip4_del_interface_routes (im, ip4_af.fib_index, address,
1214 ip4_add_interface_routes (sw_if_index,
1215 im, ip4_af.fib_index,
1217 (lm->if_address_pool, if_address_index));
1220 /* If pool did not grow/shrink: add duplicate address. */
1221 if (elts_before != pool_elts (lm->if_address_pool))
1223 ip4_add_del_interface_address_callback_t * cb;
1224 vec_foreach (cb, im->add_del_interface_address_callbacks)
1225 cb->function (im, cb->function_opaque, sw_if_index,
1226 address, address_length,
1232 vec_free (addr_fib);
1237 ip4_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1238 ip4_address_t * address, u32 address_length,
1241 return ip4_add_del_interface_address_internal
1242 (vm, sw_if_index, address, address_length,
1243 /* redistribute */ 1,
1244 /* insert_routes */ 1,
1248 static clib_error_t *
1249 ip4_sw_interface_admin_up_down (vnet_main_t * vnm,
1253 ip4_main_t * im = &ip4_main;
1254 ip_interface_address_t * ia;
1256 u32 is_admin_up, fib_index;
1258 /* Fill in lookup tables with default table (0). */
1259 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1261 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1263 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1265 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1267 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1268 0 /* honor unnumbered */,
1270 a = ip_interface_address_get_address (&im->lookup_main, ia);
1272 ip4_add_interface_routes (sw_if_index,
1276 ip4_del_interface_routes (im, fib_index,
1277 a, ia->address_length);
1283 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip4_sw_interface_admin_up_down);
1285 static clib_error_t *
1286 ip4_sw_interface_add_del (vnet_main_t * vnm,
1290 vlib_main_t * vm = vnm->vlib_main;
1291 ip4_main_t * im = &ip4_main;
1292 ip_lookup_main_t * lm = &im->lookup_main;
1295 for (cast = 0; cast < VNET_N_CAST; cast++)
1297 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1298 vnet_config_main_t * vcm = &cm->config_main;
1300 if (! vcm->node_index_by_feature_index)
1302 if (cast == VNET_UNICAST)
1304 static char * start_nodes[] = { "ip4-input", "ip4-input-no-checksum", };
1305 static char * feature_nodes[] = {
1306 [IP4_RX_FEATURE_CHECK_ACCESS] = "ip4-inacl",
1307 [IP4_RX_FEATURE_SOURCE_CHECK_REACHABLE_VIA_RX] = "ip4-source-check-via-rx",
1308 [IP4_RX_FEATURE_SOURCE_CHECK_REACHABLE_VIA_ANY] = "ip4-source-check-via-any",
1309 [IP4_RX_FEATURE_IPSEC] = "ipsec-input-ip4",
1310 [IP4_RX_FEATURE_VPATH] = "vpath-input-ip4",
1311 [IP4_RX_FEATURE_LOOKUP] = "ip4-lookup",
1314 vnet_config_init (vm, vcm,
1315 start_nodes, ARRAY_LEN (start_nodes),
1316 feature_nodes, ARRAY_LEN (feature_nodes));
1320 static char * start_nodes[] = { "ip4-input", "ip4-input-no-checksum", };
1321 static char * feature_nodes[] = {
1322 [IP4_RX_FEATURE_VPATH] = "vpath-input-ip4",
1323 [IP4_RX_FEATURE_LOOKUP] = "ip4-lookup-multicast",
1326 vnet_config_init (vm, vcm,
1327 start_nodes, ARRAY_LEN (start_nodes),
1328 feature_nodes, ARRAY_LEN (feature_nodes));
1332 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1333 ci = cm->config_index_by_sw_if_index[sw_if_index];
1336 ci = vnet_config_add_feature (vm, vcm,
1338 IP4_RX_FEATURE_LOOKUP,
1339 /* config data */ 0,
1340 /* # bytes of config data */ 0);
1342 ci = vnet_config_del_feature (vm, vcm,
1344 IP4_RX_FEATURE_LOOKUP,
1345 /* config data */ 0,
1346 /* # bytes of config data */ 0);
1348 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1351 return /* no error */ 0;
1354 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip4_sw_interface_add_del);
1356 static u8 * format_ip4_lookup_trace (u8 * s, va_list * args);
1358 VLIB_REGISTER_NODE (ip4_lookup_node) = {
1359 .function = ip4_lookup,
1360 .name = "ip4-lookup",
1361 .vector_size = sizeof (u32),
1363 .format_trace = format_ip4_lookup_trace,
1365 .n_next_nodes = IP_LOOKUP_N_NEXT,
1366 .next_nodes = IP4_LOOKUP_NEXT_NODES,
1369 VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_node, ip4_lookup)
1372 ip4_indirect (vlib_main_t * vm,
1373 vlib_node_runtime_t * node,
1374 vlib_frame_t * frame)
1376 return ip4_lookup_inline (vm, node, frame,
1377 /* lookup_for_responses_to_locally_received_packets */ 0,
1378 /* is_indirect */ 1);
1381 VLIB_REGISTER_NODE (ip4_indirect_node) = {
1382 .function = ip4_indirect,
1383 .name = "ip4-indirect",
1384 .vector_size = sizeof (u32),
1386 .format_trace = format_ip4_lookup_trace,
1388 .n_next_nodes = IP_LOOKUP_N_NEXT,
1389 .next_nodes = IP4_LOOKUP_NEXT_NODES,
1392 VLIB_NODE_FUNCTION_MULTIARCH (ip4_indirect_node, ip4_indirect)
1395 /* Global IP4 main. */
1396 ip4_main_t ip4_main;
1399 ip4_lookup_init (vlib_main_t * vm)
1401 ip4_main_t * im = &ip4_main;
1404 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
1409 m = pow2_mask (i) << (32 - i);
1412 im->fib_masks[i] = clib_host_to_net_u32 (m);
1415 /* Create FIB with index 0 and table id of 0. */
1416 find_ip4_fib_by_table_index_or_id (im, /* table id */ 0, IP4_ROUTE_FLAG_TABLE_ID);
1418 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 0);
1422 pn = pg_get_node (ip4_lookup_node.index);
1423 pn->unformat_edit = unformat_pg_ip4_header;
1427 ethernet_arp_header_t h;
1429 memset (&h, 0, sizeof (h));
1431 /* Set target ethernet address to all zeros. */
1432 memset (h.ip4_over_ethernet[1].ethernet, 0, sizeof (h.ip4_over_ethernet[1].ethernet));
1434 #define _16(f,v) h.f = clib_host_to_net_u16 (v);
1435 #define _8(f,v) h.f = v;
1436 _16 (l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet);
1437 _16 (l3_type, ETHERNET_TYPE_IP4);
1438 _8 (n_l2_address_bytes, 6);
1439 _8 (n_l3_address_bytes, 4);
1440 _16 (opcode, ETHERNET_ARP_OPCODE_request);
1444 vlib_packet_template_init (vm,
1445 &im->ip4_arp_request_packet_template,
1448 /* alloc chunk size */ 8,
1455 VLIB_INIT_FUNCTION (ip4_lookup_init);
1458 /* Adjacency taken. */
1463 /* Packet data, possibly *after* rewrite. */
1464 u8 packet_data[64 - 1*sizeof(u32)];
1465 } ip4_forward_next_trace_t;
1467 static u8 * format_ip4_forward_next_trace (u8 * s, va_list * args)
1469 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1470 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1471 ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1472 uword indent = format_get_indent (s);
1473 s = format (s, "%U%U",
1474 format_white_space, indent,
1475 format_ip4_header, t->packet_data);
1479 static u8 * format_ip4_lookup_trace (u8 * s, va_list * args)
1481 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1482 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1483 ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1484 vnet_main_t * vnm = vnet_get_main();
1485 ip4_main_t * im = &ip4_main;
1486 uword indent = format_get_indent (s);
1488 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1489 t->fib_index, t->adj_index, format_ip_adjacency,
1490 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1491 s = format (s, "\n%U%U",
1492 format_white_space, indent,
1493 format_ip4_header, t->packet_data);
1497 static u8 * format_ip4_rewrite_trace (u8 * s, va_list * args)
1499 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1500 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1501 ip4_forward_next_trace_t * t = va_arg (*args, ip4_forward_next_trace_t *);
1502 vnet_main_t * vnm = vnet_get_main();
1503 ip4_main_t * im = &ip4_main;
1504 uword indent = format_get_indent (s);
1506 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1507 t->fib_index, t->adj_index, format_ip_adjacency,
1508 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1509 s = format (s, "\n%U%U",
1510 format_white_space, indent,
1511 format_ip_adjacency_packet_data,
1512 vnm, &im->lookup_main, t->adj_index,
1513 t->packet_data, sizeof (t->packet_data));
1517 /* Common trace function for all ip4-forward next nodes. */
1519 ip4_forward_next_trace (vlib_main_t * vm,
1520 vlib_node_runtime_t * node,
1521 vlib_frame_t * frame,
1522 vlib_rx_or_tx_t which_adj_index)
1525 ip4_main_t * im = &ip4_main;
1527 n_left = frame->n_vectors;
1528 from = vlib_frame_vector_args (frame);
1533 vlib_buffer_t * b0, * b1;
1534 ip4_forward_next_trace_t * t0, * t1;
1536 /* Prefetch next iteration. */
1537 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1538 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1543 b0 = vlib_get_buffer (vm, bi0);
1544 b1 = vlib_get_buffer (vm, bi1);
1546 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1548 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1549 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1550 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1551 t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1552 vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1553 vec_elt (im->fib_index_by_sw_if_index,
1554 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));
1560 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1562 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1563 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1564 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1565 t1->fib_index = (vnet_buffer(b1)->sw_if_index[VLIB_TX] != (u32)~0) ?
1566 vnet_buffer(b1)->sw_if_index[VLIB_TX] :
1567 vec_elt (im->fib_index_by_sw_if_index,
1568 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1569 clib_memcpy (t1->packet_data,
1570 vlib_buffer_get_current (b1),
1571 sizeof (t1->packet_data));
1581 ip4_forward_next_trace_t * t0;
1585 b0 = vlib_get_buffer (vm, bi0);
1587 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1589 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1590 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1591 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1592 t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1593 vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1594 vec_elt (im->fib_index_by_sw_if_index,
1595 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1596 clib_memcpy (t0->packet_data,
1597 vlib_buffer_get_current (b0),
1598 sizeof (t0->packet_data));
1606 ip4_drop_or_punt (vlib_main_t * vm,
1607 vlib_node_runtime_t * node,
1608 vlib_frame_t * frame,
1609 ip4_error_t error_code)
1611 u32 * buffers = vlib_frame_vector_args (frame);
1612 uword n_packets = frame->n_vectors;
1614 vlib_error_drop_buffers (vm, node,
1619 ip4_input_node.index,
1622 if (node->flags & VLIB_NODE_FLAG_TRACE)
1623 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
1629 ip4_drop (vlib_main_t * vm,
1630 vlib_node_runtime_t * node,
1631 vlib_frame_t * frame)
1632 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_ADJACENCY_DROP); }
1635 ip4_punt (vlib_main_t * vm,
1636 vlib_node_runtime_t * node,
1637 vlib_frame_t * frame)
1638 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_ADJACENCY_PUNT); }
1641 ip4_miss (vlib_main_t * vm,
1642 vlib_node_runtime_t * node,
1643 vlib_frame_t * frame)
1644 { return ip4_drop_or_punt (vm, node, frame, IP4_ERROR_DST_LOOKUP_MISS); }
1646 VLIB_REGISTER_NODE (ip4_drop_node,static) = {
1647 .function = ip4_drop,
1649 .vector_size = sizeof (u32),
1651 .format_trace = format_ip4_forward_next_trace,
1659 VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop)
1661 VLIB_REGISTER_NODE (ip4_punt_node,static) = {
1662 .function = ip4_punt,
1664 .vector_size = sizeof (u32),
1666 .format_trace = format_ip4_forward_next_trace,
1674 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_node, ip4_punt)
1676 VLIB_REGISTER_NODE (ip4_miss_node,static) = {
1677 .function = ip4_miss,
1679 .vector_size = sizeof (u32),
1681 .format_trace = format_ip4_forward_next_trace,
1689 VLIB_NODE_FUNCTION_MULTIARCH (ip4_miss_node, ip4_miss)
1691 /* Compute TCP/UDP/ICMP4 checksum in software. */
1693 ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1697 u32 ip_header_length, payload_length_host_byte_order;
1698 u32 n_this_buffer, n_bytes_left;
1700 void * data_this_buffer;
1702 /* Initialize checksum with ip header. */
1703 ip_header_length = ip4_header_bytes (ip0);
1704 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->length) - ip_header_length;
1705 sum0 = clib_host_to_net_u32 (payload_length_host_byte_order + (ip0->protocol << 16));
1707 if (BITS (uword) == 32)
1709 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u32));
1710 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->dst_address, u32));
1713 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u64));
1715 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1716 data_this_buffer = (void *) ip0 + ip_header_length;
1717 if (n_this_buffer + ip_header_length > p0->current_length)
1718 n_this_buffer = p0->current_length > ip_header_length ? p0->current_length - ip_header_length : 0;
1721 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1722 n_bytes_left -= n_this_buffer;
1723 if (n_bytes_left == 0)
1726 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
1727 p0 = vlib_get_buffer (vm, p0->next_buffer);
1728 data_this_buffer = vlib_buffer_get_current (p0);
1729 n_this_buffer = p0->current_length;
1732 sum16 = ~ ip_csum_fold (sum0);
1738 ip4_tcp_udp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1740 ip4_header_t * ip0 = vlib_buffer_get_current (p0);
1741 udp_header_t * udp0;
1744 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1745 || ip0->protocol == IP_PROTOCOL_UDP);
1747 udp0 = (void *) (ip0 + 1);
1748 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1750 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1751 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1755 sum16 = ip4_tcp_udp_compute_checksum (vm, p0, ip0);
1757 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1758 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1764 ip4_local (vlib_main_t * vm,
1765 vlib_node_runtime_t * node,
1766 vlib_frame_t * frame)
1768 ip4_main_t * im = &ip4_main;
1769 ip_lookup_main_t * lm = &im->lookup_main;
1770 ip_local_next_t next_index;
1771 u32 * from, * to_next, n_left_from, n_left_to_next;
1772 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
1774 from = vlib_frame_vector_args (frame);
1775 n_left_from = frame->n_vectors;
1776 next_index = node->cached_next_index;
1778 if (node->flags & VLIB_NODE_FLAG_TRACE)
1779 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
1781 while (n_left_from > 0)
1783 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1785 while (n_left_from >= 4 && n_left_to_next >= 2)
1787 vlib_buffer_t * p0, * p1;
1788 ip4_header_t * ip0, * ip1;
1789 udp_header_t * udp0, * udp1;
1790 ip4_fib_mtrie_t * mtrie0, * mtrie1;
1791 ip4_fib_mtrie_leaf_t leaf0, leaf1;
1792 ip_adjacency_t * adj0, * adj1;
1793 u32 pi0, ip_len0, udp_len0, flags0, next0, fib_index0, adj_index0;
1794 u32 pi1, ip_len1, udp_len1, flags1, next1, fib_index1, adj_index1;
1795 i32 len_diff0, len_diff1;
1796 u8 error0, is_udp0, is_tcp_udp0, good_tcp_udp0, proto0;
1797 u8 error1, is_udp1, is_tcp_udp1, good_tcp_udp1, proto1;
1800 pi0 = to_next[0] = from[0];
1801 pi1 = to_next[1] = from[1];
1805 n_left_to_next -= 2;
1807 p0 = vlib_get_buffer (vm, pi0);
1808 p1 = vlib_get_buffer (vm, pi1);
1810 ip0 = vlib_buffer_get_current (p0);
1811 ip1 = vlib_buffer_get_current (p1);
1813 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
1814 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
1815 fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
1816 vnet_buffer(p1)->sw_if_index[VLIB_RX]);
1818 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
1819 mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie;
1821 leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
1823 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
1824 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 0);
1826 /* Treat IP frag packets as "experimental" protocol for now
1827 until support of IP frag reassembly is implemented */
1828 proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
1829 proto1 = ip4_is_fragment(ip1) ? 0xfe : ip1->protocol;
1830 is_udp0 = proto0 == IP_PROTOCOL_UDP;
1831 is_udp1 = proto1 == IP_PROTOCOL_UDP;
1832 is_tcp_udp0 = is_udp0 || proto0 == IP_PROTOCOL_TCP;
1833 is_tcp_udp1 = is_udp1 || proto1 == IP_PROTOCOL_TCP;
1838 good_tcp_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1839 good_tcp_udp1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1841 udp0 = ip4_next_header (ip0);
1842 udp1 = ip4_next_header (ip1);
1844 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1845 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1846 good_tcp_udp1 |= is_udp1 && udp1->checksum == 0;
1848 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
1849 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 1);
1851 /* Verify UDP length. */
1852 ip_len0 = clib_net_to_host_u16 (ip0->length);
1853 ip_len1 = clib_net_to_host_u16 (ip1->length);
1854 udp_len0 = clib_net_to_host_u16 (udp0->length);
1855 udp_len1 = clib_net_to_host_u16 (udp1->length);
1857 len_diff0 = ip_len0 - udp_len0;
1858 len_diff1 = ip_len1 - udp_len1;
1860 len_diff0 = is_udp0 ? len_diff0 : 0;
1861 len_diff1 = is_udp1 ? len_diff1 : 0;
1863 if (PREDICT_FALSE (! (is_tcp_udp0 & is_tcp_udp1
1864 & good_tcp_udp0 & good_tcp_udp1)))
1869 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
1870 flags0 = ip4_tcp_udp_validate_checksum (vm, p0);
1872 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1873 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
1878 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
1879 flags1 = ip4_tcp_udp_validate_checksum (vm, p1);
1881 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1882 good_tcp_udp1 |= is_udp1 && udp1->checksum == 0;
1886 good_tcp_udp0 &= len_diff0 >= 0;
1887 good_tcp_udp1 &= len_diff1 >= 0;
1889 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
1890 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2);
1892 error0 = error1 = IP4_ERROR_UNKNOWN_PROTOCOL;
1894 error0 = len_diff0 < 0 ? IP4_ERROR_UDP_LENGTH : error0;
1895 error1 = len_diff1 < 0 ? IP4_ERROR_UDP_LENGTH : error1;
1897 ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM);
1898 error0 = (is_tcp_udp0 && ! good_tcp_udp0
1899 ? IP4_ERROR_TCP_CHECKSUM + is_udp0
1901 error1 = (is_tcp_udp1 && ! good_tcp_udp1
1902 ? IP4_ERROR_TCP_CHECKSUM + is_udp1
1905 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
1906 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3);
1908 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
1909 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
1911 vnet_buffer (p1)->ip.adj_index[VLIB_RX] = adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
1912 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
1914 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
1916 /* no_default_route */ 1));
1917 ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
1919 /* no_default_route */ 1));
1921 adj0 = ip_get_adjacency (lm, adj_index0);
1922 adj1 = ip_get_adjacency (lm, adj_index1);
1925 * Must have a route to source otherwise we drop the packet.
1926 * ip4 broadcasts are accepted, e.g. to make dhcp client work
1928 error0 = (error0 == IP4_ERROR_UNKNOWN_PROTOCOL
1929 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
1930 && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
1931 && adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
1932 && ip0->dst_address.as_u32 != 0xFFFFFFFF
1933 ? IP4_ERROR_SRC_LOOKUP_MISS
1935 error1 = (error1 == IP4_ERROR_UNKNOWN_PROTOCOL
1936 && adj1->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
1937 && adj1->lookup_next_index != IP_LOOKUP_NEXT_ARP
1938 && adj1->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
1939 && ip0->dst_address.as_u32 != 0xFFFFFFFF
1940 ? IP4_ERROR_SRC_LOOKUP_MISS
1943 next0 = lm->local_next_by_ip_protocol[proto0];
1944 next1 = lm->local_next_by_ip_protocol[proto1];
1946 next0 = error0 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1947 next1 = error1 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1949 p0->error = error0 ? error_node->errors[error0] : 0;
1950 p1->error = error1 ? error_node->errors[error1] : 0;
1952 enqueue_code = (next0 != next_index) + 2*(next1 != next_index);
1954 if (PREDICT_FALSE (enqueue_code != 0))
1956 switch (enqueue_code)
1962 n_left_to_next += 1;
1963 vlib_set_next_frame_buffer (vm, node, next0, pi0);
1969 n_left_to_next += 1;
1970 vlib_set_next_frame_buffer (vm, node, next1, pi1);
1974 /* A B B or A B C */
1976 n_left_to_next += 2;
1977 vlib_set_next_frame_buffer (vm, node, next0, pi0);
1978 vlib_set_next_frame_buffer (vm, node, next1, pi1);
1981 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1983 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1990 while (n_left_from > 0 && n_left_to_next > 0)
1994 udp_header_t * udp0;
1995 ip4_fib_mtrie_t * mtrie0;
1996 ip4_fib_mtrie_leaf_t leaf0;
1997 ip_adjacency_t * adj0;
1998 u32 pi0, next0, ip_len0, udp_len0, flags0, fib_index0, adj_index0;
2000 u8 error0, is_udp0, is_tcp_udp0, good_tcp_udp0, proto0;
2002 pi0 = to_next[0] = from[0];
2006 n_left_to_next -= 1;
2008 p0 = vlib_get_buffer (vm, pi0);
2010 ip0 = vlib_buffer_get_current (p0);
2012 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
2013 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
2015 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
2017 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
2019 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
2021 /* Treat IP frag packets as "experimental" protocol for now
2022 until support of IP frag reassembly is implemented */
2023 proto0 = ip4_is_fragment(ip0) ? 0xfe : ip0->protocol;
2024 is_udp0 = proto0 == IP_PROTOCOL_UDP;
2025 is_tcp_udp0 = is_udp0 || proto0 == IP_PROTOCOL_TCP;
2029 good_tcp_udp0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
2031 udp0 = ip4_next_header (ip0);
2033 /* Don't verify UDP checksum for packets with explicit zero checksum. */
2034 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
2036 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
2038 /* Verify UDP length. */
2039 ip_len0 = clib_net_to_host_u16 (ip0->length);
2040 udp_len0 = clib_net_to_host_u16 (udp0->length);
2042 len_diff0 = ip_len0 - udp_len0;
2044 len_diff0 = is_udp0 ? len_diff0 : 0;
2046 if (PREDICT_FALSE (! (is_tcp_udp0 & good_tcp_udp0)))
2051 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED))
2052 flags0 = ip4_tcp_udp_validate_checksum (vm, p0);
2054 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
2055 good_tcp_udp0 |= is_udp0 && udp0->checksum == 0;
2059 good_tcp_udp0 &= len_diff0 >= 0;
2061 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
2063 error0 = IP4_ERROR_UNKNOWN_PROTOCOL;
2065 error0 = len_diff0 < 0 ? IP4_ERROR_UDP_LENGTH : error0;
2067 ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM);
2068 error0 = (is_tcp_udp0 && ! good_tcp_udp0
2069 ? IP4_ERROR_TCP_CHECKSUM + is_udp0
2072 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
2074 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
2075 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
2077 ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
2079 /* no_default_route */ 1));
2081 adj0 = ip_get_adjacency (lm, adj_index0);
2083 /* Must have a route to source otherwise we drop the packet. */
2084 error0 = (error0 == IP4_ERROR_UNKNOWN_PROTOCOL
2085 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE
2086 && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
2087 && adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL
2088 && ip0->dst_address.as_u32 != 0xFFFFFFFF
2089 ? IP4_ERROR_SRC_LOOKUP_MISS
2092 next0 = lm->local_next_by_ip_protocol[proto0];
2094 next0 = error0 != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
2096 p0->error = error0? error_node->errors[error0] : 0;
2098 if (PREDICT_FALSE (next0 != next_index))
2100 n_left_to_next += 1;
2101 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2104 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2107 n_left_to_next -= 1;
2111 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2114 return frame->n_vectors;
2117 VLIB_REGISTER_NODE (ip4_local_node,static) = {
2118 .function = ip4_local,
2119 .name = "ip4-local",
2120 .vector_size = sizeof (u32),
2122 .format_trace = format_ip4_forward_next_trace,
2124 .n_next_nodes = IP_LOCAL_N_NEXT,
2126 [IP_LOCAL_NEXT_DROP] = "error-drop",
2127 [IP_LOCAL_NEXT_PUNT] = "error-punt",
2128 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip4-udp-lookup",
2129 [IP_LOCAL_NEXT_ICMP] = "ip4-icmp-input",
2133 VLIB_NODE_FUNCTION_MULTIARCH (ip4_local_node, ip4_local)
2135 void ip4_register_protocol (u32 protocol, u32 node_index)
2137 vlib_main_t * vm = vlib_get_main();
2138 ip4_main_t * im = &ip4_main;
2139 ip_lookup_main_t * lm = &im->lookup_main;
2141 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
2142 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip4_local_node.index, node_index);
2145 static clib_error_t *
2146 show_ip_local_command_fn (vlib_main_t * vm,
2147 unformat_input_t * input,
2148 vlib_cli_command_t * cmd)
2150 ip4_main_t * im = &ip4_main;
2151 ip_lookup_main_t * lm = &im->lookup_main;
2154 vlib_cli_output (vm, "Protocols handled by ip4_local");
2155 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2157 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2158 vlib_cli_output (vm, "%d", i);
2165 VLIB_CLI_COMMAND (show_ip_local, static) = {
2166 .path = "show ip local",
2167 .function = show_ip_local_command_fn,
2168 .short_help = "Show ip local protocol table",
2172 ip4_arp (vlib_main_t * vm,
2173 vlib_node_runtime_t * node,
2174 vlib_frame_t * frame)
2176 vnet_main_t * vnm = vnet_get_main();
2177 ip4_main_t * im = &ip4_main;
2178 ip_lookup_main_t * lm = &im->lookup_main;
2179 u32 * from, * to_next_drop;
2180 uword n_left_from, n_left_to_next_drop, next_index;
2181 static f64 time_last_seed_change = -1e100;
2182 static u32 hash_seeds[3];
2183 static uword hash_bitmap[256 / BITS (uword)];
2186 if (node->flags & VLIB_NODE_FLAG_TRACE)
2187 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
2189 time_now = vlib_time_now (vm);
2190 if (time_now - time_last_seed_change > 1e-3)
2193 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
2194 sizeof (hash_seeds));
2195 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
2196 hash_seeds[i] = r[i];
2198 /* Mark all hash keys as been no-seen before. */
2199 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
2202 time_last_seed_change = time_now;
2205 from = vlib_frame_vector_args (frame);
2206 n_left_from = frame->n_vectors;
2207 next_index = node->cached_next_index;
2208 if (next_index == IP4_ARP_NEXT_DROP)
2209 next_index = IP4_ARP_N_NEXT; /* point to first interface */
2211 while (n_left_from > 0)
2213 vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP,
2214 to_next_drop, n_left_to_next_drop);
2216 while (n_left_from > 0 && n_left_to_next_drop > 0)
2220 ethernet_header_t * eh0;
2221 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
2223 ip_adjacency_t * adj0;
2227 p0 = vlib_get_buffer (vm, pi0);
2229 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2230 adj0 = ip_get_adjacency (lm, adj_index0);
2231 ip0 = vlib_buffer_get_current (p0);
2233 /* If packet destination is not local, send ARP to next hop */
2234 if (adj0->arp.next_hop.ip4.as_u32)
2235 ip0->dst_address.data_u32 = adj0->arp.next_hop.ip4.as_u32;
2238 * if ip4_rewrite_local applied the IP_LOOKUP_NEXT_ARP
2239 * rewrite to this packet, we need to skip it here.
2240 * Note, to distinguish from src IP addr *.8.6.*, we
2241 * check for a bcast eth dest instead of IPv4 version.
2243 eh0 = (ethernet_header_t*)ip0;
2244 if ((ip0->ip_version_and_header_length & 0xF0) != 0x40)
2247 u16 * etype = &eh0->type;
2248 while ((*etype == clib_host_to_net_u16 (0x8100)) //dot1q
2249 || (*etype == clib_host_to_net_u16 (0x88a8)))//dot1ad
2252 etype += 2; //vlan tag also 16 bits, same as etype
2254 if (*etype == clib_host_to_net_u16 (0x0806)) //arp
2256 vlib_buffer_advance (
2257 p0, sizeof(ethernet_header_t) + (4*vlan_num));
2258 ip0 = vlib_buffer_get_current (p0);
2266 sw_if_index0 = adj0->rewrite_header.sw_if_index;
2267 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
2269 a0 ^= ip0->dst_address.data_u32;
2272 hash_v3_finalize32 (a0, b0, c0);
2274 c0 &= BITS (hash_bitmap) - 1;
2275 c0 = c0 / BITS (uword);
2276 m0 = (uword) 1 << (c0 % BITS (uword));
2278 bm0 = hash_bitmap[c0];
2279 drop0 = (bm0 & m0) != 0;
2281 /* Mark it as seen. */
2282 hash_bitmap[c0] = bm0 | m0;
2286 to_next_drop[0] = pi0;
2288 n_left_to_next_drop -= 1;
2290 p0->error = node->errors[drop0 ? IP4_ARP_ERROR_DROP : IP4_ARP_ERROR_REQUEST_SENT];
2296 * Can happen if the control-plane is programming tables
2297 * with traffic flowing; at least that's today's lame excuse.
2299 if (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP)
2301 p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ];
2304 /* Send ARP request. */
2308 ethernet_arp_header_t * h0;
2309 vnet_hw_interface_t * hw_if0;
2311 h0 = vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, &bi0);
2313 /* Add rewrite/encap string for ARP packet. */
2314 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
2316 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2318 /* Src ethernet address in ARP header. */
2319 clib_memcpy (h0->ip4_over_ethernet[0].ethernet, hw_if0->hw_address,
2320 sizeof (h0->ip4_over_ethernet[0].ethernet));
2322 ip4_src_address_for_packet (im, p0, &h0->ip4_over_ethernet[0].ip4, sw_if_index0);
2324 /* Copy in destination address we are requesting. */
2325 h0->ip4_over_ethernet[1].ip4.data_u32 = ip0->dst_address.data_u32;
2327 vlib_buffer_copy_trace_flag (vm, p0, bi0);
2328 b0 = vlib_get_buffer (vm, bi0);
2329 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
2331 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2333 vlib_set_next_frame_buffer (vm, node, adj0->rewrite_header.next_index, bi0);
2337 vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
2340 return frame->n_vectors;
2343 static char * ip4_arp_error_strings[] = {
2344 [IP4_ARP_ERROR_DROP] = "address overflow drops",
2345 [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
2346 [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
2347 [IP4_ARP_ERROR_REPLICATE_DROP] = "ARP replication completed",
2348 [IP4_ARP_ERROR_REPLICATE_FAIL] = "ARP replication failed",
2351 VLIB_REGISTER_NODE (ip4_arp_node) = {
2352 .function = ip4_arp,
2354 .vector_size = sizeof (u32),
2356 .format_trace = format_ip4_forward_next_trace,
2358 .n_errors = ARRAY_LEN (ip4_arp_error_strings),
2359 .error_strings = ip4_arp_error_strings,
2361 .n_next_nodes = IP4_ARP_N_NEXT,
2363 [IP4_ARP_NEXT_DROP] = "error-drop",
2367 #define foreach_notrace_ip4_arp_error \
2373 clib_error_t * arp_notrace_init (vlib_main_t * vm)
2375 vlib_node_runtime_t *rt =
2376 vlib_node_get_runtime (vm, ip4_arp_node.index);
2378 /* don't trace ARP request packets */
2380 vnet_pcap_drop_trace_filter_add_del \
2381 (rt->errors[IP4_ARP_ERROR_##a], \
2383 foreach_notrace_ip4_arp_error;
2388 VLIB_INIT_FUNCTION(arp_notrace_init);
2391 /* Send an ARP request to see if given destination is reachable on given interface. */
2393 ip4_probe_neighbor (vlib_main_t * vm, ip4_address_t * dst, u32 sw_if_index)
2395 vnet_main_t * vnm = vnet_get_main();
2396 ip4_main_t * im = &ip4_main;
2397 ethernet_arp_header_t * h;
2398 ip4_address_t * src;
2399 ip_interface_address_t * ia;
2400 ip_adjacency_t * adj;
2401 vnet_hw_interface_t * hi;
2402 vnet_sw_interface_t * si;
2406 si = vnet_get_sw_interface (vnm, sw_if_index);
2408 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2410 return clib_error_return (0, "%U: interface %U down",
2411 format_ip4_address, dst,
2412 format_vnet_sw_if_index_name, vnm,
2416 src = ip4_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2419 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2420 return clib_error_return
2421 (0, "no matching interface address for destination %U (interface %U)",
2422 format_ip4_address, dst,
2423 format_vnet_sw_if_index_name, vnm, sw_if_index);
2426 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2428 h = vlib_packet_template_get_packet (vm, &im->ip4_arp_request_packet_template, &bi);
2430 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2432 clib_memcpy (h->ip4_over_ethernet[0].ethernet, hi->hw_address, sizeof (h->ip4_over_ethernet[0].ethernet));
2434 h->ip4_over_ethernet[0].ip4 = src[0];
2435 h->ip4_over_ethernet[1].ip4 = dst[0];
2437 b = vlib_get_buffer (vm, bi);
2438 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2440 /* Add encapsulation string for software interface (e.g. ethernet header). */
2441 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2442 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2445 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2446 u32 * to_next = vlib_frame_vector_args (f);
2449 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2452 return /* no error */ 0;
2456 IP4_REWRITE_NEXT_DROP,
2457 IP4_REWRITE_NEXT_ARP,
2458 } ip4_rewrite_next_t;
2461 ip4_rewrite_inline (vlib_main_t * vm,
2462 vlib_node_runtime_t * node,
2463 vlib_frame_t * frame,
2464 int rewrite_for_locally_received_packets)
2466 ip_lookup_main_t * lm = &ip4_main.lookup_main;
2467 u32 * from = vlib_frame_vector_args (frame);
2468 u32 n_left_from, n_left_to_next, * to_next, next_index;
2469 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
2470 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2472 n_left_from = frame->n_vectors;
2473 next_index = node->cached_next_index;
2474 u32 cpu_index = os_get_cpu_number();
2476 while (n_left_from > 0)
2478 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2480 while (n_left_from >= 4 && n_left_to_next >= 2)
2482 ip_adjacency_t * adj0, * adj1;
2483 vlib_buffer_t * p0, * p1;
2484 ip4_header_t * ip0, * ip1;
2485 u32 pi0, rw_len0, next0, error0, checksum0, adj_index0;
2486 u32 pi1, rw_len1, next1, error1, checksum1, adj_index1;
2487 u32 next0_override, next1_override;
2489 if (rewrite_for_locally_received_packets)
2490 next0_override = next1_override = 0;
2492 /* Prefetch next iteration. */
2494 vlib_buffer_t * p2, * p3;
2496 p2 = vlib_get_buffer (vm, from[2]);
2497 p3 = vlib_get_buffer (vm, from[3]);
2499 vlib_prefetch_buffer_header (p2, STORE);
2500 vlib_prefetch_buffer_header (p3, STORE);
2502 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2503 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2506 pi0 = to_next[0] = from[0];
2507 pi1 = to_next[1] = from[1];
2512 n_left_to_next -= 2;
2514 p0 = vlib_get_buffer (vm, pi0);
2515 p1 = vlib_get_buffer (vm, pi1);
2517 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2518 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2520 /* We should never rewrite a pkt using the MISS adjacency */
2521 ASSERT(adj_index0 && adj_index1);
2523 ip0 = vlib_buffer_get_current (p0);
2524 ip1 = vlib_buffer_get_current (p1);
2526 error0 = error1 = IP4_ERROR_NONE;
2528 /* Decrement TTL & update checksum.
2529 Works either endian, so no need for byte swap. */
2530 if (! rewrite_for_locally_received_packets)
2532 i32 ttl0 = ip0->ttl, ttl1 = ip1->ttl;
2534 /* Input node should have reject packets with ttl 0. */
2535 ASSERT (ip0->ttl > 0);
2536 ASSERT (ip1->ttl > 0);
2538 checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
2539 checksum1 = ip1->checksum + clib_host_to_net_u16 (0x0100);
2541 checksum0 += checksum0 >= 0xffff;
2542 checksum1 += checksum1 >= 0xffff;
2544 ip0->checksum = checksum0;
2545 ip1->checksum = checksum1;
2553 error0 = ttl0 <= 0 ? IP4_ERROR_TIME_EXPIRED : error0;
2554 error1 = ttl1 <= 0 ? IP4_ERROR_TIME_EXPIRED : error1;
2556 /* Verify checksum. */
2557 ASSERT (ip0->checksum == ip4_header_checksum (ip0));
2558 ASSERT (ip1->checksum == ip4_header_checksum (ip1));
2561 /* Rewrite packet header and updates lengths. */
2562 adj0 = ip_get_adjacency (lm, adj_index0);
2563 adj1 = ip_get_adjacency (lm, adj_index1);
2565 if (rewrite_for_locally_received_packets)
2568 * If someone sends e.g. an icmp4 w/ src = dst = interface addr,
2569 * we end up here with a local adjacency in hand
2570 * The local adj rewrite data is 0xfefe on purpose.
2571 * Bad engineer, no donut for you.
2573 if (PREDICT_FALSE(adj0->lookup_next_index
2574 == IP_LOOKUP_NEXT_LOCAL))
2575 error0 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2576 if (PREDICT_FALSE(adj0->lookup_next_index
2577 == IP_LOOKUP_NEXT_ARP))
2578 next0_override = IP4_REWRITE_NEXT_ARP;
2579 if (PREDICT_FALSE(adj1->lookup_next_index
2580 == IP_LOOKUP_NEXT_LOCAL))
2581 error1 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2582 if (PREDICT_FALSE(adj1->lookup_next_index
2583 == IP_LOOKUP_NEXT_ARP))
2584 next1_override = IP4_REWRITE_NEXT_ARP;
2587 /* Worth pipelining. No guarantee that adj0,1 are hot... */
2588 rw_len0 = adj0[0].rewrite_header.data_bytes;
2589 rw_len1 = adj1[0].rewrite_header.data_bytes;
2590 next0 = (error0 == IP4_ERROR_NONE)
2591 ? adj0[0].rewrite_header.next_index : 0;
2593 if (rewrite_for_locally_received_packets)
2594 next0 = next0 && next0_override ? next0_override : next0;
2596 next1 = (error1 == IP4_ERROR_NONE)
2597 ? adj1[0].rewrite_header.next_index : 0;
2599 if (rewrite_for_locally_received_packets)
2600 next1 = next1 && next1_override ? next1_override : next1;
2603 * We've already accounted for an ethernet_header_t elsewhere
2605 if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
2606 vlib_increment_combined_counter
2607 (&lm->adjacency_counters,
2608 cpu_index, adj_index0,
2609 /* packet increment */ 0,
2610 /* byte increment */ rw_len0-sizeof(ethernet_header_t));
2612 if (PREDICT_FALSE (rw_len1 > sizeof(ethernet_header_t)))
2613 vlib_increment_combined_counter
2614 (&lm->adjacency_counters,
2615 cpu_index, adj_index1,
2616 /* packet increment */ 0,
2617 /* byte increment */ rw_len1-sizeof(ethernet_header_t));
2619 /* Check MTU of outgoing interface. */
2620 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2621 ? IP4_ERROR_MTU_EXCEEDED
2623 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2624 ? IP4_ERROR_MTU_EXCEEDED
2627 p0->current_data -= rw_len0;
2628 p1->current_data -= rw_len1;
2630 p0->current_length += rw_len0;
2631 p1->current_length += rw_len1;
2633 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2634 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2636 p0->error = error_node->errors[error0];
2637 p1->error = error_node->errors[error1];
2639 /* Guess we are only writing on simple Ethernet header. */
2640 vnet_rewrite_two_headers (adj0[0], adj1[0],
2642 sizeof (ethernet_header_t));
2644 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2645 to_next, n_left_to_next,
2646 pi0, pi1, next0, next1);
2649 while (n_left_from > 0 && n_left_to_next > 0)
2651 ip_adjacency_t * adj0;
2654 u32 pi0, rw_len0, adj_index0, next0, error0, checksum0;
2657 if (rewrite_for_locally_received_packets)
2660 pi0 = to_next[0] = from[0];
2662 p0 = vlib_get_buffer (vm, pi0);
2664 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2666 /* We should never rewrite a pkt using the MISS adjacency */
2669 adj0 = ip_get_adjacency (lm, adj_index0);
2671 ip0 = vlib_buffer_get_current (p0);
2673 error0 = IP4_ERROR_NONE;
2674 next0 = 0; /* drop on error */
2676 /* Decrement TTL & update checksum. */
2677 if (! rewrite_for_locally_received_packets)
2679 i32 ttl0 = ip0->ttl;
2681 checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
2683 checksum0 += checksum0 >= 0xffff;
2685 ip0->checksum = checksum0;
2687 ASSERT (ip0->ttl > 0);
2693 ASSERT (ip0->checksum == ip4_header_checksum (ip0));
2695 error0 = ttl0 <= 0 ? IP4_ERROR_TIME_EXPIRED : error0;
2698 if (rewrite_for_locally_received_packets)
2701 * If someone sends e.g. an icmp4 w/ src = dst = interface addr,
2702 * we end up here with a local adjacency in hand
2703 * The local adj rewrite data is 0xfefe on purpose.
2704 * Bad engineer, no donut for you.
2706 if (PREDICT_FALSE(adj0->lookup_next_index
2707 == IP_LOOKUP_NEXT_LOCAL))
2708 error0 = IP4_ERROR_SPOOFED_LOCAL_PACKETS;
2710 * We have to override the next_index in ARP adjacencies,
2711 * because they're set up for ip4-arp, not this node...
2713 if (PREDICT_FALSE(adj0->lookup_next_index
2714 == IP_LOOKUP_NEXT_ARP))
2715 next0_override = IP4_REWRITE_NEXT_ARP;
2718 /* Guess we are only writing on simple Ethernet header. */
2719 vnet_rewrite_one_header (adj0[0], ip0,
2720 sizeof (ethernet_header_t));
2722 /* Update packet buffer attributes/set output interface. */
2723 rw_len0 = adj0[0].rewrite_header.data_bytes;
2725 if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
2726 vlib_increment_combined_counter
2727 (&lm->adjacency_counters,
2728 cpu_index, adj_index0,
2729 /* packet increment */ 0,
2730 /* byte increment */ rw_len0-sizeof(ethernet_header_t));
2732 /* Check MTU of outgoing interface. */
2733 error0 = (vlib_buffer_length_in_chain (vm, p0)
2734 > adj0[0].rewrite_header.max_l3_packet_bytes
2735 ? IP4_ERROR_MTU_EXCEEDED
2738 p0->error = error_node->errors[error0];
2739 p0->current_data -= rw_len0;
2740 p0->current_length += rw_len0;
2741 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
2742 adj0[0].rewrite_header.sw_if_index;
2744 next0 = (error0 == IP4_ERROR_NONE)
2745 ? adj0[0].rewrite_header.next_index : 0;
2747 if (rewrite_for_locally_received_packets)
2748 next0 = next0 && next0_override ? next0_override : next0;
2753 n_left_to_next -= 1;
2755 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2756 to_next, n_left_to_next,
2760 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2763 /* Need to do trace after rewrites to pick up new packet data. */
2764 if (node->flags & VLIB_NODE_FLAG_TRACE)
2765 ip4_forward_next_trace (vm, node, frame, adj_rx_tx);
2767 return frame->n_vectors;
2771 ip4_rewrite_transit (vlib_main_t * vm,
2772 vlib_node_runtime_t * node,
2773 vlib_frame_t * frame)
2775 return ip4_rewrite_inline (vm, node, frame,
2776 /* rewrite_for_locally_received_packets */ 0);
2780 ip4_rewrite_local (vlib_main_t * vm,
2781 vlib_node_runtime_t * node,
2782 vlib_frame_t * frame)
2784 return ip4_rewrite_inline (vm, node, frame,
2785 /* rewrite_for_locally_received_packets */ 1);
2788 VLIB_REGISTER_NODE (ip4_rewrite_node) = {
2789 .function = ip4_rewrite_transit,
2790 .name = "ip4-rewrite-transit",
2791 .vector_size = sizeof (u32),
2793 .format_trace = format_ip4_rewrite_trace,
2797 [IP4_REWRITE_NEXT_DROP] = "error-drop",
2798 [IP4_REWRITE_NEXT_ARP] = "ip4-arp",
2802 VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_node, ip4_rewrite_transit)
2804 VLIB_REGISTER_NODE (ip4_rewrite_local_node) = {
2805 .function = ip4_rewrite_local,
2806 .name = "ip4-rewrite-local",
2807 .vector_size = sizeof (u32),
2809 .sibling_of = "ip4-rewrite-transit",
2811 .format_trace = format_ip4_rewrite_trace,
2815 [IP4_REWRITE_NEXT_DROP] = "error-drop",
2816 [IP4_REWRITE_NEXT_ARP] = "ip4-arp",
2820 VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_local_node, ip4_rewrite_local)
2822 static clib_error_t *
2823 add_del_interface_table (vlib_main_t * vm,
2824 unformat_input_t * input,
2825 vlib_cli_command_t * cmd)
2827 vnet_main_t * vnm = vnet_get_main();
2828 clib_error_t * error = 0;
2829 u32 sw_if_index, table_id;
2833 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2835 error = clib_error_return (0, "unknown interface `%U'",
2836 format_unformat_error, input);
2840 if (unformat (input, "%d", &table_id))
2844 error = clib_error_return (0, "expected table id `%U'",
2845 format_unformat_error, input);
2850 ip4_main_t * im = &ip4_main;
2851 ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
2855 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2856 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2864 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2865 .path = "set interface ip table",
2866 .function = add_del_interface_table,
2867 .short_help = "Add/delete FIB table id for interface",
2872 ip4_lookup_multicast (vlib_main_t * vm,
2873 vlib_node_runtime_t * node,
2874 vlib_frame_t * frame)
2876 ip4_main_t * im = &ip4_main;
2877 ip_lookup_main_t * lm = &im->lookup_main;
2878 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
2879 u32 n_left_from, n_left_to_next, * from, * to_next;
2880 ip_lookup_next_t next;
2881 u32 cpu_index = os_get_cpu_number();
2883 from = vlib_frame_vector_args (frame);
2884 n_left_from = frame->n_vectors;
2885 next = node->cached_next_index;
2887 while (n_left_from > 0)
2889 vlib_get_next_frame (vm, node, next,
2890 to_next, n_left_to_next);
2892 while (n_left_from >= 4 && n_left_to_next >= 2)
2894 vlib_buffer_t * p0, * p1;
2895 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
2896 ip_lookup_next_t next0, next1;
2897 ip4_header_t * ip0, * ip1;
2898 ip_adjacency_t * adj0, * adj1;
2899 u32 fib_index0, fib_index1;
2900 u32 flow_hash_config0, flow_hash_config1;
2902 /* Prefetch next iteration. */
2904 vlib_buffer_t * p2, * p3;
2906 p2 = vlib_get_buffer (vm, from[2]);
2907 p3 = vlib_get_buffer (vm, from[3]);
2909 vlib_prefetch_buffer_header (p2, LOAD);
2910 vlib_prefetch_buffer_header (p3, LOAD);
2912 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
2913 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
2916 pi0 = to_next[0] = from[0];
2917 pi1 = to_next[1] = from[1];
2919 p0 = vlib_get_buffer (vm, pi0);
2920 p1 = vlib_get_buffer (vm, pi1);
2922 ip0 = vlib_buffer_get_current (p0);
2923 ip1 = vlib_buffer_get_current (p1);
2925 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
2926 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
2927 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
2928 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
2929 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
2930 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
2932 adj_index0 = ip4_fib_lookup_buffer (im, fib_index0,
2933 &ip0->dst_address, p0);
2934 adj_index1 = ip4_fib_lookup_buffer (im, fib_index1,
2935 &ip1->dst_address, p1);
2937 adj0 = ip_get_adjacency (lm, adj_index0);
2938 adj1 = ip_get_adjacency (lm, adj_index1);
2940 next0 = adj0->lookup_next_index;
2941 next1 = adj1->lookup_next_index;
2944 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
2947 vec_elt_at_index (im->fibs, fib_index1)->flow_hash_config;
2949 vnet_buffer (p0)->ip.flow_hash = ip4_compute_flow_hash
2950 (ip0, flow_hash_config0);
2952 vnet_buffer (p1)->ip.flow_hash = ip4_compute_flow_hash
2953 (ip1, flow_hash_config1);
2955 ASSERT (adj0->n_adj > 0);
2956 ASSERT (adj1->n_adj > 0);
2957 ASSERT (is_pow2 (adj0->n_adj));
2958 ASSERT (is_pow2 (adj1->n_adj));
2959 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
2960 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
2962 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
2963 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
2965 if (1) /* $$$$$$ HACK FIXME */
2966 vlib_increment_combined_counter
2967 (cm, cpu_index, adj_index0, 1,
2968 vlib_buffer_length_in_chain (vm, p0));
2969 if (1) /* $$$$$$ HACK FIXME */
2970 vlib_increment_combined_counter
2971 (cm, cpu_index, adj_index1, 1,
2972 vlib_buffer_length_in_chain (vm, p1));
2976 n_left_to_next -= 2;
2979 wrong_next = (next0 != next) + 2*(next1 != next);
2980 if (PREDICT_FALSE (wrong_next != 0))
2988 n_left_to_next += 1;
2989 vlib_set_next_frame_buffer (vm, node, next0, pi0);
2995 n_left_to_next += 1;
2996 vlib_set_next_frame_buffer (vm, node, next1, pi1);
3002 n_left_to_next += 2;
3003 vlib_set_next_frame_buffer (vm, node, next0, pi0);
3004 vlib_set_next_frame_buffer (vm, node, next1, pi1);
3008 vlib_put_next_frame (vm, node, next, n_left_to_next);
3010 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
3016 while (n_left_from > 0 && n_left_to_next > 0)
3020 u32 pi0, adj_index0;
3021 ip_lookup_next_t next0;
3022 ip_adjacency_t * adj0;
3024 u32 flow_hash_config0;
3029 p0 = vlib_get_buffer (vm, pi0);
3031 ip0 = vlib_buffer_get_current (p0);
3033 fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
3034 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
3035 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
3036 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
3038 adj_index0 = ip4_fib_lookup_buffer (im, fib_index0,
3039 &ip0->dst_address, p0);
3041 adj0 = ip_get_adjacency (lm, adj_index0);
3043 next0 = adj0->lookup_next_index;
3046 vec_elt_at_index (im->fibs, fib_index0)->flow_hash_config;
3048 vnet_buffer (p0)->ip.flow_hash =
3049 ip4_compute_flow_hash (ip0, flow_hash_config0);
3051 ASSERT (adj0->n_adj > 0);
3052 ASSERT (is_pow2 (adj0->n_adj));
3053 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
3055 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
3057 if (1) /* $$$$$$ HACK FIXME */
3058 vlib_increment_combined_counter
3059 (cm, cpu_index, adj_index0, 1,
3060 vlib_buffer_length_in_chain (vm, p0));
3064 n_left_to_next -= 1;
3067 if (PREDICT_FALSE (next0 != next))
3069 n_left_to_next += 1;
3070 vlib_put_next_frame (vm, node, next, n_left_to_next);
3072 vlib_get_next_frame (vm, node, next,
3073 to_next, n_left_to_next);
3076 n_left_to_next -= 1;
3080 vlib_put_next_frame (vm, node, next, n_left_to_next);
3083 if (node->flags & VLIB_NODE_FLAG_TRACE)
3084 ip4_forward_next_trace(vm, node, frame, VLIB_TX);
3086 return frame->n_vectors;
3089 VLIB_REGISTER_NODE (ip4_lookup_multicast_node,static) = {
3090 .function = ip4_lookup_multicast,
3091 .name = "ip4-lookup-multicast",
3092 .vector_size = sizeof (u32),
3094 .format_trace = format_ip4_lookup_trace,
3096 .n_next_nodes = IP_LOOKUP_N_NEXT,
3097 .next_nodes = IP4_LOOKUP_NEXT_NODES,
3100 VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_multicast_node, ip4_lookup_multicast)
3102 VLIB_REGISTER_NODE (ip4_multicast_node,static) = {
3103 .function = ip4_drop,
3104 .name = "ip4-multicast",
3105 .vector_size = sizeof (u32),
3107 .format_trace = format_ip4_forward_next_trace,
3115 int ip4_lookup_validate (ip4_address_t *a, u32 fib_index0)
3117 ip4_main_t * im = &ip4_main;
3118 ip4_fib_mtrie_t * mtrie0;
3119 ip4_fib_mtrie_leaf_t leaf0;
3122 mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie;
3124 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
3125 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 0);
3126 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 1);
3127 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 2);
3128 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 3);
3130 /* Handle default route. */
3131 leaf0 = (leaf0 == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie0->default_leaf : leaf0);
3133 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
3135 return adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
3137 /* no_default_route */ 0);
3140 static clib_error_t *
3141 test_lookup_command_fn (vlib_main_t * vm,
3142 unformat_input_t * input,
3143 vlib_cli_command_t * cmd)
3149 ip4_address_t ip4_base_address;
3152 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3153 if (unformat (input, "table %d", &table_id))
3155 else if (unformat (input, "count %f", &count))
3158 else if (unformat (input, "%U",
3159 unformat_ip4_address, &ip4_base_address))
3162 return clib_error_return (0, "unknown input `%U'",
3163 format_unformat_error, input);
3168 for (i = 0; i < n; i++)
3170 if (!ip4_lookup_validate (&ip4_base_address, table_id))
3173 ip4_base_address.as_u32 =
3174 clib_host_to_net_u32 (1 +
3175 clib_net_to_host_u32 (ip4_base_address.as_u32));
3179 vlib_cli_output (vm, "%llu errors out of %d lookups\n", errors, n);
3181 vlib_cli_output (vm, "No errors in %d lookups\n", n);
3186 VLIB_CLI_COMMAND (lookup_test_command, static) = {
3187 .path = "test lookup",
3188 .short_help = "test lookup",
3189 .function = test_lookup_command_fn,
3192 int vnet_set_ip4_flow_hash (u32 table_id, u32 flow_hash_config)
3194 ip4_main_t * im4 = &ip4_main;
3196 uword * p = hash_get (im4->fib_index_by_table_id, table_id);
3199 return VNET_API_ERROR_NO_SUCH_FIB;
3201 fib = vec_elt_at_index (im4->fibs, p[0]);
3203 fib->flow_hash_config = flow_hash_config;
3207 static clib_error_t *
3208 set_ip_flow_hash_command_fn (vlib_main_t * vm,
3209 unformat_input_t * input,
3210 vlib_cli_command_t * cmd)
3214 u32 flow_hash_config = 0;
3217 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3218 if (unformat (input, "table %d", &table_id))
3221 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3222 foreach_flow_hash_bit
3228 return clib_error_return (0, "unknown input `%U'",
3229 format_unformat_error, input);
3231 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3237 case VNET_API_ERROR_NO_SUCH_FIB:
3238 return clib_error_return (0, "no such FIB table %d", table_id);
3241 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3248 VLIB_CLI_COMMAND (set_ip_flow_hash_command, static) = {
3249 .path = "set ip flow-hash",
3251 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
3252 .function = set_ip_flow_hash_command_fn,
3255 int vnet_set_ip4_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3258 vnet_main_t * vnm = vnet_get_main();
3259 vnet_interface_main_t * im = &vnm->interface_main;
3260 ip4_main_t * ipm = &ip4_main;
3261 ip_lookup_main_t * lm = &ipm->lookup_main;
3262 vnet_classify_main_t * cm = &vnet_classify_main;
3264 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3265 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3267 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3268 return VNET_API_ERROR_NO_SUCH_ENTRY;
3270 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3271 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
3276 static clib_error_t *
3277 set_ip_classify_command_fn (vlib_main_t * vm,
3278 unformat_input_t * input,
3279 vlib_cli_command_t * cmd)
3281 u32 table_index = ~0;
3282 int table_index_set = 0;
3283 u32 sw_if_index = ~0;
3286 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3287 if (unformat (input, "table-index %d", &table_index))
3288 table_index_set = 1;
3289 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3290 vnet_get_main(), &sw_if_index))
3296 if (table_index_set == 0)
3297 return clib_error_return (0, "classify table-index must be specified");
3299 if (sw_if_index == ~0)
3300 return clib_error_return (0, "interface / subif must be specified");
3302 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3309 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3310 return clib_error_return (0, "No such interface");
3312 case VNET_API_ERROR_NO_SUCH_ENTRY:
3313 return clib_error_return (0, "No such classifier table");
3318 VLIB_CLI_COMMAND (set_ip_classify_command, static) = {
3319 .path = "set ip classify",
3321 "set ip classify intfc <int> table-index <index>",
3322 .function = set_ip_classify_command_fn,