2 * Copyright (c) 2017 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.
15 #include <nat/dslite.h>
16 #include <nat/nat_inlines.h>
17 #include <nat/nat_syslog.h>
21 DSLITE_IN2OUT_NEXT_IP4_LOOKUP,
22 DSLITE_IN2OUT_NEXT_IP6_ICMP,
23 DSLITE_IN2OUT_NEXT_DROP,
24 DSLITE_IN2OUT_NEXT_SLOWPATH,
26 } dslite_in2out_next_t;
28 static char *dslite_in2out_error_strings[] = {
29 #define _(sym,string) string,
35 slow_path (dslite_main_t * dm, dslite_session_key_t * in2out_key,
36 dslite_session_t ** sp, u32 next, u8 * error, u32 thread_index)
39 clib_bihash_kv_16_8_t b4_kv, b4_value;
40 clib_bihash_kv_24_8_t in2out_kv;
41 clib_bihash_kv_8_8_t out2in_kv;
42 dlist_elt_t *head_elt, *oldest_elt, *elt;
45 snat_session_key_t out2in_key;
48 out2in_key.protocol = in2out_key->proto;
49 out2in_key.fib_index = 0;
51 b4_kv.key[0] = in2out_key->softwire_id.as_u64[0];
52 b4_kv.key[1] = in2out_key->softwire_id.as_u64[1];
54 if (clib_bihash_search_16_8
55 (&dm->per_thread_data[thread_index].b4_hash, &b4_kv, &b4_value))
57 pool_get (dm->per_thread_data[thread_index].b4s, b4);
58 clib_memset (b4, 0, sizeof (*b4));
59 b4->addr.as_u64[0] = in2out_key->softwire_id.as_u64[0];
60 b4->addr.as_u64[1] = in2out_key->softwire_id.as_u64[1];
62 pool_get (dm->per_thread_data[thread_index].list_pool, head_elt);
63 b4->sessions_per_b4_list_head_index =
64 head_elt - dm->per_thread_data[thread_index].list_pool;
65 clib_dlist_init (dm->per_thread_data[thread_index].list_pool,
66 b4->sessions_per_b4_list_head_index);
68 b4_index = b4_kv.value = b4 - dm->per_thread_data[thread_index].b4s;
69 clib_bihash_add_del_16_8 (&dm->per_thread_data[thread_index].b4_hash,
72 vlib_set_simple_counter (&dm->total_b4s, thread_index, 0,
74 per_thread_data[thread_index].b4s));
78 b4_index = b4_value.value;
80 pool_elt_at_index (dm->per_thread_data[thread_index].b4s,
84 //TODO configurable quota
85 if (b4->nsessions >= 1000)
88 clib_dlist_remove_head (dm->per_thread_data[thread_index].list_pool,
89 b4->sessions_per_b4_list_head_index);
90 ASSERT (oldest_index != ~0);
91 clib_dlist_addtail (dm->per_thread_data[thread_index].list_pool,
92 b4->sessions_per_b4_list_head_index, oldest_index);
94 pool_elt_at_index (dm->per_thread_data[thread_index].list_pool,
97 pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
100 in2out_kv.key[0] = s->in2out.as_u64[0];
101 in2out_kv.key[1] = s->in2out.as_u64[1];
102 in2out_kv.key[2] = s->in2out.as_u64[2];
103 clib_bihash_add_del_24_8 (&dm->per_thread_data[thread_index].in2out,
105 out2in_kv.key = s->out2in.as_u64;
106 clib_bihash_add_del_8_8 (&dm->per_thread_data[thread_index].out2in,
108 snat_free_outside_address_and_port (dm->addr_pool, thread_index,
111 nat_syslog_dslite_apmdel (b4_index, &s->in2out.softwire_id,
112 &s->in2out.addr, s->in2out.port,
113 &s->out2in.addr, s->out2in.port,
116 if (snat_alloc_outside_address_and_port
117 (dm->addr_pool, 0, thread_index, &out2in_key,
118 dm->port_per_thread, thread_index))
123 if (snat_alloc_outside_address_and_port
124 (dm->addr_pool, 0, thread_index, &out2in_key,
125 dm->port_per_thread, thread_index))
127 *error = DSLITE_ERROR_OUT_OF_PORTS;
128 return DSLITE_IN2OUT_NEXT_DROP;
130 pool_get (dm->per_thread_data[thread_index].sessions, s);
131 clib_memset (s, 0, sizeof (*s));
134 pool_get (dm->per_thread_data[thread_index].list_pool, elt);
135 clib_dlist_init (dm->per_thread_data[thread_index].list_pool,
136 elt - dm->per_thread_data[thread_index].list_pool);
137 elt->value = s - dm->per_thread_data[thread_index].sessions;
138 s->per_b4_index = elt - dm->per_thread_data[thread_index].list_pool;
139 s->per_b4_list_head_index = b4->sessions_per_b4_list_head_index;
140 clib_dlist_addtail (dm->per_thread_data[thread_index].list_pool,
141 s->per_b4_list_head_index,
142 elt - dm->per_thread_data[thread_index].list_pool);
144 vlib_set_simple_counter (&dm->total_sessions, thread_index, 0,
145 pool_elts (dm->per_thread_data
146 [thread_index].sessions));
149 s->in2out = *in2out_key;
150 s->out2in = out2in_key;
152 in2out_kv.key[0] = s->in2out.as_u64[0];
153 in2out_kv.key[1] = s->in2out.as_u64[1];
154 in2out_kv.key[2] = s->in2out.as_u64[2];
155 in2out_kv.value = s - dm->per_thread_data[thread_index].sessions;
156 clib_bihash_add_del_24_8 (&dm->per_thread_data[thread_index].in2out,
158 out2in_kv.key = s->out2in.as_u64;
159 out2in_kv.value = s - dm->per_thread_data[thread_index].sessions;
160 clib_bihash_add_del_8_8 (&dm->per_thread_data[thread_index].out2in,
163 nat_syslog_dslite_apmadd (b4_index, &s->in2out.softwire_id, &s->in2out.addr,
164 s->in2out.port, &s->out2in.addr, s->out2in.port,
171 dslite_icmp_in2out (dslite_main_t * dm, ip6_header_t * ip6,
172 ip4_header_t * ip4, dslite_session_t ** sp, u32 next,
173 u8 * error, u32 thread_index)
175 dslite_session_t *s = 0;
176 icmp46_header_t *icmp = ip4_next_header (ip4);
177 clib_bihash_kv_24_8_t kv, value;
178 dslite_session_key_t key;
180 icmp_echo_header_t *echo;
181 u32 new_addr, old_addr;
185 if (icmp_is_error_message (icmp))
187 n = DSLITE_IN2OUT_NEXT_DROP;
188 *error = DSLITE_ERROR_BAD_ICMP_TYPE;
192 echo = (icmp_echo_header_t *) (icmp + 1);
194 key.addr = ip4->src_address;
195 key.port = echo->identifier;
196 key.proto = SNAT_PROTOCOL_ICMP;
197 key.softwire_id.as_u64[0] = ip6->src_address.as_u64[0];
198 key.softwire_id.as_u64[1] = ip6->src_address.as_u64[1];
200 kv.key[0] = key.as_u64[0];
201 kv.key[1] = key.as_u64[1];
202 kv.key[2] = key.as_u64[2];
204 if (clib_bihash_search_24_8
205 (&dm->per_thread_data[thread_index].in2out, &kv, &value))
207 n = slow_path (dm, &key, &s, next, error, thread_index);
208 if (PREDICT_FALSE (next == DSLITE_IN2OUT_NEXT_DROP))
214 pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
218 old_addr = ip4->src_address.as_u32;
219 ip4->src_address = s->out2in.addr;
220 new_addr = ip4->src_address.as_u32;
222 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
223 ip4->checksum = ip_csum_fold (sum);
225 old_id = echo->identifier;
226 echo->identifier = new_id = s->out2in.port;
227 sum = icmp->checksum;
228 sum = ip_csum_update (sum, old_id, new_id, icmp_echo_header_t, identifier);
229 icmp->checksum = ip_csum_fold (sum);
237 dslite_in2out_node_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
238 vlib_frame_t * frame, u8 is_slow_path)
240 u32 n_left_from, *from, *to_next;
241 dslite_in2out_next_t next_index;
243 vlib_node_runtime_t *error_node;
244 u32 thread_index = vm->thread_index;
245 f64 now = vlib_time_now (vm);
246 dslite_main_t *dm = &dslite_main;
249 is_slow_path ? dm->dslite_in2out_slowpath_node_index :
250 dm->dslite_in2out_node_index;
252 error_node = vlib_node_get_runtime (vm, node_index);
254 from = vlib_frame_vector_args (frame);
255 n_left_from = frame->n_vectors;
256 next_index = node->cached_next_index;
258 while (n_left_from > 0)
262 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
264 while (n_left_from > 0 && n_left_to_next > 0)
268 u32 next0 = DSLITE_IN2OUT_NEXT_IP4_LOOKUP;
271 u8 error0 = DSLITE_ERROR_IN2OUT;
273 dslite_session_t *s0 = 0;
274 clib_bihash_kv_24_8_t kv0, value0;
275 dslite_session_key_t key0;
279 u32 new_addr0, old_addr0;
280 u16 old_port0, new_port0;
282 /* speculatively enqueue b0 to the current next frame */
290 b0 = vlib_get_buffer (vm, bi0);
291 ip60 = vlib_buffer_get_current (b0);
293 if (PREDICT_FALSE (ip60->protocol != IP_PROTOCOL_IP_IN_IP))
295 if (ip60->protocol == IP_PROTOCOL_ICMP6)
297 next0 = DSLITE_IN2OUT_NEXT_IP6_ICMP;
300 error0 = DSLITE_ERROR_BAD_IP6_PROTOCOL;
301 next0 = DSLITE_IN2OUT_NEXT_DROP;
305 ip40 = vlib_buffer_get_current (b0) + sizeof (ip6_header_t);
306 proto0 = ip_proto_to_snat_proto (ip40->protocol);
308 if (PREDICT_FALSE (proto0 == ~0))
310 error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL;
311 next0 = DSLITE_IN2OUT_NEXT_DROP;
315 udp0 = ip4_next_header (ip40);
316 tcp0 = (tcp_header_t *) udp0;
320 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
323 dslite_icmp_in2out (dm, ip60, ip40, &s0, next0, &error0,
325 if (PREDICT_FALSE (next0 == DSLITE_IN2OUT_NEXT_DROP))
333 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
335 next0 = DSLITE_IN2OUT_NEXT_SLOWPATH;
340 key0.addr = ip40->src_address;
341 key0.port = udp0->src_port;
343 key0.softwire_id.as_u64[0] = ip60->src_address.as_u64[0];
344 key0.softwire_id.as_u64[1] = ip60->src_address.as_u64[1];
346 kv0.key[0] = key0.as_u64[0];
347 kv0.key[1] = key0.as_u64[1];
348 kv0.key[2] = key0.as_u64[2];
350 if (clib_bihash_search_24_8
351 (&dm->per_thread_data[thread_index].in2out, &kv0, &value0))
356 slow_path (dm, &key0, &s0, next0, &error0, thread_index);
357 if (PREDICT_FALSE (next0 == DSLITE_IN2OUT_NEXT_DROP))
362 next0 = DSLITE_IN2OUT_NEXT_SLOWPATH;
369 pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
373 old_addr0 = ip40->src_address.as_u32;
374 ip40->src_address = s0->out2in.addr;
375 new_addr0 = ip40->src_address.as_u32;
376 sum0 = ip40->checksum;
378 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
380 ip40->checksum = ip_csum_fold (sum0);
381 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
383 old_port0 = tcp0->src_port;
384 tcp0->src_port = s0->out2in.port;
385 new_port0 = tcp0->src_port;
387 sum0 = tcp0->checksum;
389 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
392 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
394 mss_clamping (&snat_main, tcp0, &sum0);
395 tcp0->checksum = ip_csum_fold (sum0);
399 old_port0 = udp0->src_port;
400 udp0->src_port = s0->out2in.port;
406 s0->last_heard = now;
408 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
409 /* Per-B4 LRU list maintenance */
410 clib_dlist_remove (dm->per_thread_data[thread_index].list_pool,
412 clib_dlist_addtail (dm->per_thread_data[thread_index].list_pool,
413 s0->per_b4_list_head_index, s0->per_b4_index);
416 (clib_net_to_host_u32
417 (ip60->ip_version_traffic_class_and_flow_label) & 0x0ff00000) >>
419 vlib_buffer_advance (b0, sizeof (ip6_header_t));
422 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
423 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
425 dslite_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
426 t->next_index = next0;
427 t->session_index = ~0;
430 s0 - dm->per_thread_data[thread_index].sessions;
433 b0->error = error_node->errors[error0];
435 /* verify speculative enqueue, maybe switch current next frame */
436 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
437 n_left_to_next, bi0, next0);
439 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
442 return frame->n_vectors;
445 VLIB_NODE_FN (dslite_in2out_node) (vlib_main_t * vm,
446 vlib_node_runtime_t * node,
447 vlib_frame_t * frame)
449 return dslite_in2out_node_fn_inline (vm, node, frame, 0);
453 VLIB_REGISTER_NODE (dslite_in2out_node) = {
454 .name = "dslite-in2out",
455 .vector_size = sizeof (u32),
456 .format_trace = format_dslite_trace,
457 .type = VLIB_NODE_TYPE_INTERNAL,
458 .n_errors = ARRAY_LEN (dslite_in2out_error_strings),
459 .error_strings = dslite_in2out_error_strings,
460 .n_next_nodes = DSLITE_IN2OUT_N_NEXT,
461 /* edit / add dispositions here */
463 [DSLITE_IN2OUT_NEXT_DROP] = "error-drop",
464 [DSLITE_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
465 [DSLITE_IN2OUT_NEXT_IP6_ICMP] = "ip6-icmp-input",
466 [DSLITE_IN2OUT_NEXT_SLOWPATH] = "dslite-in2out-slowpath",
471 VLIB_NODE_FN (dslite_in2out_slowpath_node) (vlib_main_t * vm,
472 vlib_node_runtime_t * node,
473 vlib_frame_t * frame)
475 return dslite_in2out_node_fn_inline (vm, node, frame, 1);
479 VLIB_REGISTER_NODE (dslite_in2out_slowpath_node) = {
480 .name = "dslite-in2out-slowpath",
481 .vector_size = sizeof (u32),
482 .format_trace = format_dslite_trace,
483 .type = VLIB_NODE_TYPE_INTERNAL,
484 .n_errors = ARRAY_LEN (dslite_in2out_error_strings),
485 .error_strings = dslite_in2out_error_strings,
486 .n_next_nodes = DSLITE_IN2OUT_N_NEXT,
487 /* edit / add dispositions here */
489 [DSLITE_IN2OUT_NEXT_DROP] = "error-drop",
490 [DSLITE_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
491 [DSLITE_IN2OUT_NEXT_IP6_ICMP] = "ip6-lookup",
492 [DSLITE_IN2OUT_NEXT_SLOWPATH] = "dslite-in2out-slowpath",
498 * fd.io coding-style-patch-verification: ON
501 * eval: (c-set-style "gnu")