FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
ip4_neighbor.c
Go to the documentation of this file.
1 /*
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 
41 #include <vnet/ethernet/ethernet.h>
42 
43 void
45 {
48 
50  (&ip4_main,
51  &adj->sub_type.nbr.next_hop.ip4, adj->rewrite_header.sw_if_index, &ia);
52  if (!src)
53  return;
54 
55  ip4_neighbor_probe (vlib_get_main (), vnet_get_main (), adj, src, dst);
56 }
57 
58 void
60  vnet_main_t * vnm,
62 {
63  vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
64  ip4_main_t *i4m = &ip4_main;
65  u8 *rewrite, rewrite_len;
66 
67  if (NULL == addr)
68  {
69  ip4_main_t *i4m = &ip4_main;
70  addr = ip4_interface_first_address (i4m, sw_if_index, 0);
71  }
72 
73  if (addr)
74  {
75  clib_warning ("Sending GARP for IP4 address %U on sw_if_idex %d",
76  format_ip4_address, addr, sw_if_index);
77 
78  /* Form GARP packet for output - Gratuitous ARP is an ARP request packet
79  where the interface IP/MAC pair is used for both source and request
80  MAC/IP pairs in the request */
81  u32 bi = 0;
83  (vm, &i4m->ip4_arp_request_packet_template, &bi);
84 
85  if (!h)
86  return;
87 
90  h->ip4_over_ethernet[0].ip4 = addr[0];
91  h->ip4_over_ethernet[1].ip4 = addr[0];
92 
93  /* Setup MAC header with ARP Etype and broadcast DMAC */
94  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
95  rewrite =
96  ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_ARP,
98  rewrite_len = vec_len (rewrite);
99  vlib_buffer_advance (b, -rewrite_len);
101  clib_memcpy_fast (e->dst_address, rewrite, rewrite_len);
102  vec_free (rewrite);
103 
104  /* Send GARP packet out the specified interface */
105  vnet_buffer (b)->sw_if_index[VLIB_RX] =
106  vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
108  u32 *to_next = vlib_frame_vector_args (f);
109  to_next[0] = bi;
110  f->n_vectors = 1;
112  }
113 }
114 
118  vlib_frame_t * frame, int is_glean)
119 {
120  vnet_main_t *vnm = vnet_get_main ();
121  ip4_main_t *im = &ip4_main;
122  ip_lookup_main_t *lm = &im->lookup_main;
123  u32 *from, *to_next_drop;
124  uword n_left_from, n_left_to_next_drop, next_index;
125  u32 thread_index = vm->thread_index;
126  u64 seed;
127 
128  if (node->flags & VLIB_NODE_FLAG_TRACE)
129  ip4_forward_next_trace (vm, node, frame, VLIB_TX);
130 
131  seed = throttle_seed (&im->arp_throttle, thread_index, vlib_time_now (vm));
132 
133  from = vlib_frame_vector_args (frame);
134  n_left_from = frame->n_vectors;
135  next_index = node->cached_next_index;
136  if (next_index == IP4_ARP_NEXT_DROP)
137  next_index = IP4_ARP_N_NEXT; /* point to first interface */
138 
139  while (n_left_from > 0)
140  {
142  to_next_drop, n_left_to_next_drop);
143 
144  while (n_left_from > 0 && n_left_to_next_drop > 0)
145  {
146  u32 pi0, adj_index0, sw_if_index0;
147  ip4_address_t resolve0, src0;
148  vlib_buffer_t *p0, *b0;
149  ip_adjacency_t *adj0;
150  u64 r0;
151 
152  pi0 = from[0];
153  p0 = vlib_get_buffer (vm, pi0);
154 
155  from += 1;
156  n_left_from -= 1;
157  to_next_drop[0] = pi0;
158  to_next_drop += 1;
159  n_left_to_next_drop -= 1;
160 
161  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
162  adj0 = adj_get (adj_index0);
163  sw_if_index0 = adj0->rewrite_header.sw_if_index;
164 
165  if (is_glean)
166  {
167  /* resolve the packet's destination */
169  resolve0 = ip0->dst_address;
170  src0 = adj0->sub_type.glean.receive_addr.ip4;
171  }
172  else
173  {
174  /* resolve the incomplete adj */
175  resolve0 = adj0->sub_type.nbr.next_hop.ip4;
176  /* Src IP address in ARP header. */
177  if (ip4_src_address_for_packet (lm, sw_if_index0, &src0))
178  {
179  /* No source address available */
181  continue;
182  }
183  }
184 
185  /* combine the address and interface for the hash key */
186  r0 = (u64) resolve0.data_u32 << 32;
187  r0 |= sw_if_index0;
188 
189  if (throttle_check (&im->arp_throttle, thread_index, r0, seed))
190  {
191  p0->error = node->errors[IP4_ARP_ERROR_THROTTLED];
192  continue;
193  }
194 
195  /*
196  * the adj has been updated to a rewrite but the node the DPO that got
197  * us here hasn't - yet. no big deal. we'll drop while we wait.
198  */
200  {
201  p0->error = node->errors[IP4_ARP_ERROR_RESOLVED];
202  continue;
203  }
204 
205  /*
206  * Can happen if the control-plane is programming tables
207  * with traffic flowing; at least that's today's lame excuse.
208  */
209  if ((is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN)
210  || (!is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP))
211  {
213  continue;
214  }
215 
216  /* Send ARP request. */
217  b0 = ip4_neighbor_probe (vm, vnm, adj0, &src0, &resolve0);
218 
219  if (PREDICT_TRUE (NULL != b0))
220  {
221  /* copy the persistent fields from the original */
222  clib_memcpy_fast (b0->opaque2, p0->opaque2,
223  sizeof (p0->opaque2));
225  }
226  else
227  {
229  continue;
230  }
231  }
232 
233  vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
234  }
235 
236  return frame->n_vectors;
237 }
238 
241 {
242  return (ip4_arp_inline (vm, node, frame, 0));
243 }
244 
247 {
248  return (ip4_arp_inline (vm, node, frame, 1));
249 }
250 
251 static char *ip4_arp_error_strings[] = {
252  [IP4_ARP_ERROR_THROTTLED] = "ARP requests throttled",
253  [IP4_ARP_ERROR_RESOLVED] = "ARP requests resolved",
254  [IP4_ARP_ERROR_NO_BUFFERS] = "ARP requests out of buffer",
255  [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
256  [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
257  [IP4_ARP_ERROR_NO_SOURCE_ADDRESS] = "no source address for ARP request",
258 };
259 
260 /* *INDENT-OFF* */
262 {
263  .name = "ip4-arp",
264  .vector_size = sizeof (u32),
265  .format_trace = format_ip4_forward_next_trace,
266  .n_errors = ARRAY_LEN (ip4_arp_error_strings),
267  .error_strings = ip4_arp_error_strings,
268  .n_next_nodes = IP4_ARP_N_NEXT,
269  .next_nodes = {
270  [IP4_ARP_NEXT_DROP] = "ip4-drop",
271  },
272 };
273 
275 {
276  .name = "ip4-glean",
277  .vector_size = sizeof (u32),
278  .format_trace = format_ip4_forward_next_trace,
279  .n_errors = ARRAY_LEN (ip4_arp_error_strings),
280  .error_strings = ip4_arp_error_strings,
281  .n_next_nodes = IP4_ARP_N_NEXT,
282  .next_nodes = {
283  [IP4_ARP_NEXT_DROP] = "ip4-drop",
284  },
285 };
286 /* *INDENT-ON* */
287 
288 #define foreach_notrace_ip4_arp_error \
289 _(THROTTLED) \
290 _(RESOLVED) \
291 _(NO_BUFFERS) \
292 _(REQUEST_SENT) \
293 _(NON_ARP_ADJ) \
294 _(NO_SOURCE_ADDRESS)
295 
296 static clib_error_t *
298 {
300 
301  /* don't trace ARP request packets */
302 #define _(a) \
303  vnet_pcap_drop_trace_filter_add_del \
304  (rt->errors[IP4_ARP_ERROR_##a], \
305  1 /* is_add */);
307 #undef _
308  return 0;
309 }
310 
312 
313 /*
314  * fd.io coding-style-patch-verification: ON
315  *
316  * Local Variables:
317  * eval: (c-set-style "gnu")
318  * End:
319  */
u32 opaque2[14]
Definition: buffer.h:170
#define VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST
Definition: rewrite.h:234
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define PREDICT_TRUE(x)
Definition: clib.h:119
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:281
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:291
IP unicast adjacency.
Definition: adj.h:227
u32 thread_index
Definition: main.h:218
This packet is to be rewritten and forwarded to the next processing node.
Definition: adj.h:73
vl_api_address_t src
Definition: gre.api:54
ip_lookup_main_t lookup_main
Definition: ip4.h:108
union ip_adjacency_t_::@137 sub_type
#define VLIB_NODE_FN(node)
Definition: node.h:202
void ip4_neighbor_probe_dst(const ip_adjacency_t *adj, const ip4_address_t *dst)
Definition: ip4_neighbor.c:44
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:472
vhost_vring_addr_t addr
Definition: vhost_user.h:254
unsigned char u8
Definition: types.h:56
format_function_t format_ip4_address
Definition: format.h:73
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:459
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:400
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
ethernet_arp_ip4_over_ethernet_address_t ip4_over_ethernet[2]
Definition: arp_packet.h:142
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
ip4_address_t dst_address
Definition: ip4_packet.h:170
u8 dst_address[6]
Definition: packet.h:55
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
unsigned int u32
Definition: types.h:88
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
static_always_inline void mac_address_from_bytes(mac_address_t *mac, const u8 *bytes)
Definition: mac_address.h:92
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:216
vec_header_t h
Definition: buffer.c:322
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
static clib_error_t * arp_notrace_init(vlib_main_t *vm)
Definition: ip4_neighbor.c:297
static u64 throttle_seed(throttle_t *t, u32 thread_index, f64 time_now)
Definition: throttle.h:41
This packet matches an "interface route" and packets need to be passed to ARP to find rewrite string ...
Definition: adj.h:68
#define always_inline
Definition: ipsec.h:28
vl_api_address_t dst
Definition: gre.api:55
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
vlib_main_t * vm
Definition: in2out_ed.c:1599
static uword ip4_arp_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_glean)
Definition: ip4_neighbor.c:116
This packet matches an "incomplete adjacency" and packets need to be passed to ARP to find rewrite st...
Definition: adj.h:63
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:399
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static ip4_address_t * ip4_interface_address_matching_destination(ip4_main_t *im, const ip4_address_t *dst, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4.h:235
#define clib_warning(format, args...)
Definition: error.h:59
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:89
void ip4_neighbor_advertise(vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index, const ip4_address_t *addr)
Definition: ip4_neighbor.c:59
#define ARRAY_LEN(x)
Definition: clib.h:66
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:483
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
static vlib_buffer_t * ip4_neighbor_probe(vlib_main_t *vm, vnet_main_t *vnm, const ip_adjacency_t *adj0, const ip4_address_t *src, const ip4_address_t *dst)
Definition: ip4_neighbor.h:30
vlib_packet_template_t ip4_arp_request_packet_template
Template used to generate IP4 ARP packets.
Definition: ip4.h:150
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:517
struct ip_adjacency_t_::@137::@138 nbr
IP_LOOKUP_NEXT_ARP/IP_LOOKUP_NEXT_REWRITE.
IPv4 main type.
Definition: ip4.h:106
throttle_t arp_throttle
ARP throttling.
Definition: ip4.h:174
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
struct ip_adjacency_t_::@137::@140 glean
IP_LOOKUP_NEXT_GLEAN.
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_ip4_address_t hi
Definition: arp.api:37
Definition: defs.h:47
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
ip_lookup_next_t lookup_next_index
Next hop after ip4-lookup.
Definition: adj.h:329
vlib_node_registration_t ip4_arp_node
(constructor) VLIB_REGISTER_NODE (ip4_arp_node)
Definition: ip4_neighbor.c:261
void ip4_forward_next_trace(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vlib_rx_or_tx_t which_adj_index)
Definition: ip4_forward.c:1279
VLIB buffer representation.
Definition: buffer.h:102
#define foreach_notrace_ip4_arp_error
Definition: ip4_neighbor.c:288
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
u8 * format_ip4_forward_next_trace(u8 *s, va_list *args)
Definition: ip4_forward.c:1229
u8 * ethernet_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
build a rewrite string to use for sending packets of type &#39;link_type&#39; to &#39;dst_address&#39; ...
Definition: interface.c:83
#define vnet_buffer(b)
Definition: buffer.h:417
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1144
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1600
u16 flags
Copy of main node flags.
Definition: node.h:511
vlib_node_registration_t ip4_glean_node
(constructor) VLIB_REGISTER_NODE (ip4_glean_node)
Definition: ip4_neighbor.c:274
static char * ip4_arp_error_strings[]
Definition: ip4_neighbor.c:251
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:304
static int ip4_src_address_for_packet(ip_lookup_main_t *lm, u32 sw_if_index, ip4_address_t *src)
Definition: ip4.h:214
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
Definition: defs.h:46
static int throttle_check(throttle_t *t, u32 thread_index, u64 hash, u64 seed)
Definition: throttle.h:54