FD.io VPP  v16.09
Vector Packet Processing
cnat_ipv4_tcp_inside_input.c
Go to the documentation of this file.
1 /*
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_tcp_inside_input.c - cnat_ipv4_tcp_inside_input node pipeline
4  * stage functions
5  *
6  *
7  * Copyright (c) 2008-2014 Cisco and/or its affiliates.
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *---------------------------------------------------------------------------
20  */
21 
22 #include <vlib/vlib.h>
23 #include <vnet/vnet.h>
24 #include <vppinfra/error.h>
25 #include <vnet/buffer.h>
26 
27 #include "cnat_db.h"
28 #include "tcp_header_definitions.h"
29 #include "cnat_config.h"
30 #include "cnat_global.h"
31 #include "cnat_v4_functions.h"
32 
33 #define foreach_cnat_ipv4_tcp_inside_input_error \
34 _(CNAT_V4_TCP_I2O_PKT_IN, "tcp i2o packets received") \
35 _(CNAT_V4_TCP_I2O_PKT_T, "tcp i2o packets natted") \
36 _(CNAT_V4_TCP_I2O_EXCEPTION, "packets to tcp i2o exception") \
37 _(CNAT_V4_TCP_I2O_TTL_GEN, "generated TTL expiry ICMP packets") \
38 _(CNAT_V4_TCP_I2O_TTL_GEN_DROP, "could not generate TTL expiry ICMP packets") \
39 _(CNAT_V4_TCP_I2O_SESSION_DROP, "could not generate session") \
40 _(CNAT_V4_UDP_I2O_FRAG_DROP, "non-first fragment drop")
41 
42 typedef enum {
43 #define _(sym,str) sym,
45 #undef _
48 
50 #define _(sym,string) string,
52 #undef _
53 };
54 
55 typedef struct {
57  /* $$$$ add data here */
58 
59  /* convenience variables */
63 
64 typedef enum {
70 
71 #define CNAT_REWRITE_OUTPUT CNAT_V4_TCP_I2O_T
72 #define CNAT_V4_ICMP_GEN CNAT_V4_TCP_I2O_D
73 
74 //#define CNAT_V4_TCP_I2O_E CNAT_V4_TCP_I2O_D //remove it once exception node is created
77 
78 #define NSTAGES 6
79 
80 /*
81  * Use the generic buffer metadata + first line of packet data prefetch
82  * stage function from <api/pipeline.h>. This is usually a Good Idea.
83  */
84 #define stage0 generic_stage0
85 
86 
87 static inline void
88 stage1(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
89 {
90  u64 a, b, c;
91  u32 bucket;
92  u8 *prefetch_target;
93  //cnat_feature_data_t *fd = (cnat_feature_data_t *)ctx->feature_data;
94 
95 
96  vlib_buffer_t * b0 = vlib_get_buffer (vm, buffer_index);
98  u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
99  tcp_hdr_type *tcp = (tcp_hdr_type *)((u8*)ip + ipv4_hdr_len);
100 
101  u64 tmp = 0;
102  tmp = vnet_buffer(b0)->vcgn_uii.key.k.ipv4 =
103  clib_net_to_host_u32(ip->src_addr);
104  vnet_buffer(b0)->vcgn_uii.key.k.port =
105  clib_net_to_host_u16 (tcp->src_port);
106 
107  tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.port) << 32;
108 
109  PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
110  vnet_buffer(b0)->vcgn_uii.key.k.vrf,
111  CNAT_TCP)
112  tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.vrf) << 48;
113 
115 
116  prefetch_target = (u8 *)(&cnat_in2out_hash[bucket]);
117  vnet_buffer(b0)->vcgn_uii.bucket = bucket;
118 
119  /* Prefetch the hash bucket */
120  CLIB_PREFETCH(prefetch_target, CLIB_CACHE_LINE_BYTES, LOAD);
121 }
122 
123 static inline void
124 stage2(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
125 { /* nothing */ }
126 
127 
128 #define SPP_LOG2_CACHE_LINE_BYTES 6
129 #define SPP_CACHE_LINE_BYTES (1 << SPP_LOG2_CACHE_LINE_BYTES)
130 
131 static inline void
132 stage3(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
133 {
134  vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
135  uword prefetch_target0, prefetch_target1;
136  u32 bucket = vnet_buffer(b0)->vcgn_uii.bucket;
137 
138  /* read the hash bucket */
139  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket
140  = cnat_in2out_hash[bucket].next;
141 
142  if (PREDICT_TRUE(db_index != EMPTY)) {
143  /*
144  * Prefetch database keys. We save space by not cache-line
145  * aligning the DB entries. We don't want to waste LSU
146  * bandwidth prefetching stuff we won't need.
147  */
148  prefetch_target0 = (uword)(cnat_main_db + db_index);
149  CLIB_PREFETCH((void*)prefetch_target0, CLIB_CACHE_LINE_BYTES, LOAD);
150  /* Just beyond DB key #2 */
151  prefetch_target1 = prefetch_target0 +
153  /* If the targets are in different lines, do the second prefetch */
154  if (PREDICT_FALSE((prefetch_target0 & ~(SPP_CACHE_LINE_BYTES-1)) !=
155  (prefetch_target1 & ~(SPP_CACHE_LINE_BYTES-1)))) {
156  CLIB_PREFETCH((void *)prefetch_target1, CLIB_CACHE_LINE_BYTES, LOAD);
157  }
158  }
159 }
160 
161 static inline void
162 stage4(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
163 {
165  vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
166  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
167 
168  /*
169  * Note: if the search already failed (empty bucket),
170  * the answer is already in the pipeline context structure
171  */
172  if (PREDICT_TRUE(db_index != EMPTY)) {
173 
174  /*
175  * Note: hash collisions suck. We can't easily prefetch around them.
176  * The first trip around the track will be fast. After that, maybe
177  * not so much...
178  */
179  do {
180  db = cnat_main_db + db_index;
181  if (PREDICT_TRUE(db->in2out_key.key64 ==
182  vnet_buffer(b0)->vcgn_uii.key.key64)) {
183  break;
184  }
185  db_index = db->in2out_hash.next;
186  } while (db_index != EMPTY);
187 
188  /* Stick the answer back into the pipeline context structure */
189  vnet_buffer(b0)->vcgn_uii.bucket = db_index;
190  }
191 }
192 
193 
194 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
195  u32 bi)
196 {
197  vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
198  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
199  spp_ctx_t *ctx = (spp_ctx_t *) &vnet_buffer(b0)->vcgn_uii;
200  int disposition = CNAT_V4_TCP_I2O_T;
201  int counter = CNAT_V4_TCP_I2O_PKT_T;
202 
204  u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
205  tcp_hdr_type *tcp = (tcp_hdr_type *)((u8*)ip + ipv4_hdr_len);
207  u32 node_counter_base_index = n->error_heap_index;
208  vlib_error_main_t * em = &vm->error_main;
209  cnat_session_entry_t *session_db = NULL;
211  cnat_key_t dest_info;
212  u32 window;
213  u8 scale;
214 
215 
216  INCREMENT_NODE_COUNTER(CNAT_V4_TCP_I2O_PKT_IN);
217 
219  if (PREDICT_FALSE(ip->ttl <= 1)) {
220  /* Try to generate ICMP error msg, as TTL is <= 1 */
221 
223  (ctx, ip, ctx->ru.rx.uidb_index)) {
224 
225  /* Generated ICMP */
226  disposition = CNAT_REWRITE_OUTPUT;
227  counter = CNAT_V4_TCP_I2O_TTL_GEN;
228  } else {
229  /* Could not generated ICMP - drop the packet */
230  disposition = CNAT_V4_TCP_I2O_D;
231  counter = CNAT_V4_TCP_I2O_TTL_GEN_DROP;
232  }
233  goto drop_pkt;
234  }
235  }
236 
237  if (PREDICT_FALSE(db_index == EMPTY)) {
238  /* Deleted fragment code from here */
239  disposition = CNAT_V4_TCP_I2O_E;
240  counter = CNAT_V4_TCP_I2O_EXCEPTION;
241  } else {
242  db = cnat_main_db + db_index;
243 
244  /* Handle destination sessions */
245  dest_info.k.port = clib_net_to_host_u16(tcp->dest_port);
246  dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
247 
249 
250  /* No DBL support, so just update the destn and proceed */
251  db->dst_ipv4 = dest_info.k.ipv4;
252  db->dst_port = dest_info.k.port;
253  goto update_pkt;
254  }
255 
256  if(PREDICT_FALSE(db->dst_ipv4 != dest_info.k.ipv4 ||
257  db->dst_port != dest_info.k.port)) {
258  if(PREDICT_TRUE(db->nsessions == 0)) {
259  /* Should be a static entry
260  * Note this session as the first session and log
261  */
262  cnat_add_dest_n_log(db, &dest_info);
263  } else if(PREDICT_FALSE(db->nsessions == 1)) {
264  /* Destn is not same as in main db. Multiple session
265  * scenario
266  */
267  dest_info.k.vrf = db->in2out_key.k.vrf;
268  session_db = cnat_handle_1to2_session(db, &dest_info);
269  if(PREDICT_FALSE(session_db == NULL)) {
270  disposition = CNAT_V4_TCP_I2O_D;
271  counter = CNAT_V4_TCP_I2O_SESSION_DROP;
272  goto drop_pkt;
273  }
274  } else { /* There are already multiple destinations */
275  dest_info.k.vrf = db->in2out_key.k.vrf;
276  /* If session already exists,
277  * cnat_create_session_db_entry will return the existing db
278  * else create a new db
279  * If could not create, return NULL
280  */
281  session_db = cnat_create_session_db_entry(&dest_info,
282  db, TRUE);
283  if(PREDICT_FALSE(session_db == NULL)) {
284  disposition = CNAT_V4_TCP_I2O_D;
285  counter = CNAT_V4_TCP_I2O_SESSION_DROP;
286  goto drop_pkt;
287  }
288  }
289  if(PREDICT_TRUE(session_db != 0)) {
290  /* Have to repeat the window size check for new destinations */
291  window = (u32)clib_net_to_host_u16(tcp->window_size);
292  window = window << session_db->scale;
293  if(PREDICT_TRUE(!session_db->window)) {
294  calculate_window_scale(tcp, &scale);
295  session_db->scale = scale;
296  session_db->window = window;
297  } else if (PREDICT_FALSE(session_db->window <
298  window)) {
299  /* Update the db entry with window option from packet */
300  session_db->window = window;
301  } else {
302  /* Do nothing */
303  }
304  session_db->tcp_seq_num = clib_net_to_host_u32(tcp->seq_num);
305  session_db->ack_no = clib_net_to_host_u32(tcp->ack_num);
306 #if DEBUG > 1
307  printf("\n In2out SDB stages seq no = %u,"
308  " ack no = %u, window = %u\n",
309  session_db->tcp_seq_num,
310  session_db->ack_no,
311  session_db->window);
312 #endif
313 
314  }
315  } else {
316  //Update the seq no and ack no for subsequent communication
317  //after connection establishment
318  //No need to update window here. Window is already updated
319  //during connection establishment
320  window = (u32)clib_net_to_host_u16(tcp->window_size);
321  window = window << db->scale;
322  if(PREDICT_FALSE(!ALG_ENABLED_DB(db))) {
323  //This check is done since proto_data is part of union in main
324  //db entry
325  db->proto_data.tcp_seq_chk.seq_no =
326  clib_net_to_host_u32(tcp->seq_num);
327  db->proto_data.tcp_seq_chk.ack_no =
328  clib_net_to_host_u32(tcp->ack_num);
329  }
330  if (PREDICT_FALSE(db->diff_window < window)) {
331  /* Update the db entry with window option from packet */
332  db->diff_window = window;
333  }
334 #if DEBUG > 1
335  printf("\n In2out MainDB seq no = %u,"
336  "\n ack no = %u\n",
337  db->proto_data.tcp_seq_chk.seq_no,
338  db->proto_data.tcp_seq_chk.ack_no);
339  printf("\n In2out MAINDB window = %u\n",
340  db->diff_window);
341 #endif
342  }
343 update_pkt:
344 
345  counter = CNAT_V4_TCP_I2O_PKT_T;
346  disposition = CNAT_V4_TCP_I2O_T;
347 
348  /* NO FRAGMENT & ALG HANDLING. DELETING THE CODE */
349 
351  /*
352  * Decrement TTL and update IPv4 checksum
353  */
355  }
356 
358  tcp,
359  db->out2in_key.k.ipv4,
360  db->out2in_key.k.port,
361  db
362  /*, db->in2out_key.k.vrf */);
363 
364  /* update transaltion counters */
365  db->in2out_pkts++;
367 
368  /* update the timer for good mode, or evil mode dst_ip match */
369 
370  if(PREDICT_FALSE(session_db != NULL)) {
371  V4_TCP_UPDATE_SESSION_DB_FLAG(session_db, tcp);
372  CNAT_DB_TIMEOUT_RST(session_db);
373  } else {
376  }
377  }
378 
379 drop_pkt:
380 
381  em->counters[node_counter_base_index + counter] += 1;
382  return disposition;
383 }
384 
385 #include <vnet/pipeline.h>
386 
388  vlib_node_runtime_t * node,
389  vlib_frame_t * frame)
390 {
391  return dispatch_pipeline (vm, node, frame);
392 }
393 
394 
397  .name = "vcgn-v4-tcp-i2o",
398  .vector_size = sizeof (u32),
400 
403 
404  .n_next_nodes = CNAT_V4_TCP_I2O_NEXT,
405 
406  /* edit / add dispositions here */
407  .next_nodes = {
408  [CNAT_V4_TCP_I2O_E] = "vcgn-v4-tcp-i2o-e",
409  [CNAT_V4_TCP_I2O_T] = "ip4-input",
410  [CNAT_V4_TCP_I2O_D] = "error-drop",
411  },
412 };
413 
415 {
417 
418  mp->vlib_main = vm;
419  mp->vnet_main = vnet_get_main();
420 
421  return 0;
422 }
423 
#define ALG_ENABLED_DB(db)
Definition: cnat_db.h:694
u32 error_heap_index
Definition: node.h:278
u32 diff_window
Definition: cnat_db.h:275
cnat_main_db_entry_t * cnat_main_db
Definition: cnat_db_v2.c:201
static uword cnat_ipv4_tcp_inside_input_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
cnat_session_entry_t * cnat_create_session_db_entry(cnat_key_t *ko, cnat_main_db_entry_t *bdb, u8 log)
Definition: cnat_db_v2.c:2643
a
Definition: bitmap.h:516
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define PREDICT_TRUE(x)
Definition: clib.h:98
void ipv4_decr_ttl_n_calc_csum(ipv4_header *ipv4)
#define NULL
Definition: clib.h:55
u32 tcp_seq_num
Definition: cnat_db.h:369
#define foreach_cnat_ipv4_tcp_inside_input_error
u64 key64
Definition: cnat_db.h:109
#define V4_TCP_UPDATE_SESSION_FLAG(db, tcp)
void tcp_in2out_nat_mss_n_checksum(ipv4_header *ip, tcp_hdr_type *tcp, u32 ipv4_addr, u16 port, cnat_main_db_entry_t *db)
u32 ack_no
Definition: cnat_db.h:372
Definition: cnat_db.h:153
struct _vlib_node_registration vlib_node_registration_t
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:62
index_slist_t in2out_hash
Definition: cnat_db.h:156
cnat_session_entry_t * cnat_handle_1to2_session(cnat_main_db_entry_t *mdb, cnat_key_t *dest_info)
Definition: cnat_db_v2.c:1027
static char * cnat_ipv4_tcp_inside_input_error_strings[]
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
index_slist_t * cnat_in2out_hash
Definition: cnat_db_v2.c:196
#define CNAT_DB_TIMEOUT_RST(db)
Definition: cnat_db.h:561
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
union cnat_main_db_entry_t::@214 proto_data
static void stage2(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
#define CNAT_REWRITE_OUTPUT
u8 scale
Definition: cnat_db.h:378
unsigned long u64
Definition: types.h:89
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
vlib_node_registration_t cnat_ipv4_tcp_inside_input_node
(constructor) VLIB_REGISTER_NODE (cnat_ipv4_tcp_inside_input_node)
cnat_ipv4_tcp_inside_input_next_t
u16 dst_port
Definition: cnat_db.h:257
#define PLATFORM_CNAT_SET_RX_VRF(ctx, rx_vrf, proto)
vlib_error_main_t error_main
Definition: main.h:124
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
static void stage4(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
#define PREDICT_FALSE(x)
Definition: clib.h:97
int icmpv4_generate_with_throttling(spp_ctx_t *ctx, ipv4_header *ipv4, u16 rx_uidb_index)
struct cnat_main_db_entry_t::@214::tcp_seq_chk_t tcp_seq_chk
clib_error_t * cnat_ipv4_tcp_inside_input_init(vlib_main_t *vm)
void calculate_window_scale(tcp_hdr_type *tcp_header, u8 *scale)
Definition: cnat_util.c:2135
u64 * counters
Definition: error.h:78
svmdb_client_t * c
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
cnat_db_key_t k
Definition: cnat_db.h:108
cnat_ipv4_tcp_inside_input_main_t cnat_ipv4_tcp_inside_input_main
void cnat_add_dest_n_log(cnat_main_db_entry_t *mdb, cnat_key_t *dest_info)
Definition: cnat_db_v2.c:1083
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:335
#define TRUE
Definition: cnat_db.h:78
#define PLATFORM_DBL_SUPPORT
#define INCREMENT_NODE_COUNTER(c)
static void stage3(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
cnat_key_t out2in_key
Definition: cnat_db.h:198
#define CNAT_TCP
Definition: cnat_db.h:94
u64 uword
Definition: types.h:112
u16 nsessions
Definition: cnat_db.h:266
u32 window
Definition: cnat_db.h:375
u8 scale
Definition: cnat_db.h:272
Definition: cnat_db.h:336
#define CNAT_MAIN_HASH_MASK
Definition: cnat_db.h:56
unsigned char u8
Definition: types.h:56
u32 dst_ipv4
Definition: cnat_db.h:254
struct _spp_ctx spp_ctx_t
cnat_ipv4_tcp_inside_input_t
u32 in2out_forwarding_count
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
#define EMPTY
Definition: index_list.h:24
#define PLATFORM_HANDLE_TTL_DECREMENT
#define CNAT_V4_GET_HASH(key64, hash, mask)
Definition: cnat_db.h:536
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
#define SPP_CACHE_LINE_BYTES
#define V4_TCP_UPDATE_SESSION_DB_FLAG(sdb, tcp)
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
Definition: defs.h:46
cnat_key_t in2out_key
Definition: cnat_db.h:201
u32 in2out_pkts
Definition: cnat_db.h:210