blob: 368703d595b898c2af08cdc560fa6cf72fca06de [file] [log] [blame]
Neale Rannscbe25aa2019-09-30 10:53:31 +00001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/*
16 * ip/ip4_forward.c: IP v4 forwarding
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
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:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
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.
38 */
39
40#include <vnet/ip-neighbor/ip4_neighbor.h>
41#include <vnet/ethernet/ethernet.h>
Neale Rannse4031132020-10-26 13:00:06 +000042#include <vnet/util/throttle.h>
Neale Rannse2fe0972020-11-26 08:37:27 +000043#include <vnet/fib/fib_sas.h>
Ed Warnicke9e178872021-08-14 16:19:43 -050044#include <vnet/ip/ip_sas.h>
Neale Rannse4031132020-10-26 13:00:06 +000045
46/** ARP throttling */
47static throttle_t arp_throttle;
Neale Rannscbe25aa2019-09-30 10:53:31 +000048
Steven Luong19be3282021-03-23 11:55:33 -070049VLIB_REGISTER_LOG_CLASS (ip4_neighbor_log, static) = {
50 .class_name = "ip4",
51 .subclass_name = "neighbor",
52};
53
54#define log_debug(fmt, ...) \
55 vlib_log_debug (ip4_neighbor_log.class, fmt, __VA_ARGS__)
56
Neale Rannscbe25aa2019-09-30 10:53:31 +000057void
Neale Rannsfd2417b2021-07-16 14:00:16 +000058ip4_neighbor_probe_dst (u32 sw_if_index, u32 thread_index,
59 const ip4_address_t *dst)
Neale Rannscbe25aa2019-09-30 10:53:31 +000060{
Neale Rannse2fe0972020-11-26 08:37:27 +000061 ip4_address_t src;
62 adj_index_t ai;
Neale Rannscbe25aa2019-09-30 10:53:31 +000063
Neale Rannse2fe0972020-11-26 08:37:27 +000064 /* any glean will do, it's just for the rewrite */
65 ai = adj_glean_get (FIB_PROTOCOL_IP4, sw_if_index, NULL);
Neale Rannscbe25aa2019-09-30 10:53:31 +000066
Ed Warnicke9e178872021-08-14 16:19:43 -050067 if (ADJ_INDEX_INVALID != ai &&
68 (fib_sas4_get (sw_if_index, dst, &src) ||
69 ip4_sas_by_sw_if_index (sw_if_index, dst, &src)))
Neale Rannse2fe0972020-11-26 08:37:27 +000070 ip4_neighbor_probe (vlib_get_main (),
71 vnet_get_main (), adj_get (ai), &src, dst);
Neale Rannscbe25aa2019-09-30 10:53:31 +000072}
73
74void
Neale Rannsfd2417b2021-07-16 14:00:16 +000075ip4_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index,
76 u32 thread_index, const ip4_address_t *addr)
Neale Rannscbe25aa2019-09-30 10:53:31 +000077{
78 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
79 ip4_main_t *i4m = &ip4_main;
80 u8 *rewrite, rewrite_len;
Neale Rannse2fe0972020-11-26 08:37:27 +000081 ip4_address_t tmp;
Neale Rannscbe25aa2019-09-30 10:53:31 +000082
83 if (NULL == addr)
84 {
Ed Warnicke9e178872021-08-14 16:19:43 -050085 if (fib_sas4_get (sw_if_index, NULL, &tmp) ||
86 ip4_sas_by_sw_if_index (sw_if_index, NULL, &tmp))
Steven Luong18991be2021-07-15 08:57:02 -070087 addr = &tmp;
Neale Rannscbe25aa2019-09-30 10:53:31 +000088 }
89
90 if (addr)
91 {
Steven Luong19be3282021-03-23 11:55:33 -070092 log_debug ("Sending GARP for IP4 address %U on sw_if_idex %d",
93 format_ip4_address, addr, sw_if_index);
Neale Rannscbe25aa2019-09-30 10:53:31 +000094
95 /* Form GARP packet for output - Gratuitous ARP is an ARP request packet
96 where the interface IP/MAC pair is used for both source and request
97 MAC/IP pairs in the request */
98 u32 bi = 0;
99 ethernet_arp_header_t *h = vlib_packet_template_get_packet
100 (vm, &i4m->ip4_arp_request_packet_template, &bi);
101
102 if (!h)
103 return;
104
105 mac_address_from_bytes (&h->ip4_over_ethernet[0].mac, hi->hw_address);
106 mac_address_from_bytes (&h->ip4_over_ethernet[1].mac, hi->hw_address);
107 h->ip4_over_ethernet[0].ip4 = addr[0];
108 h->ip4_over_ethernet[1].ip4 = addr[0];
109
110 /* Setup MAC header with ARP Etype and broadcast DMAC */
111 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
112 rewrite =
113 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_ARP,
114 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST);
115 rewrite_len = vec_len (rewrite);
116 vlib_buffer_advance (b, -rewrite_len);
117 ethernet_header_t *e = vlib_buffer_get_current (b);
118 clib_memcpy_fast (e->dst_address, rewrite, rewrite_len);
119 vec_free (rewrite);
120
121 /* Send GARP packet out the specified interface */
122 vnet_buffer (b)->sw_if_index[VLIB_RX] =
123 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
124 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
125 u32 *to_next = vlib_frame_vector_args (f);
126 to_next[0] = bi;
127 f->n_vectors = 1;
128 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsfd2417b2021-07-16 14:00:16 +0000129
130 vlib_increment_simple_counter (
131 &ip_neighbor_counters[AF_IP4].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_GRAT],
132 thread_index, sw_if_index, 1);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000133 }
134}
135
136always_inline uword
137ip4_arp_inline (vlib_main_t * vm,
138 vlib_node_runtime_t * node,
139 vlib_frame_t * frame, int is_glean)
140{
141 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +0000142 u32 *from, *to_next_drop;
143 uword n_left_from, n_left_to_next_drop, next_index;
144 u32 thread_index = vm->thread_index;
145 u64 seed;
146
147 if (node->flags & VLIB_NODE_FLAG_TRACE)
148 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
149
Neale Rannse4031132020-10-26 13:00:06 +0000150 seed = throttle_seed (&arp_throttle, thread_index, vlib_time_now (vm));
Neale Rannscbe25aa2019-09-30 10:53:31 +0000151
152 from = vlib_frame_vector_args (frame);
153 n_left_from = frame->n_vectors;
154 next_index = node->cached_next_index;
155 if (next_index == IP4_ARP_NEXT_DROP)
156 next_index = IP4_ARP_N_NEXT; /* point to first interface */
157
158 while (n_left_from > 0)
159 {
160 vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP,
161 to_next_drop, n_left_to_next_drop);
162
163 while (n_left_from > 0 && n_left_to_next_drop > 0)
164 {
165 u32 pi0, adj_index0, sw_if_index0;
166 ip4_address_t resolve0, src0;
167 vlib_buffer_t *p0, *b0;
168 ip_adjacency_t *adj0;
169 u64 r0;
170
171 pi0 = from[0];
172 p0 = vlib_get_buffer (vm, pi0);
173
174 from += 1;
175 n_left_from -= 1;
176 to_next_drop[0] = pi0;
177 to_next_drop += 1;
178 n_left_to_next_drop -= 1;
179
180 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
181 adj0 = adj_get (adj_index0);
182 sw_if_index0 = adj0->rewrite_header.sw_if_index;
183
184 if (is_glean)
185 {
186 /* resolve the packet's destination */
187 ip4_header_t *ip0 = vlib_buffer_get_current (p0);
188 resolve0 = ip0->dst_address;
Neale Rannse2fe0972020-11-26 08:37:27 +0000189 src0 = adj0->sub_type.glean.rx_pfx.fp_addr.ip4;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000190 }
191 else
192 {
193 /* resolve the incomplete adj */
194 resolve0 = adj0->sub_type.nbr.next_hop.ip4;
195 /* Src IP address in ARP header. */
Ed Warnicke9e178872021-08-14 16:19:43 -0500196 if (!fib_sas4_get (sw_if_index0, &resolve0, &src0) &&
197 !ip4_sas_by_sw_if_index (sw_if_index0, &resolve0, &src0))
Neale Rannscbe25aa2019-09-30 10:53:31 +0000198 {
199 /* No source address available */
200 p0->error = node->errors[IP4_ARP_ERROR_NO_SOURCE_ADDRESS];
201 continue;
202 }
203 }
204
205 /* combine the address and interface for the hash key */
206 r0 = (u64) resolve0.data_u32 << 32;
207 r0 |= sw_if_index0;
208
Neale Rannse4031132020-10-26 13:00:06 +0000209 if (throttle_check (&arp_throttle, thread_index, r0, seed))
Neale Rannscbe25aa2019-09-30 10:53:31 +0000210 {
211 p0->error = node->errors[IP4_ARP_ERROR_THROTTLED];
212 continue;
213 }
214
215 /*
216 * the adj has been updated to a rewrite but the node the DPO that got
217 * us here hasn't - yet. no big deal. we'll drop while we wait.
218 */
219 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
220 {
221 p0->error = node->errors[IP4_ARP_ERROR_RESOLVED];
222 continue;
223 }
224
225 /*
226 * Can happen if the control-plane is programming tables
227 * with traffic flowing; at least that's today's lame excuse.
228 */
229 if ((is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN)
230 || (!is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP))
231 {
232 p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ];
233 continue;
234 }
235
236 /* Send ARP request. */
237 b0 = ip4_neighbor_probe (vm, vnm, adj0, &src0, &resolve0);
238
239 if (PREDICT_TRUE (NULL != b0))
240 {
241 /* copy the persistent fields from the original */
242 clib_memcpy_fast (b0->opaque2, p0->opaque2,
243 sizeof (p0->opaque2));
244 p0->error = node->errors[IP4_ARP_ERROR_REQUEST_SENT];
245 }
246 else
247 {
248 p0->error = node->errors[IP4_ARP_ERROR_NO_BUFFERS];
249 continue;
250 }
251 }
252
253 vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
254 }
255
256 return frame->n_vectors;
257}
258
259VLIB_NODE_FN (ip4_arp_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
260 vlib_frame_t * frame)
261{
262 return (ip4_arp_inline (vm, node, frame, 0));
263}
264
265VLIB_NODE_FN (ip4_glean_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
266 vlib_frame_t * frame)
267{
268 return (ip4_arp_inline (vm, node, frame, 1));
269}
270
271static char *ip4_arp_error_strings[] = {
272 [IP4_ARP_ERROR_THROTTLED] = "ARP requests throttled",
273 [IP4_ARP_ERROR_RESOLVED] = "ARP requests resolved",
274 [IP4_ARP_ERROR_NO_BUFFERS] = "ARP requests out of buffer",
275 [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
276 [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
277 [IP4_ARP_ERROR_NO_SOURCE_ADDRESS] = "no source address for ARP request",
278};
279
280/* *INDENT-OFF* */
281VLIB_REGISTER_NODE (ip4_arp_node) =
282{
283 .name = "ip4-arp",
284 .vector_size = sizeof (u32),
285 .format_trace = format_ip4_forward_next_trace,
286 .n_errors = ARRAY_LEN (ip4_arp_error_strings),
287 .error_strings = ip4_arp_error_strings,
288 .n_next_nodes = IP4_ARP_N_NEXT,
289 .next_nodes = {
290 [IP4_ARP_NEXT_DROP] = "ip4-drop",
291 },
292};
293
294VLIB_REGISTER_NODE (ip4_glean_node) =
295{
296 .name = "ip4-glean",
297 .vector_size = sizeof (u32),
298 .format_trace = format_ip4_forward_next_trace,
299 .n_errors = ARRAY_LEN (ip4_arp_error_strings),
300 .error_strings = ip4_arp_error_strings,
301 .n_next_nodes = IP4_ARP_N_NEXT,
302 .next_nodes = {
303 [IP4_ARP_NEXT_DROP] = "ip4-drop",
304 },
305};
306/* *INDENT-ON* */
307
308#define foreach_notrace_ip4_arp_error \
309_(THROTTLED) \
310_(RESOLVED) \
311_(NO_BUFFERS) \
312_(REQUEST_SENT) \
313_(NON_ARP_ADJ) \
314_(NO_SOURCE_ADDRESS)
315
316static clib_error_t *
317arp_notrace_init (vlib_main_t * vm)
318{
319 vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, ip4_arp_node.index);
320
321 /* don't trace ARP request packets */
322#define _(a) \
323 vnet_pcap_drop_trace_filter_add_del \
324 (rt->errors[IP4_ARP_ERROR_##a], \
325 1 /* is_add */);
326 foreach_notrace_ip4_arp_error;
327#undef _
328 return 0;
329}
330
331VLIB_INIT_FUNCTION (arp_notrace_init);
332
Neale Rannse4031132020-10-26 13:00:06 +0000333static clib_error_t *
334ip4_neighbor_main_loop_enter (vlib_main_t * vm)
335{
336 vlib_thread_main_t *tm = &vlib_thread_main;
337 u32 n_vlib_mains = tm->n_vlib_mains;
338
339 throttle_init (&arp_throttle, n_vlib_mains, 1e-3);
340
341 return (NULL);
342}
343
344VLIB_MAIN_LOOP_ENTER_FUNCTION (ip4_neighbor_main_loop_enter);
345
346
Neale Rannscbe25aa2019-09-30 10:53:31 +0000347/*
348 * fd.io coding-style-patch-verification: ON
349 *
350 * Local Variables:
351 * eval: (c-set-style "gnu")
352 * End:
353 */