FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
dslite_out2in.c
Go to the documentation of this file.
1 /*
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:
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 #include <nat/dslite.h>
16 #include <nat/nat_inlines.h>
17 
19 
20 typedef enum
21 {
27 
28 static char *dslite_out2in_error_strings[] = {
29 #define _(sym,string) string,
31 #undef _
32 };
33 
34 static inline u32
36  dslite_session_t ** sp, u32 next, u8 * error,
37  u32 thread_index)
38 {
39  dslite_session_t *s = 0;
40  icmp46_header_t *icmp = ip4_next_header (ip4);
41  clib_bihash_kv_8_8_t kv, value;
43  u32 n = next;
44  icmp_echo_header_t *echo;
45  u32 new_addr, old_addr;
46  u16 old_id, new_id;
47  ip_csum_t sum;
48 
49  echo = (icmp_echo_header_t *) (icmp + 1);
50 
51  if (icmp_is_error_message (icmp) || (icmp->type != ICMP4_echo_reply))
52  {
54  *error = DSLITE_ERROR_BAD_ICMP_TYPE;
55  goto done;
56  }
57 
58  key.addr = ip4->dst_address;
59  key.port = echo->identifier;
60  key.protocol = SNAT_PROTOCOL_ICMP;
61  key.fib_index = 0;
62  kv.key = key.as_u64;
63 
64  if (clib_bihash_search_8_8
65  (&dm->per_thread_data[thread_index].out2in, &kv, &value))
66  {
68  *error = DSLITE_ERROR_NO_TRANSLATION;
69  goto done;
70  }
71  else
72  {
73  s =
74  pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
75  value.value);
76  }
77 
78  old_id = echo->identifier;
79  echo->identifier = new_id = s->in2out.port;
80  sum = icmp->checksum;
81  sum = ip_csum_update (sum, old_id, new_id, icmp_echo_header_t, identifier);
82  icmp->checksum = ip_csum_fold (sum);
83 
84  old_addr = ip4->dst_address.as_u32;
85  ip4->dst_address = s->in2out.addr;
86  new_addr = ip4->dst_address.as_u32;
87 
88  sum = ip4->checksum;
89  sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
90  ip4->checksum = ip_csum_fold (sum);
91 
92 done:
93  *sp = s;
94  return n;
95 }
96 
97 static uword
99  vlib_frame_t * frame)
100 {
101  u32 n_left_from, *from, *to_next;
102  dslite_out2in_next_t next_index;
103  vlib_node_runtime_t *error_node;
104  u32 thread_index = vm->thread_index;
105  f64 now = vlib_time_now (vm);
106  dslite_main_t *dm = &dslite_main;
107 
108  error_node = vlib_node_get_runtime (vm, dslite_out2in_node.index);
109 
110  from = vlib_frame_vector_args (frame);
111  n_left_from = frame->n_vectors;
112  next_index = node->cached_next_index;
113 
114 
115  while (n_left_from > 0)
116  {
117  u32 n_left_to_next;
118 
119  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
120 
121  while (n_left_from > 0 && n_left_to_next > 0)
122  {
123  u32 bi0;
124  vlib_buffer_t *b0;
126  u8 error0 = DSLITE_ERROR_OUT2IN;
127  ip4_header_t *ip40;
128  ip6_header_t *ip60;
129  u32 proto0;
130  udp_header_t *udp0;
131  tcp_header_t *tcp0;
132  clib_bihash_kv_8_8_t kv0, value0;
133  snat_session_key_t key0;
134  dslite_session_t *s0 = 0;
135  ip_csum_t sum0;
136  u32 new_addr0, old_addr0;
137  u16 new_port0, old_port0;
138 
139  /* speculatively enqueue b0 to the current next frame */
140  bi0 = from[0];
141  to_next[0] = bi0;
142  from += 1;
143  to_next += 1;
144  n_left_from -= 1;
145  n_left_to_next -= 1;
146 
147  b0 = vlib_get_buffer (vm, bi0);
148  ip40 = vlib_buffer_get_current (b0);
149  proto0 = ip_proto_to_snat_proto (ip40->protocol);
150 
151  if (PREDICT_FALSE (proto0 == ~0))
152  {
153  error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL;
154  next0 = DSLITE_OUT2IN_NEXT_DROP;
155  goto trace0;
156  }
157 
158  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
159  {
160  next0 =
161  dslite_icmp_out2in (dm, ip40, &s0, next0, &error0,
162  thread_index);
163  if (PREDICT_FALSE (next0 == DSLITE_OUT2IN_NEXT_DROP))
164  goto trace0;
165 
166  goto encap0;
167  }
168 
169  udp0 = ip4_next_header (ip40);
170  tcp0 = (tcp_header_t *) udp0;
171 
172  key0.addr = ip40->dst_address;
173  key0.port = udp0->dst_port;
174  key0.protocol = proto0;
175  key0.fib_index = 0;
176  kv0.key = key0.as_u64;
177 
178  if (clib_bihash_search_8_8
179  (&dm->per_thread_data[thread_index].out2in, &kv0, &value0))
180  {
181  next0 = DSLITE_OUT2IN_NEXT_DROP;
182  error0 = DSLITE_ERROR_NO_TRANSLATION;
183  goto trace0;
184  }
185  else
186  {
187  s0 =
188  pool_elt_at_index (dm->per_thread_data[thread_index].sessions,
189  value0.value);
190  }
191 
192  old_addr0 = ip40->dst_address.as_u32;
193  ip40->dst_address = s0->in2out.addr;
194  new_addr0 = ip40->dst_address.as_u32;
195 
196  sum0 = ip40->checksum;
197  sum0 =
198  ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
199  dst_address);
200  ip40->checksum = ip_csum_fold (sum0);
201 
202  if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
203  {
204  old_port0 = tcp0->dst_port;
205  tcp0->dst_port = s0->in2out.port;
206  new_port0 = tcp0->dst_port;
207 
208  sum0 = tcp0->checksum;
209  sum0 =
210  ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
211  dst_address);
212  sum0 =
213  ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
214  length);
215  tcp0->checksum = ip_csum_fold (sum0);
216  }
217  else
218  {
219  old_port0 = udp0->dst_port;
220  udp0->dst_port = s0->in2out.port;
221  udp0->checksum = 0;
222  }
223 
224  encap0:
225  /* Construct IPv6 header */
226  vlib_buffer_advance (b0, -(sizeof (ip6_header_t)));
227  ip60 = vlib_buffer_get_current (b0);
229  clib_host_to_net_u32 ((6 << 28) + (ip40->tos << 20));
230  ip60->payload_length = ip40->length;
231  ip60->protocol = IP_PROTOCOL_IP_IN_IP;
232  ip60->hop_limit = ip40->ttl;
233  ip60->src_address.as_u64[0] = dm->aftr_ip6_addr.as_u64[0];
234  ip60->src_address.as_u64[1] = dm->aftr_ip6_addr.as_u64[1];
235  ip60->dst_address.as_u64[0] = s0->in2out.softwire_id.as_u64[0];
236  ip60->dst_address.as_u64[1] = s0->in2out.softwire_id.as_u64[1];
237 
238  /* Accounting */
239  s0->last_heard = now;
240  s0->total_pkts++;
241  s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
242  /* Per-B4 LRU list maintenance */
243  clib_dlist_remove (dm->per_thread_data[thread_index].list_pool,
244  s0->per_b4_index);
245  clib_dlist_addtail (dm->per_thread_data[thread_index].list_pool,
246  s0->per_b4_list_head_index, s0->per_b4_index);
247  trace0:
249  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
250  {
251  dslite_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
252  t->next_index = next0;
253  t->session_index = ~0;
254  if (s0)
255  t->session_index =
256  s0 - dm->per_thread_data[thread_index].sessions;
257  }
258 
259  b0->error = error_node->errors[error0];
260 
261  /* verify speculative enqueue, maybe switch current next frame */
262  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
263  n_left_to_next, bi0, next0);
264  }
265  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
266  }
267 
268  return frame->n_vectors;
269 }
270 
271 /* *INDENT-OFF* */
273  .function = dslite_out2in_node_fn,
274  .name = "dslite-out2in",
275  .vector_size = sizeof (u32),
276  .format_trace = format_dslite_trace,
277  .type = VLIB_NODE_TYPE_INTERNAL,
279  .error_strings = dslite_out2in_error_strings,
280  .n_next_nodes = DSLITE_OUT2IN_N_NEXT,
281  /* edit / add dispositions here */
282  .next_nodes = {
283  [DSLITE_OUT2IN_NEXT_DROP] = "error-drop",
284  [DSLITE_OUT2IN_NEXT_IP4_LOOKUP] = "ip4-lookup",
285  [DSLITE_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
286  },
287 };
288 /* *INDENT-ON* */
289 
291 
292 /*
293  * fd.io coding-style-patch-verification: ON
294  *
295  * Local Variables:
296  * eval: (c-set-style "gnu")
297  * End:
298  */
dslite_out2in_next_t
Definition: dslite_out2in.c:20
u8 * format_dslite_trace(u8 *s, va_list *args)
Definition: dslite.c:221
#define PREDICT_TRUE(x)
Definition: clib.h:108
u64 as_u64[2]
Definition: ip6_packet.h:51
static_always_inline u8 icmp_is_error_message(icmp46_header_t *icmp)
Definition: nat_inlines.h:53
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:227
u32 thread_index
Definition: main.h:179
uword ip_csum_t
Definition: ip_packet.h:181
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:472
u32 session_index
Definition: dslite.h:98
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:263
struct _tcp_header tcp_header_t
ip6_address_t src_address
Definition: ip6_packet.h:378
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
ip4_address_t dst_address
Definition: ip4_packet.h:169
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:240
unsigned int u32
Definition: types.h:88
static uword dslite_out2in_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: dslite_out2in.c:98
dlist_elt_t * list_pool
Definition: dslite.h:75
clib_bihash_8_8_t out2in
Definition: dslite.h:62
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
u64 key
the key
Definition: bihash_8_8.h:33
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
Definition: dlist.h:43
unsigned short u16
Definition: types.h:57
u16 protocol
Definition: nat.h:54
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:205
#define PREDICT_FALSE(x)
Definition: clib.h:107
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#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:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:138
u64 value
the value
Definition: bihash_8_8.h:34
VLIB_NODE_FUNCTION_MULTIARCH(dslite_out2in_node, dslite_out2in_node_fn)
ip6_address_t aftr_ip6_addr
Definition: dslite.h:80
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:155
u16 n_vectors
Definition: node.h:401
vlib_main_t * vm
Definition: buffer.c:294
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
dslite_main_t dslite_main
Definition: dslite.c:19
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:31
#define ARRAY_LEN(x)
Definition: clib.h:61
ip4_address_t addr
Definition: nat.h:52
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:455
static char * dslite_out2in_error_strings[]
Definition: dslite_out2in.c:28
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:218
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
Definition: dlist.h:99
dslite_per_thread_data_t * per_thread_data
Definition: dslite.h:84
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
struct _vlib_node_registration vlib_node_registration_t
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:365
u16 payload_length
Definition: ip6_packet.h:369
static u32 ip_proto_to_snat_proto(u8 ip_proto)
The NAT inline functions.
Definition: nat_inlines.h:26
u32 next_index
Definition: dslite.h:97
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:267
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:231
vlib_node_registration_t dslite_out2in_node
(constructor) VLIB_REGISTER_NODE (dslite_out2in_node)
Definition: dslite_out2in.c:18
static u32 dslite_icmp_out2in(dslite_main_t *dm, ip4_header_t *ip4, dslite_session_t **sp, u32 next, u8 *error, u32 thread_index)
Definition: dslite_out2in.c:35
dslite_session_t * sessions
Definition: dslite.h:72
#define foreach_dslite_error
Definition: dslite.h:106
u16 flags
Copy of main node flags.
Definition: node.h:507
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:310
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:116
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:58
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:237
u16 fib_index
Definition: nat.h:54
ip6_address_t dst_address
Definition: ip6_packet.h:378