2 * Copyright (c) 2018 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.
17 * @brief Classify for one armed NAT44 (in+out interface)
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/fib/ip4_fib.h>
24 #include <nat/nat_inlines.h>
25 #include <nat/nat44/ed_inlines.h>
27 #define foreach_nat44_classify_error \
28 _(NEXT_IN2OUT, "next in2out") \
29 _(NEXT_OUT2IN, "next out2in") \
30 _(FRAG_CACHED, "fragment cached")
34 #define _(sym,str) NAT44_CLASSIFY_ERROR_##sym,
35 foreach_nat44_classify_error
37 NAT44_CLASSIFY_N_ERROR,
38 } nat44_classify_error_t;
40 static char *nat44_classify_error_strings[] = {
41 #define _(sym,string) string,
42 foreach_nat44_classify_error
48 NAT44_CLASSIFY_NEXT_IN2OUT,
49 NAT44_CLASSIFY_NEXT_OUT2IN,
50 NAT44_CLASSIFY_NEXT_DROP,
51 NAT44_CLASSIFY_N_NEXT,
52 } nat44_classify_next_t;
58 } nat44_classify_trace_t;
61 format_nat44_classify_trace (u8 * s, va_list * args)
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65 nat44_classify_trace_t *t = va_arg (*args, nat44_classify_trace_t *);
69 s = format (s, "nat44-classify: fragment cached");
72 next = t->next_in2out ? "nat44-in2out" : "nat44-out2in";
73 s = format (s, "nat44-classify: next %s", next);
80 nat44_classify_node_fn_inline (vlib_main_t * vm,
81 vlib_node_runtime_t * node,
84 u32 n_left_from, *from, *to_next;
85 nat44_classify_next_t next_index;
86 snat_main_t *sm = &snat_main;
87 snat_static_mapping_t *m;
88 u32 next_in2out = 0, next_out2in = 0;
90 from = vlib_frame_vector_args (frame);
91 n_left_from = frame->n_vectors;
92 next_index = node->cached_next_index;
94 while (n_left_from > 0)
98 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
100 while (n_left_from > 0 && n_left_to_next > 0)
104 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT;
107 clib_bihash_kv_8_8_t kv0, value0;
109 /* speculatively enqueue b0 to the current next frame */
117 b0 = vlib_get_buffer (vm, bi0);
118 ip0 = vlib_buffer_get_current (b0);
121 vec_foreach (ap, sm->addresses)
123 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
125 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
131 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
133 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
134 /* try to classify the fragment based on IP header alone */
135 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external,
138 m = pool_elt_at_index (sm->static_mappings, value0.value);
139 if (m->local_addr.as_u32 != m->external_addr.as_u32)
140 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
143 init_nat_k (&kv0, ip0->dst_address,
144 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
145 ip_proto_to_nat_proto (ip0->protocol));
146 if (!clib_bihash_search_8_8
147 (&sm->static_mapping_by_external, &kv0, &value0))
149 m = pool_elt_at_index (sm->static_mappings, value0.value);
150 if (m->local_addr.as_u32 != m->external_addr.as_u32)
151 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
156 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
157 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
159 nat44_classify_trace_t *t =
160 vlib_add_trace (vm, node, b0, sizeof (*t));
162 t->next_in2out = next0 == NAT44_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
165 next_in2out += next0 == NAT44_CLASSIFY_NEXT_IN2OUT;
166 next_out2in += next0 == NAT44_CLASSIFY_NEXT_OUT2IN;
168 /* verify speculative enqueue, maybe switch current next frame */
169 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
170 to_next, n_left_to_next,
174 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
177 vlib_node_increment_counter (vm, node->node_index,
178 NAT44_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
179 vlib_node_increment_counter (vm, node->node_index,
180 NAT44_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
181 return frame->n_vectors;
185 nat44_handoff_classify_node_fn_inline (vlib_main_t * vm,
186 vlib_node_runtime_t * node,
187 vlib_frame_t * frame)
189 u32 n_left_from, *from, *to_next;
190 nat44_classify_next_t next_index;
191 snat_main_t *sm = &snat_main;
192 snat_static_mapping_t *m;
193 u32 next_in2out = 0, next_out2in = 0;
195 from = vlib_frame_vector_args (frame);
196 n_left_from = frame->n_vectors;
197 next_index = node->cached_next_index;
199 while (n_left_from > 0)
203 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
205 while (n_left_from > 0 && n_left_to_next > 0)
209 u32 next0 = NAT_NEXT_IN2OUT_CLASSIFY;
212 clib_bihash_kv_8_8_t kv0, value0;
214 /* speculatively enqueue b0 to the current next frame */
222 b0 = vlib_get_buffer (vm, bi0);
223 ip0 = vlib_buffer_get_current (b0);
226 vec_foreach (ap, sm->addresses)
228 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
230 next0 = NAT_NEXT_OUT2IN_CLASSIFY;
236 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
238 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
239 /* try to classify the fragment based on IP header alone */
240 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external,
243 m = pool_elt_at_index (sm->static_mappings, value0.value);
244 if (m->local_addr.as_u32 != m->external_addr.as_u32)
245 next0 = NAT_NEXT_OUT2IN_CLASSIFY;
248 init_nat_k (&kv0, ip0->dst_address,
249 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
250 ip_proto_to_nat_proto (ip0->protocol));
251 if (!clib_bihash_search_8_8
252 (&sm->static_mapping_by_external, &kv0, &value0))
254 m = pool_elt_at_index (sm->static_mappings, value0.value);
255 if (m->local_addr.as_u32 != m->external_addr.as_u32)
256 next0 = NAT_NEXT_OUT2IN_CLASSIFY;
261 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
262 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
264 nat44_classify_trace_t *t =
265 vlib_add_trace (vm, node, b0, sizeof (*t));
267 t->next_in2out = next0 == NAT_NEXT_IN2OUT_CLASSIFY ? 1 : 0;
270 next_in2out += next0 == NAT_NEXT_IN2OUT_CLASSIFY;
271 next_out2in += next0 == NAT_NEXT_OUT2IN_CLASSIFY;
273 /* verify speculative enqueue, maybe switch current next frame */
274 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
275 to_next, n_left_to_next,
279 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
282 vlib_node_increment_counter (vm, node->node_index,
283 NAT44_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
284 vlib_node_increment_counter (vm, node->node_index,
285 NAT44_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
286 return frame->n_vectors;
290 nat44_ed_classify_node_fn_inline (vlib_main_t * vm,
291 vlib_node_runtime_t * node,
292 vlib_frame_t * frame)
294 u32 n_left_from, *from, *to_next;
295 nat44_classify_next_t next_index;
296 snat_main_t *sm = &snat_main;
297 snat_static_mapping_t *m;
298 u32 next_in2out = 0, next_out2in = 0;
300 from = vlib_frame_vector_args (frame);
301 n_left_from = frame->n_vectors;
302 next_index = node->cached_next_index;
304 while (n_left_from > 0)
308 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
310 while (n_left_from > 0 && n_left_to_next > 0)
314 u32 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
315 u32 sw_if_index0, rx_fib_index0;
318 clib_bihash_kv_8_8_t kv0, value0;
319 clib_bihash_kv_16_8_t ed_kv0, ed_value0;
321 /* speculatively enqueue b0 to the current next frame */
329 b0 = vlib_get_buffer (vm, bi0);
330 ip0 = vlib_buffer_get_current (b0);
333 vnet_feature_next (&arc_next, b0);
334 vnet_buffer2 (b0)->nat.arc_next = arc_next;
336 if (ip0->protocol != IP_PROTOCOL_ICMP)
338 /* process leading fragment/whole packet (with L4 header) */
339 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
341 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
343 init_ed_k (&ed_kv0, ip0->src_address,
344 vnet_buffer (b0)->ip.reass.l4_src_port,
346 vnet_buffer (b0)->ip.reass.l4_dst_port,
347 rx_fib_index0, ip0->protocol);
348 /* process whole packet */
349 if (!clib_bihash_search_16_8 (&sm->flow_hash, &ed_kv0,
352 ASSERT (vm->thread_index ==
353 ed_value_get_thread_index (&ed_value0));
354 snat_main_per_thread_data_t *tsm =
355 &sm->per_thread_data[vm->thread_index];
356 snat_session_t *s = pool_elt_at_index (
357 tsm->sessions, ed_value_get_session_index (&ed_value0));
358 clib_bihash_kv_16_8_t i2o_kv;
359 nat_6t_flow_to_ed_k (&i2o_kv, &s->i2o);
360 vnet_buffer2 (b0)->nat.cached_session_index =
361 ed_value_get_session_index (&ed_value0);
362 if (i2o_kv.key[0] == ed_kv0.key[0] &&
363 i2o_kv.key[1] == ed_kv0.key[1])
365 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
369 next0 = NAT_NEXT_OUT2IN_ED_FAST_PATH;
374 /* session doesn't exist so continue in code */
378 vec_foreach (ap, sm->addresses)
380 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
382 next0 = NAT_NEXT_OUT2IN_ED_FAST_PATH;
388 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
390 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
391 /* try to classify the fragment based on IP header alone */
392 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external,
395 m = pool_elt_at_index (sm->static_mappings, value0.value);
396 if (m->local_addr.as_u32 != m->external_addr.as_u32)
397 next0 = NAT_NEXT_OUT2IN_ED_FAST_PATH;
400 init_nat_k (&kv0, ip0->dst_address,
401 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
402 ip_proto_to_nat_proto (ip0->protocol));
403 if (!clib_bihash_search_8_8
404 (&sm->static_mapping_by_external, &kv0, &value0))
406 m = pool_elt_at_index (sm->static_mappings, value0.value);
407 if (m->local_addr.as_u32 != m->external_addr.as_u32)
408 next0 = NAT_NEXT_OUT2IN_ED_FAST_PATH;
413 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
414 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
416 nat44_classify_trace_t *t =
417 vlib_add_trace (vm, node, b0, sizeof (*t));
419 t->next_in2out = next0 == NAT_NEXT_IN2OUT_ED_FAST_PATH ? 1 : 0;
422 next_in2out += next0 == NAT_NEXT_IN2OUT_ED_FAST_PATH;
423 next_out2in += next0 == NAT_NEXT_OUT2IN_ED_FAST_PATH;
425 /* verify speculative enqueue, maybe switch current next frame */
426 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
427 to_next, n_left_to_next,
431 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
434 vlib_node_increment_counter (vm, node->node_index,
435 NAT44_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
436 vlib_node_increment_counter (vm, node->node_index,
437 NAT44_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
438 return frame->n_vectors;
441 VLIB_NODE_FN (nat44_classify_node) (vlib_main_t * vm,
442 vlib_node_runtime_t * node,
443 vlib_frame_t * frame)
445 return nat44_classify_node_fn_inline (vm, node, frame);
449 VLIB_REGISTER_NODE (nat44_classify_node) = {
450 .name = "nat44-classify",
451 .vector_size = sizeof (u32),
452 .format_trace = format_nat44_classify_trace,
453 .type = VLIB_NODE_TYPE_INTERNAL,
454 .n_errors = ARRAY_LEN(nat44_classify_error_strings),
455 .error_strings = nat44_classify_error_strings,
456 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
458 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
459 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
460 [NAT44_CLASSIFY_NEXT_DROP] = "error-drop",
465 VLIB_NODE_FN (nat44_ed_classify_node) (vlib_main_t * vm,
466 vlib_node_runtime_t * node,
467 vlib_frame_t * frame)
469 return nat44_ed_classify_node_fn_inline (vm, node, frame);
473 VLIB_REGISTER_NODE (nat44_ed_classify_node) = {
474 .name = "nat44-ed-classify",
475 .vector_size = sizeof (u32),
476 .sibling_of = "nat-default",
477 .format_trace = format_nat44_classify_trace,
478 .type = VLIB_NODE_TYPE_INTERNAL,
482 VLIB_NODE_FN (nat44_handoff_classify_node) (vlib_main_t * vm,
483 vlib_node_runtime_t * node,
484 vlib_frame_t * frame)
486 return nat44_handoff_classify_node_fn_inline (vm, node, frame);
490 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
491 .name = "nat44-handoff-classify",
492 .vector_size = sizeof (u32),
493 .sibling_of = "nat-default",
494 .format_trace = format_nat44_classify_trace,
495 .type = VLIB_NODE_TYPE_INTERNAL,
501 * fd.io coding-style-patch-verification: ON
504 * eval: (c-set-style "gnu")