blob: 91766b4a6c1fa836de75edbefb73854ed6900409 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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_input.c: IP v4 input node
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
Neale Ranns4c7c8e52017-10-21 09:37:55 -070040#include <vnet/ip/ip4_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070041#include <vnet/ethernet/ethernet.h>
42#include <vnet/ppp/ppp.h>
43#include <vnet/hdlc/hdlc.h>
Neale Rannsc8352bc2018-08-29 10:23:58 -070044#include <vnet/util/throttle.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
Dave Barachd7cb1b52016-12-09 09:52:16 -050046typedef struct
47{
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 u8 packet_data[64];
49} ip4_input_trace_t;
50
Dave Barachd7cb1b52016-12-09 09:52:16 -050051static u8 *
52format_ip4_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053{
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -050056 ip4_input_trace_t *t = va_arg (*va, ip4_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58 s = format (s, "%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -050059 format_ip4_header, t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
61 return s;
62}
Damjan Marion3ade6b62018-05-26 18:53:34 +020063
64static_always_inline u32
65ip4_input_set_next (u32 sw_if_index, vlib_buffer_t * b, int arc_enabled)
66{
67 ip4_main_t *im = &ip4_main;
68 ip_lookup_main_t *lm = &im->lookup_main;
69 u32 next;
70 u8 arc;
71
72 ip4_header_t *ip = vlib_buffer_get_current (b);
73
74 if (PREDICT_FALSE (ip4_address_is_multicast (&ip->dst_address)))
75 {
76 next = IP4_INPUT_NEXT_LOOKUP_MULTICAST;
77 arc = lm->mcast_feature_arc_index;
78 }
79 else
80 {
81 next = IP4_INPUT_NEXT_LOOKUP;
82 arc = lm->ucast_feature_arc_index;
83 }
84
85 if (arc_enabled)
86 vnet_feature_arc_start (arc, sw_if_index, &next, b);
87
88 return next;
89}
90
91static_always_inline void
Damjan Marion067cd622018-07-11 12:47:43 +020092ip4_input_check_sw_if_index (vlib_main_t * vm,
93 vlib_simple_counter_main_t * cm, u32 sw_if_index,
Damjan Marion3ade6b62018-05-26 18:53:34 +020094 u32 * last_sw_if_index, u32 * cnt,
95 int *arc_enabled)
96{
97 ip4_main_t *im = &ip4_main;
98 ip_lookup_main_t *lm = &im->lookup_main;
99 u32 thread_index;
100 if (*last_sw_if_index == sw_if_index)
101 {
102 (*cnt)++;
103 return;
104 }
105
Damjan Marion067cd622018-07-11 12:47:43 +0200106 thread_index = vm->thread_index;
Damjan Marion3ade6b62018-05-26 18:53:34 +0200107 if (*cnt)
108 vlib_increment_simple_counter (cm, thread_index, *last_sw_if_index, *cnt);
109 *cnt = 1;
110 *last_sw_if_index = sw_if_index;
111
112 if (vnet_have_features (lm->ucast_feature_arc_index, sw_if_index) ||
113 vnet_have_features (lm->mcast_feature_arc_index, sw_if_index))
114 *arc_enabled = 1;
115 else
116 *arc_enabled = 0;
117}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119/* Validate IP v4 packets and pass them either to forwarding code
120 or drop/punt exception packets. */
121always_inline uword
122ip4_input_inline (vlib_main_t * vm,
123 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500124 vlib_frame_t * frame, int verify_checksum)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500126 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion3ade6b62018-05-26 18:53:34 +0200127 u32 n_left_from, *from;
Damjan Marion067cd622018-07-11 12:47:43 +0200128 u32 thread_index = vm->thread_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500129 vlib_node_runtime_t *error_node =
130 vlib_node_get_runtime (vm, ip4_input_node.index);
131 vlib_simple_counter_main_t *cm;
Damjan Marion3ade6b62018-05-26 18:53:34 +0200132 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
133 ip4_header_t *ip[4];
134 u16 nexts[VLIB_FRAME_SIZE], *next;
135 u32 sw_if_index[4];
136 u32 last_sw_if_index = ~0;
137 u32 cnt = 0;
138 int arc_enabled = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
140 from = vlib_frame_vector_args (frame);
141 n_left_from = frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
143 if (node->flags & VLIB_NODE_FLAG_TRACE)
144 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
145 /* stride */ 1,
146 sizeof (ip4_input_trace_t));
147
148 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500149 VNET_INTERFACE_COUNTER_IP4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Damjan Marion3ade6b62018-05-26 18:53:34 +0200151 vlib_get_buffers (vm, from, bufs, n_left_from);
152 b = bufs;
153 next = nexts;
154 while (n_left_from >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 {
Damjan Marion3ade6b62018-05-26 18:53:34 +0200156 u32 x = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Damjan Marion3ade6b62018-05-26 18:53:34 +0200158 /* Prefetch next iteration. */
159 if (n_left_from >= 12)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 {
Damjan Marion3ade6b62018-05-26 18:53:34 +0200161 vlib_prefetch_buffer_header (b[8], LOAD);
162 vlib_prefetch_buffer_header (b[9], LOAD);
163 vlib_prefetch_buffer_header (b[10], LOAD);
164 vlib_prefetch_buffer_header (b[11], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Damjan Mariond30bf012018-11-18 23:48:43 +0100166 vlib_prefetch_buffer_data (b[4], LOAD);
167 vlib_prefetch_buffer_data (b[5], LOAD);
168 vlib_prefetch_buffer_data (b[6], LOAD);
169 vlib_prefetch_buffer_data (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 }
171
Damjan Marion3ade6b62018-05-26 18:53:34 +0200172 vnet_buffer (b[0])->ip.adj_index[VLIB_RX] = ~0;
173 vnet_buffer (b[1])->ip.adj_index[VLIB_RX] = ~0;
174 vnet_buffer (b[2])->ip.adj_index[VLIB_RX] = ~0;
175 vnet_buffer (b[3])->ip.adj_index[VLIB_RX] = ~0;
176
177 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
178 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
179 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
180 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
181
182 x |= sw_if_index[0] ^ last_sw_if_index;
183 x |= sw_if_index[1] ^ last_sw_if_index;
184 x |= sw_if_index[2] ^ last_sw_if_index;
185 x |= sw_if_index[3] ^ last_sw_if_index;
186
187 if (PREDICT_TRUE (x == 0))
188 {
189 /* we deal with 4 more packets sharing the same sw_if_index
190 with the previous one, so we can optimize */
191 cnt += 4;
192 if (arc_enabled)
193 {
194 next[0] = ip4_input_set_next (sw_if_index[0], b[0], 1);
195 next[1] = ip4_input_set_next (sw_if_index[1], b[1], 1);
196 next[2] = ip4_input_set_next (sw_if_index[2], b[2], 1);
197 next[3] = ip4_input_set_next (sw_if_index[3], b[3], 1);
198 }
199 else
200 {
201 next[0] = ip4_input_set_next (sw_if_index[0], b[0], 0);
202 next[1] = ip4_input_set_next (sw_if_index[1], b[1], 0);
203 next[2] = ip4_input_set_next (sw_if_index[2], b[2], 0);
204 next[3] = ip4_input_set_next (sw_if_index[3], b[3], 0);
205 }
206 }
207 else
208 {
Damjan Marion067cd622018-07-11 12:47:43 +0200209 ip4_input_check_sw_if_index (vm, cm, sw_if_index[0],
210 &last_sw_if_index, &cnt, &arc_enabled);
211 ip4_input_check_sw_if_index (vm, cm, sw_if_index[1],
212 &last_sw_if_index, &cnt, &arc_enabled);
213 ip4_input_check_sw_if_index (vm, cm, sw_if_index[2],
214 &last_sw_if_index, &cnt, &arc_enabled);
215 ip4_input_check_sw_if_index (vm, cm, sw_if_index[3],
216 &last_sw_if_index, &cnt, &arc_enabled);
Damjan Marion3ade6b62018-05-26 18:53:34 +0200217
218 next[0] = ip4_input_set_next (sw_if_index[0], b[0], 1);
219 next[1] = ip4_input_set_next (sw_if_index[1], b[1], 1);
220 next[2] = ip4_input_set_next (sw_if_index[2], b[2], 1);
221 next[3] = ip4_input_set_next (sw_if_index[3], b[3], 1);
222 }
223
224 ip[0] = vlib_buffer_get_current (b[0]);
225 ip[1] = vlib_buffer_get_current (b[1]);
226 ip[2] = vlib_buffer_get_current (b[2]);
227 ip[3] = vlib_buffer_get_current (b[3]);
228
229 ip4_input_check_x4 (vm, error_node, b, ip, next, verify_checksum);
230
231 /* next */
232 b += 4;
233 next += 4;
234 n_left_from -= 4;
235 }
236 while (n_left_from)
237 {
238 u32 next0;
239 vnet_buffer (b[0])->ip.adj_index[VLIB_RX] = ~0;
240 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
Damjan Marion067cd622018-07-11 12:47:43 +0200241 ip4_input_check_sw_if_index (vm, cm, sw_if_index[0], &last_sw_if_index,
Damjan Marion3ade6b62018-05-26 18:53:34 +0200242 &cnt, &arc_enabled);
243 next0 = ip4_input_set_next (sw_if_index[0], b[0], arc_enabled);
244 ip[0] = vlib_buffer_get_current (b[0]);
245 ip4_input_check_x1 (vm, error_node, b[0], ip[0], &next0,
246 verify_checksum);
247 next[0] = next0;
248
249 /* next */
250 b += 1;
251 next += 1;
252 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 }
254
Damjan Marion3ade6b62018-05-26 18:53:34 +0200255 vlib_increment_simple_counter (cm, thread_index, last_sw_if_index, cnt);
256 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 return frame->n_vectors;
258}
259
Dave Barach132d51d2016-07-07 10:10:17 -0400260/** \brief IPv4 input node.
261 @node ip4-input
262
263 This is the IPv4 input node: validates ip4 header checksums,
264 verifies ip header lengths, discards pkts with expired TTLs,
265 and sends pkts to the set of ip feature nodes configured on
266 the rx interface.
267
268 @param vm vlib_main_t corresponding to the current thread
269 @param node vlib_node_runtime_t
270 @param frame vlib_frame_t whose contents should be dispatched
271
272 @par Graph mechanics: buffer metadata, next index usage
273
274 @em Uses:
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 - vnet_feature_config_main_t cm corresponding to each pkt's dst address unicast /
Dave Barach132d51d2016-07-07 10:10:17 -0400276 multicast status.
277 - <code>b->current_config_index</code> corresponding to each pkt's
Dave Barachd7cb1b52016-12-09 09:52:16 -0500278 rx sw_if_index.
Dave Barach132d51d2016-07-07 10:10:17 -0400279 - This sets the per-packet graph trajectory, ensuring that
280 each packet visits the per-interface features in order.
281
282 - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
283 - Indicates the @c sw_if_index value of the interface that the
284 packet was received on.
285
286 @em Sets:
287 - <code>vnet_buffer(b)->ip.adj_index[VLIB_TX]</code>
288 - The lookup result adjacency index.
289
290 <em>Next Indices:</em>
291 - Dispatches pkts to the (first) feature node:
292 <code> vnet_get_config_data (... &next0 ...); </code>
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 or @c error-drop
Dave Barach132d51d2016-07-07 10:10:17 -0400294*/
Damjan Marion812b32d2018-05-28 21:26:47 +0200295VLIB_NODE_FN (ip4_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
Damjan Marion3ade6b62018-05-26 18:53:34 +0200296 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297{
298 return ip4_input_inline (vm, node, frame, /* verify_checksum */ 1);
299}
300
Damjan Marion812b32d2018-05-28 21:26:47 +0200301VLIB_NODE_FN (ip4_input_no_checksum_node) (vlib_main_t * vm,
Damjan Marion3ade6b62018-05-26 18:53:34 +0200302 vlib_node_runtime_t * node,
303 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304{
305 return ip4_input_inline (vm, node, frame, /* verify_checksum */ 0);
306}
307
Damjan Marion812b32d2018-05-28 21:26:47 +0200308#ifndef CLIB_MARCH_VARIANT
Neale Ranns4c7c8e52017-10-21 09:37:55 -0700309char *ip4_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310#define _(sym,string) string,
311 foreach_ip4_error
312#undef _
313};
Damjan Marion6e363512018-08-10 22:39:11 +0200314#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317VLIB_REGISTER_NODE (ip4_input_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 .name = "ip4-input",
319 .vector_size = sizeof (u32),
Dave Barach7fff3d22018-11-27 16:52:59 -0500320 .protocol_hint = VLIB_NODE_PROTO_HINT_IP4,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
322 .n_errors = IP4_N_ERROR,
323 .error_strings = ip4_error_strings,
324
325 .n_next_nodes = IP4_INPUT_N_NEXT,
326 .next_nodes = {
327 [IP4_INPUT_NEXT_DROP] = "error-drop",
328 [IP4_INPUT_NEXT_PUNT] = "error-punt",
Neale Rannsc667ffd2018-06-27 18:59:03 -0700329 [IP4_INPUT_NEXT_OPTIONS] = "ip4-options",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 [IP4_INPUT_NEXT_LOOKUP] = "ip4-lookup",
Neale Ranns32e1c012016-11-22 17:07:28 +0000331 [IP4_INPUT_NEXT_LOOKUP_MULTICAST] = "ip4-mfib-forward-lookup",
Ole Troan92eade12016-01-13 20:17:08 +0100332 [IP4_INPUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
Klement Sekera75e7d132017-09-20 08:26:30 +0200333 [IP4_INPUT_NEXT_REASSEMBLY] = "ip4-reassembly",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 },
335
336 .format_buffer = format_ip4_header,
337 .format_trace = format_ip4_input_trace,
338};
339
Damjan Marion812b32d2018-05-28 21:26:47 +0200340VLIB_REGISTER_NODE (ip4_input_no_checksum_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 .name = "ip4-input-no-checksum",
342 .vector_size = sizeof (u32),
343
Neale Ranns8a03e4f2018-07-17 07:15:05 -0700344 .sibling_of = "ip4-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 .format_buffer = format_ip4_header,
346 .format_trace = format_ip4_input_trace,
347};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500348/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350static clib_error_t *
351ip4_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barachd7cb1b52016-12-09 09:52:16 -0500355 ethernet_register_input_type (vm, ETHERNET_TYPE_IP4, ip4_input_node.index);
356 ppp_register_input_protocol (vm, PPP_PROTOCOL_ip4, ip4_input_node.index);
357 hdlc_register_input_protocol (vm, HDLC_PROTOCOL_ip4, ip4_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
359 {
Damjan Marion6e363512018-08-10 22:39:11 +0200360 extern vlib_node_registration_t ip4_input_no_checksum_node;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 pn = pg_get_node (ip4_input_node.index);
363 pn->unformat_edit = unformat_pg_ip4_header;
364 pn = pg_get_node (ip4_input_no_checksum_node.index);
365 pn->unformat_edit = unformat_pg_ip4_header;
366 }
367
368 if ((error = vlib_call_init_function (vm, ip4_cli_init)))
369 return error;
370
371 if ((error = vlib_call_init_function (vm, ip4_source_check_init)))
372 return error;
373
Dave Barachd7cb1b52016-12-09 09:52:16 -0500374 if ((error = vlib_call_init_function
Dave Barach6f9bca22016-04-30 10:25:32 -0400375 (vm, ip4_source_and_port_range_check_init)))
376 return error;
377
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 /* Set flow hash to something non-zero. */
379 ip4_main.flow_hash_seed = 0xdeadbeef;
380
381 /* Default TTL for packets we generate. */
382 ip4_main.host_config.ttl = 64;
383
384 return error;
385}
386
387VLIB_INIT_FUNCTION (ip4_init);
Dave Barach49433ad2018-08-08 17:59:03 -0400388
389static clib_error_t *
390ip4_main_loop_enter (vlib_main_t * vm)
391{
392 ip4_main_t *im = &ip4_main;
393 vlib_thread_main_t *tm = &vlib_thread_main;
394 u32 n_vlib_mains = tm->n_vlib_mains;
Dave Barach49433ad2018-08-08 17:59:03 -0400395
Neale Rannsc8352bc2018-08-29 10:23:58 -0700396 throttle_init (&im->arp_throttle, n_vlib_mains, 1e-3);
Dave Barach49433ad2018-08-08 17:59:03 -0400397
Neale Rannsc8352bc2018-08-29 10:23:58 -0700398 return (NULL);
Dave Barach49433ad2018-08-08 17:59:03 -0400399}
400
401VLIB_MAIN_LOOP_ENTER_FUNCTION (ip4_main_loop_enter);
402
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403/*
404 * fd.io coding-style-patch-verification: ON
405 *
406 * Local Variables:
407 * eval: (c-set-style "gnu")
408 * End:
409 */