misc: Break the big IP header files to improve compile time
[vpp.git] / src / vnet / ip-neighbor / ip4_neighbor.c
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
40 #include <vnet/ip-neighbor/ip4_neighbor.h>
41 #include <vnet/ethernet/ethernet.h>
42 #include <vnet/util/throttle.h>
43
44 /** ARP throttling */
45 static throttle_t arp_throttle;
46
47 void
48 ip4_neighbor_probe_dst (const ip_adjacency_t * adj, const ip4_address_t * dst)
49 {
50   ip_interface_address_t *ia;
51   ip4_address_t *src;
52
53   src = ip4_interface_address_matching_destination
54     (&ip4_main,
55      &adj->sub_type.nbr.next_hop.ip4, adj->rewrite_header.sw_if_index, &ia);
56   if (!src)
57     return;
58
59   ip4_neighbor_probe (vlib_get_main (), vnet_get_main (), adj, src, dst);
60 }
61
62 void
63 ip4_neighbor_advertise (vlib_main_t * vm,
64                         vnet_main_t * vnm,
65                         u32 sw_if_index, const ip4_address_t * addr)
66 {
67   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
68   ip4_main_t *i4m = &ip4_main;
69   u8 *rewrite, rewrite_len;
70
71   if (NULL == addr)
72     {
73       ip4_main_t *i4m = &ip4_main;
74       addr = ip4_interface_first_address (i4m, sw_if_index, 0);
75     }
76
77   if (addr)
78     {
79       clib_warning ("Sending GARP for IP4 address %U on sw_if_idex %d",
80                     format_ip4_address, addr, sw_if_index);
81
82       /* Form GARP packet for output - Gratuitous ARP is an ARP request packet
83          where the interface IP/MAC pair is used for both source and request
84          MAC/IP pairs in the request */
85       u32 bi = 0;
86       ethernet_arp_header_t *h = vlib_packet_template_get_packet
87         (vm, &i4m->ip4_arp_request_packet_template, &bi);
88
89       if (!h)
90         return;
91
92       mac_address_from_bytes (&h->ip4_over_ethernet[0].mac, hi->hw_address);
93       mac_address_from_bytes (&h->ip4_over_ethernet[1].mac, hi->hw_address);
94       h->ip4_over_ethernet[0].ip4 = addr[0];
95       h->ip4_over_ethernet[1].ip4 = addr[0];
96
97       /* Setup MAC header with ARP Etype and broadcast DMAC */
98       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
99       rewrite =
100         ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_ARP,
101                                 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST);
102       rewrite_len = vec_len (rewrite);
103       vlib_buffer_advance (b, -rewrite_len);
104       ethernet_header_t *e = vlib_buffer_get_current (b);
105       clib_memcpy_fast (e->dst_address, rewrite, rewrite_len);
106       vec_free (rewrite);
107
108       /* Send GARP packet out the specified interface */
109       vnet_buffer (b)->sw_if_index[VLIB_RX] =
110         vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
111       vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
112       u32 *to_next = vlib_frame_vector_args (f);
113       to_next[0] = bi;
114       f->n_vectors = 1;
115       vlib_put_frame_to_node (vm, hi->output_node_index, f);
116     }
117 }
118
119 always_inline uword
120 ip4_arp_inline (vlib_main_t * vm,
121                 vlib_node_runtime_t * node,
122                 vlib_frame_t * frame, int is_glean)
123 {
124   vnet_main_t *vnm = vnet_get_main ();
125   ip4_main_t *im = &ip4_main;
126   ip_lookup_main_t *lm = &im->lookup_main;
127   u32 *from, *to_next_drop;
128   uword n_left_from, n_left_to_next_drop, next_index;
129   u32 thread_index = vm->thread_index;
130   u64 seed;
131
132   if (node->flags & VLIB_NODE_FLAG_TRACE)
133     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
134
135   seed = throttle_seed (&arp_throttle, thread_index, vlib_time_now (vm));
136
137   from = vlib_frame_vector_args (frame);
138   n_left_from = frame->n_vectors;
139   next_index = node->cached_next_index;
140   if (next_index == IP4_ARP_NEXT_DROP)
141     next_index = IP4_ARP_N_NEXT;        /* point to first interface */
142
143   while (n_left_from > 0)
144     {
145       vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP,
146                            to_next_drop, n_left_to_next_drop);
147
148       while (n_left_from > 0 && n_left_to_next_drop > 0)
149         {
150           u32 pi0, adj_index0, sw_if_index0;
151           ip4_address_t resolve0, src0;
152           vlib_buffer_t *p0, *b0;
153           ip_adjacency_t *adj0;
154           u64 r0;
155
156           pi0 = from[0];
157           p0 = vlib_get_buffer (vm, pi0);
158
159           from += 1;
160           n_left_from -= 1;
161           to_next_drop[0] = pi0;
162           to_next_drop += 1;
163           n_left_to_next_drop -= 1;
164
165           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
166           adj0 = adj_get (adj_index0);
167           sw_if_index0 = adj0->rewrite_header.sw_if_index;
168
169           if (is_glean)
170             {
171               /* resolve the packet's destination */
172               ip4_header_t *ip0 = vlib_buffer_get_current (p0);
173               resolve0 = ip0->dst_address;
174               src0 = adj0->sub_type.glean.receive_addr.ip4;
175             }
176           else
177             {
178               /* resolve the incomplete adj */
179               resolve0 = adj0->sub_type.nbr.next_hop.ip4;
180               /* Src IP address in ARP header. */
181               if (ip4_src_address_for_packet (lm, sw_if_index0, &src0))
182                 {
183                   /* No source address available */
184                   p0->error = node->errors[IP4_ARP_ERROR_NO_SOURCE_ADDRESS];
185                   continue;
186                 }
187             }
188
189           /* combine the address and interface for the hash key */
190           r0 = (u64) resolve0.data_u32 << 32;
191           r0 |= sw_if_index0;
192
193           if (throttle_check (&arp_throttle, thread_index, r0, seed))
194             {
195               p0->error = node->errors[IP4_ARP_ERROR_THROTTLED];
196               continue;
197             }
198
199           /*
200            * the adj has been updated to a rewrite but the node the DPO that got
201            * us here hasn't - yet. no big deal. we'll drop while we wait.
202            */
203           if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
204             {
205               p0->error = node->errors[IP4_ARP_ERROR_RESOLVED];
206               continue;
207             }
208
209           /*
210            * Can happen if the control-plane is programming tables
211            * with traffic flowing; at least that's today's lame excuse.
212            */
213           if ((is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN)
214               || (!is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP))
215             {
216               p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ];
217               continue;
218             }
219
220           /* Send ARP request. */
221           b0 = ip4_neighbor_probe (vm, vnm, adj0, &src0, &resolve0);
222
223           if (PREDICT_TRUE (NULL != b0))
224             {
225               /* copy the persistent fields from the original */
226               clib_memcpy_fast (b0->opaque2, p0->opaque2,
227                                 sizeof (p0->opaque2));
228               p0->error = node->errors[IP4_ARP_ERROR_REQUEST_SENT];
229             }
230           else
231             {
232               p0->error = node->errors[IP4_ARP_ERROR_NO_BUFFERS];
233               continue;
234             }
235         }
236
237       vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop);
238     }
239
240   return frame->n_vectors;
241 }
242
243 VLIB_NODE_FN (ip4_arp_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
244                              vlib_frame_t * frame)
245 {
246   return (ip4_arp_inline (vm, node, frame, 0));
247 }
248
249 VLIB_NODE_FN (ip4_glean_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
250                                vlib_frame_t * frame)
251 {
252   return (ip4_arp_inline (vm, node, frame, 1));
253 }
254
255 static char *ip4_arp_error_strings[] = {
256   [IP4_ARP_ERROR_THROTTLED] = "ARP requests throttled",
257   [IP4_ARP_ERROR_RESOLVED] = "ARP requests resolved",
258   [IP4_ARP_ERROR_NO_BUFFERS] = "ARP requests out of buffer",
259   [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent",
260   [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies",
261   [IP4_ARP_ERROR_NO_SOURCE_ADDRESS] = "no source address for ARP request",
262 };
263
264 /* *INDENT-OFF* */
265 VLIB_REGISTER_NODE (ip4_arp_node) =
266 {
267   .name = "ip4-arp",
268   .vector_size = sizeof (u32),
269   .format_trace = format_ip4_forward_next_trace,
270   .n_errors = ARRAY_LEN (ip4_arp_error_strings),
271   .error_strings = ip4_arp_error_strings,
272   .n_next_nodes = IP4_ARP_N_NEXT,
273   .next_nodes = {
274     [IP4_ARP_NEXT_DROP] = "ip4-drop",
275   },
276 };
277
278 VLIB_REGISTER_NODE (ip4_glean_node) =
279 {
280   .name = "ip4-glean",
281   .vector_size = sizeof (u32),
282   .format_trace = format_ip4_forward_next_trace,
283   .n_errors = ARRAY_LEN (ip4_arp_error_strings),
284   .error_strings = ip4_arp_error_strings,
285   .n_next_nodes = IP4_ARP_N_NEXT,
286   .next_nodes = {
287     [IP4_ARP_NEXT_DROP] = "ip4-drop",
288   },
289 };
290 /* *INDENT-ON* */
291
292 #define foreach_notrace_ip4_arp_error           \
293 _(THROTTLED)                                    \
294 _(RESOLVED)                                     \
295 _(NO_BUFFERS)                                   \
296 _(REQUEST_SENT)                                 \
297 _(NON_ARP_ADJ)                                  \
298 _(NO_SOURCE_ADDRESS)
299
300 static clib_error_t *
301 arp_notrace_init (vlib_main_t * vm)
302 {
303   vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, ip4_arp_node.index);
304
305   /* don't trace ARP request packets */
306 #define _(a)                                    \
307     vnet_pcap_drop_trace_filter_add_del         \
308         (rt->errors[IP4_ARP_ERROR_##a],         \
309          1 /* is_add */);
310   foreach_notrace_ip4_arp_error;
311 #undef _
312   return 0;
313 }
314
315 VLIB_INIT_FUNCTION (arp_notrace_init);
316
317 static clib_error_t *
318 ip4_neighbor_main_loop_enter (vlib_main_t * vm)
319 {
320   vlib_thread_main_t *tm = &vlib_thread_main;
321   u32 n_vlib_mains = tm->n_vlib_mains;
322
323   throttle_init (&arp_throttle, n_vlib_mains, 1e-3);
324
325   return (NULL);
326 }
327
328 VLIB_MAIN_LOOP_ENTER_FUNCTION (ip4_neighbor_main_loop_enter);
329
330
331 /*
332  * fd.io coding-style-patch-verification: ON
333  *
334  * Local Variables:
335  * eval: (c-set-style "gnu")
336  * End:
337  */