FD.io VPP  v16.06
Vector Packet Processing
cnat_ipv4_icmp_query_inside_input.c
Go to the documentation of this file.
1 /*
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_icmp_query_inside_input.c - cnat_ipv4_icmp_query_inside_input node pipeline stage functions
4  *
5  *
6  * Copyright (c) 2008-2014 Cisco and/or its affiliates.
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at:
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *---------------------------------------------------------------------------
19  */
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vppinfra/error.h>
23 #include <vnet/buffer.h>
24 
25 #include "cnat_ipv4_icmp.h"
26 
27 #define foreach_cnat_ipv4_icmp_q_inside_input_error \
28 _(CNAT_V4_ICMP_Q_I2O_T_PKT, "cnat v4 icmp_q i2o packet transmit") \
29 _(CNAT_V4_ICMP_Q_I2O_MISS_PKT, "cnat v4 icmp_q i2o db miss") \
30 _(CNAT_V4_ICMP_Q_I2O_TTL_GEN, "cnat v4 icmp_q i2o ttl generate") \
31 _(CNAT_V4_ICMP_Q_I2O_TTL_DROP, "cnat v4 icmp_q i2o ttl drop") \
32 _(CNAT_V4_ICMP_Q_I2O_NO_SESSION_DROP, "cnat v4 icmp_q i2o no session drop")
33 
34 typedef enum {
35 #define _(sym,str) sym,
37 #undef _
40 
42 #define _(sym,string) string,
44 #undef _
45 };
46 
47 typedef struct {
49  /* $$$$ add data here */
50 
51  /* convenience variables */
55 
56 typedef enum {
62 
65 
66 #define NSTAGES 5
67 
69  icmp_v4_t *icmp,
70  cnat_main_db_entry_t *db, u16 vrf)
71 {
72 #if 0
73  u32 postmap_ip;
74  u8 direction;
75  u32 old_ip;
76  u32 old_postmap_ip;
77 
78 
79  if(is_static_dest_nat_enabled(vrf) == CNAT_SUCCESS) {
80  direction = 0;
81  if(cnat_static_dest_db_get_translation(ip->dest_addr, &postmap_ip, vrf, direction) == CNAT_SUCCESS) {
83 
84  old_ip = spp_net_to_host_byte_order_32(&(ip->dest_addr));
85  old_postmap_ip = spp_net_to_host_byte_order_32(&postmap_ip);
86 
87  CNAT_UPDATE_L3_CHECKSUM(((u16)(old_ip & 0xFFFF)),
88  ((u16)(old_ip >> 16)),
89  (spp_net_to_host_byte_order_16(&(ip->checksum))),
90  ((u16)(old_postmap_ip & 0xFFFF)),
91  ((u16)(old_postmap_ip >> 16)))
92  ip->dest_addr = postmap_ip;
93 
94  ip->checksum =
95  spp_host_to_net_byte_order_16(new_l3_c);
96  }
97  }
98 #endif /* if 0 */
99  /*
100  * declare variable
101  */
103  /*
104  * calculate checksum
105  */
107  ((u16)(db->in2out_key.k.ipv4 >> 16)),
108  (db->in2out_key.k.port),
109  (clib_net_to_host_u16(ip->checksum)),
110  (clib_net_to_host_u16(icmp->checksum)),
111  ((u16)(db->out2in_key.k.ipv4)),
112  ((u16)(db->out2in_key.k.ipv4 >> 16)),
113  (db->out2in_key.k.port))
114  //set ip header
115  ip->src_addr =
116  clib_host_to_net_u32(db->out2in_key.k.ipv4);
117  ip->checksum =
118  clib_host_to_net_u16(new_l3_c);
119 
120  //set icmp header
121  icmp->identifier =
122  clib_host_to_net_u16(db->out2in_key.k.port);
123  icmp->checksum =
124  clib_host_to_net_u16(new_l4_c);
125 }
126 
127 /*
128  * Use the generic buffer metadata + first line of packet data prefetch
129  * stage function from <api/pipeline.h>. This is usually a Good Idea.
130  */
131 #define stage0 generic_stage0
132 
133 static inline void
134 stage1(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
135 {
136  u64 a, b, c;
137  u32 bucket;
138  u8 *prefetch_target;
139 
140  vlib_buffer_t * b0 = vlib_get_buffer (vm, buffer_index);
142  u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
143  icmp_v4_t *icmp = (icmp_v4_t *)((u8*)ip + ipv4_hdr_len);
144 
145  u64 tmp = 0;
146  tmp = vnet_buffer(b0)->vcgn_uii.key.k.ipv4 =
147  clib_net_to_host_u32(ip->src_addr);
148  vnet_buffer(b0)->vcgn_uii.key.k.port =
149  clib_net_to_host_u16 (icmp->identifier);
150 
151  tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.port) << 32;
152 
153  PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
154  vnet_buffer(b0)->vcgn_uii.key.k.vrf,
155  CNAT_ICMP)
156  tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.vrf) << 48;
157 
159 
160  prefetch_target = (u8 *)(&cnat_in2out_hash[bucket]);
161  vnet_buffer(b0)->vcgn_uii.bucket = bucket;
162 
163  /* Prefetch the hash bucket */
164  CLIB_PREFETCH(prefetch_target, CLIB_CACHE_LINE_BYTES, LOAD);
165 }
166 
167 #define SPP_LOG2_CACHE_LINE_BYTES 6
168 #define SPP_CACHE_LINE_BYTES (1 << SPP_LOG2_CACHE_LINE_BYTES)
169 
170 static inline void
171 stage2(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
172 {
173  vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
174  uword prefetch_target0, prefetch_target1;
175  u32 bucket = vnet_buffer(b0)->vcgn_uii.bucket;
176 
177  /* read the hash bucket */
178  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket
179  = cnat_in2out_hash[bucket].next;
180 
181  if (PREDICT_TRUE(db_index != EMPTY)) {
182  /*
183  * Prefetch database keys. We save space by not cache-line
184  * aligning the DB entries. We don't want to waste LSU
185  * bandwidth prefetching stuff we won't need.
186  */
187  prefetch_target0 = (uword)(cnat_main_db + db_index);
188  CLIB_PREFETCH((void*)prefetch_target0, CLIB_CACHE_LINE_BYTES, LOAD);
189  /* Just beyond DB key #2 */
190  prefetch_target1 = prefetch_target0 +
192  /* If the targets are in different lines, do the second prefetch */
193  if (PREDICT_FALSE((prefetch_target0 & ~(SPP_CACHE_LINE_BYTES-1)) !=
194  (prefetch_target1 & ~(SPP_CACHE_LINE_BYTES-1)))) {
195  CLIB_PREFETCH((void *)prefetch_target1, CLIB_CACHE_LINE_BYTES, LOAD);
196  }
197  }
198 }
199 
200 static inline void
201 stage3(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
202 {
204  vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
205  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
206 
207  /*
208  * Note: if the search already failed (empty bucket),
209  * the answer is already in the pipeline context structure
210  */
211  if (PREDICT_TRUE(db_index != EMPTY)) {
212 
213  /*
214  * Note: hash collisions suck. We can't easily prefetch around them.
215  * The first trip around the track will be fast. After that, maybe
216  * not so much...
217  */
218  do {
219  db = cnat_main_db + db_index;
220  if (PREDICT_TRUE(db->in2out_key.key64 ==
221  vnet_buffer(b0)->vcgn_uii.key.key64)) {
222  break;
223  }
224  db_index = db->in2out_hash.next;
225  } while (db_index != EMPTY);
226 
227  /* Stick the answer back into the pipeline context structure */
228  vnet_buffer(b0)->vcgn_uii.bucket = db_index;
229  }
230 }
231 
232 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
233  u32 bi)
234 {
235  vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
236  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
237  spp_ctx_t *ctx = (spp_ctx_t *) &vnet_buffer(b0)->vcgn_uii;
238  int disposition = CNAT_V4_ICMP_Q_I2O_T;
239  int counter = CNAT_V4_ICMP_Q_I2O_T_PKT;
240 
242  u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
243  icmp_v4_t *icmp = (icmp_v4_t *)((u8*)ip + ipv4_hdr_len);
245  u32 node_counter_base_index = n->error_heap_index;
246  vlib_error_main_t * em = &vm->error_main;
247  cnat_session_entry_t *session_db = NULL;
249  cnat_key_t dest_info;
250 
252  if (PREDICT_FALSE(ip->ttl <= 1)) {
253  /* Try to generate ICMP error msg, as TTL is <= 1 */
254 
256  (ctx, ip, ctx->ru.rx.uidb_index)) {
257 
258  /* Generated ICMP */
259  disposition = CNAT_V4_ICMP_Q_I2O_T;
260  counter = CNAT_V4_ICMP_Q_I2O_TTL_GEN;
261  } else {
262  /* Could not generated ICMP - drop the packet */
263  disposition = CNAT_V4_ICMP_Q_I2O_D;
264  counter = CNAT_V4_ICMP_Q_I2O_TTL_DROP;
265  }
266  goto drop_pkt;
267  }
268  }
269 
270  if (PREDICT_TRUE(db_index != EMPTY)) {
271  db = cnat_main_db + db_index;
272  dest_info.k.port = 0;
273  dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
274 
276 
277  /* No DBL support, so just update the destn and proceed */
278  db->dst_ipv4 = dest_info.k.ipv4;
279  db->dst_port = dest_info.k.port;
280  goto update_pkt;
281  }
282 
283  if(PREDICT_FALSE(db->dst_ipv4 != dest_info.k.ipv4)) {
284  if(PREDICT_TRUE(db->nsessions == 1)) {
285  /* Handle one to 2 dest scenarion */
286  dest_info.k.vrf = db->in2out_key.k.vrf;
287  session_db = cnat_handle_1to2_session(db, &dest_info);
288 
289  if(PREDICT_FALSE(session_db == NULL)) {
290  disposition = CNAT_V4_ICMP_Q_I2O_D;
291  counter = CNAT_V4_ICMP_Q_I2O_NO_SESSION_DROP;
292  goto drop_pkt;
293  }
294  } else if (PREDICT_FALSE(db->nsessions == 0)) {
295  /* Should be a static entry
296  * Note this session as the first session and log
297  */
298  cnat_add_dest_n_log(db, &dest_info);
299  } else { /* Many translations exist already */
300  dest_info.k.vrf = db->in2out_key.k.vrf;
301  /* If session already exists,
302  * cnat_create_session_db_entry will return the existing db
303  * else create a new db
304  * If could not create, return NULL
305  */
306  session_db = cnat_create_session_db_entry(&dest_info,
307  db, TRUE);
308 
309  if(PREDICT_FALSE(session_db == NULL)) {
310  disposition = CNAT_V4_ICMP_Q_I2O_D;
311  counter = CNAT_V4_ICMP_Q_I2O_NO_SESSION_DROP;
312  goto drop_pkt;
313  }
314  }
315  }
316 
317 update_pkt:
318 
320  printf("\nDUMPING ICMP PKT BEFORE\n");
321  print_icmp_pkt(ip);
322  }
323 
325  /*
326  * Decrement TTL and update IPv4 checksum
327  */
329  }
330 
331  /*
332  * 1. update src ipv4 addr and src icmp identifier
333  * 2. update ipv4 checksum and icmp checksum
334  */
335  swap_ip_src_icmp_id(ip, icmp, db, db->in2out_key.k.vrf);
336 
338  printf("\nDUMPING ICMP PKT AFTER\n");
339  print_icmp_pkt(ip);
340  }
341 
342  /*
343  * update db counter, timer
344  */
345 
346  if(PREDICT_FALSE(session_db != 0)) {
347  CNAT_DB_TIMEOUT_RST(session_db);
348  } else {
350  }
351  db->in2out_pkts++;
353 
354  } else {
355  disposition = CNAT_V4_ICMP_Q_I2O_E;
356  counter = CNAT_V4_ICMP_Q_I2O_MISS_PKT;
357  }
358 
359 drop_pkt:
360 
361  em->counters[node_counter_base_index + counter] += 1;
362  return disposition;
363 }
364 
365 #include <vnet/pipeline.h>
366 
368  vlib_node_runtime_t * node,
369  vlib_frame_t * frame)
370 {
371  return dispatch_pipeline (vm, node, frame);
372 }
373 
374 
377  .name = "vcgn-v4-icmp-q-i2o",
378  .vector_size = sizeof (u32),
380 
383 
384  .n_next_nodes = CNAT_V4_ICMP_Q_I2O_NEXT,
385 
386  /* edit / add dispositions here */
387  .next_nodes = {
388  [CNAT_V4_ICMP_Q_I2O_E] = "vcgn-v4-icmp-q-i2o-e",
389  [CNAT_V4_ICMP_Q_I2O_T] = "ip4-input",
390  [CNAT_V4_ICMP_Q_I2O_D] = "error-drop",
391  },
392 };
393 
395 {
397 
398  mp->vlib_main = vm;
399  mp->vnet_main = vnet_get_main();
400 
401  return 0;
402 }
403 
#define foreach_cnat_ipv4_icmp_q_inside_input_error
u32 error_heap_index
Definition: node.h:244
cnat_main_db_entry_t * cnat_main_db
Definition: cnat_db_v2.c:195
#define CNAT_UPDATE_L3_ICMP_CHECKSUM(old_l3_1, old_l3_2, old_l4, old_l3_c, old_l4_c, new_l3_1, new_l3_2, new_l4)
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:2597
a
Definition: bitmap.h:393
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
static uword cnat_ipv4_icmp_q_inside_input_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define PREDICT_TRUE(x)
Definition: clib.h:98
#define NULL
Definition: clib.h:55
u64 key64
Definition: cnat_db.h:109
Definition: cnat_db.h:153
u32 icmp_debug_flag
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:1012
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
void swap_ip_src_icmp_id(ipv4_header *ip, icmp_v4_t *icmp, cnat_main_db_entry_t *db, u16 vrf)
#define SPP_CACHE_LINE_BYTES
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
index_slist_t * cnat_in2out_hash
Definition: cnat_db_v2.c:190
#define CNAT_DB_TIMEOUT_RST(db)
Definition: cnat_db.h:561
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
unsigned long u64
Definition: types.h:89
static void stage3(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
u16 dst_port
Definition: cnat_db.h:257
vlib_node_registration_t cnat_ipv4_icmp_q_inside_input_node
(constructor) VLIB_REGISTER_NODE (cnat_ipv4_icmp_q_inside_input_node)
#define PLATFORM_CNAT_SET_RX_VRF(ctx, rx_vrf, proto)
vlib_error_main_t error_main
Definition: main.h:124
#define PREDICT_FALSE(x)
Definition: clib.h:97
int icmpv4_generate_with_throttling(spp_ctx_t *ctx, ipv4_header *ipv4, u16 rx_uidb_index)
u64 * counters
Definition: error.h:73
cnat_ipv4_icmp_q_inside_input_main_t cnat_ipv4_icmp_q_inside_input_main
#define CNAT_ICMP
Definition: cnat_db.h:95
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static void stage2(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
#define ARRAY_LEN(x)
Definition: clib.h:59
cnat_db_key_t k
Definition: cnat_db.h:108
void cnat_add_dest_n_log(cnat_main_db_entry_t *mdb, cnat_key_t *dest_info)
Definition: cnat_db_v2.c:1068
unsigned int u32
Definition: types.h:88
#define CNAT_UPDATE_L3_CHECKSUM_DECLARE
#define vnet_buffer(b)
Definition: buffer.h:300
#define TRUE
Definition: cnat_db.h:78
#define PLATFORM_DBL_SUPPORT
cnat_key_t out2in_key
Definition: cnat_db.h:198
u64 uword
Definition: types.h:112
u16 nsessions
Definition: cnat_db.h:266
Definition: cnat_db.h:336
unsigned short u16
Definition: types.h:57
#define CNAT_MAIN_HASH_MASK
Definition: cnat_db.h:56
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
unsigned char u8
Definition: types.h:56
u32 dst_ipv4
Definition: cnat_db.h:254
always_inline void ipv4_decr_ttl_n_calc_csum(ipv4_header *ipv4)
Definition: cnat_global.c:61
struct _spp_ctx spp_ctx_t
u32 in2out_forwarding_count
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
clib_error_t * cnat_ipv4_icmp_q_inside_input_init(vlib_main_t *vm)
#define EMPTY
Definition: index_list.h:24
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
#define PLATFORM_HANDLE_TTL_DECREMENT
#define CNAT_UPDATE_L3_L4_CHECKSUM_DECLARE
#define CNAT_V4_GET_HASH(key64, hash, mask)
Definition: cnat_db.h:536
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
void print_icmp_pkt(ipv4_header *ip)
always_inline 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:45
cnat_key_t in2out_key
Definition: cnat_db.h:201
u32 in2out_pkts
Definition: cnat_db.h:210
#define CNAT_UPDATE_L3_CHECKSUM(old_l3_1, old_l3_2, old_l3_c,new_l3_1, new_l3_2)
static char * cnat_ipv4_icmp_q_inside_input_error_strings[]