More janitorial work
[vpp.git] / plugins / plugins / vcgn / cnat_ipv4_icmp_query_inside_input.c
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,
36   foreach_cnat_ipv4_icmp_q_inside_input_error 
37 #undef _
38   CNAT_IPV4_ICMP_Q_INSIDE_INPUT_N_ERROR,
39 } cnat_ipv4_icmp_q_inside_input_t;
40
41 static char * cnat_ipv4_icmp_q_inside_input_error_strings[] = {
42 #define _(sym,string) string,
43   foreach_cnat_ipv4_icmp_q_inside_input_error
44 #undef _
45 };
46
47 typedef struct {
48   u32 cached_next_index;
49   /* $$$$ add data here */
50
51   /* convenience variables */
52   vlib_main_t * vlib_main;
53   vnet_main_t * vnet_main;
54 } cnat_ipv4_icmp_q_inside_input_main_t;
55
56 typedef enum {
57     CNAT_V4_ICMP_Q_I2O_T,
58     CNAT_V4_ICMP_Q_I2O_E,
59     CNAT_V4_ICMP_Q_I2O_D,
60     CNAT_V4_ICMP_Q_I2O_NEXT,
61 } cnat_ipv4_icmp_q_inside_input_next_t;
62
63 cnat_ipv4_icmp_q_inside_input_main_t cnat_ipv4_icmp_q_inside_input_main;
64 vlib_node_registration_t cnat_ipv4_icmp_q_inside_input_node;
65
66 #define NSTAGES 5
67
68 inline void swap_ip_src_icmp_id(ipv4_header *ip,
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) {
82             CNAT_UPDATE_L3_CHECKSUM_DECLARE 
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      */
102     CNAT_UPDATE_L3_L4_CHECKSUM_DECLARE
103     /*
104      * calculate checksum
105      */
106     CNAT_UPDATE_L3_ICMP_CHECKSUM(((u16)(db->in2out_key.k.ipv4)),
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);
141     ipv4_header *ip = vlib_buffer_get_current (b0);
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
158     CNAT_V4_GET_HASH(tmp, bucket, CNAT_MAIN_HASH_MASK)
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 +
191         STRUCT_OFFSET_OF(cnat_main_db_entry_t, user_ports);
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 {
203   cnat_main_db_entry_t *db;
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
241     ipv4_header *ip = (ipv4_header *)vlib_buffer_get_current(b0);
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);
244     vlib_node_t *n = vlib_get_node (vm, cnat_ipv4_icmp_q_inside_input_node.index);
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;
248     cnat_main_db_entry_t *db = NULL;
249     cnat_key_t dest_info;
250
251     if (PLATFORM_HANDLE_TTL_DECREMENT) {
252         if (PREDICT_FALSE(ip->ttl <= 1)) {
253             /* Try to generate ICMP error msg, as TTL is <= 1 */
254
255             if (icmpv4_generate_with_throttling
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
275         if(PREDICT_TRUE(!PLATFORM_DBL_SUPPORT)) {
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
319         if (PREDICT_FALSE(icmp_debug_flag)) {
320             printf("\nDUMPING ICMP PKT BEFORE\n");
321             print_icmp_pkt(ip);
322         }
323
324         if (PLATFORM_HANDLE_TTL_DECREMENT) {
325             /*
326              * Decrement TTL and update IPv4 checksum
327              */
328             ipv4_decr_ttl_n_calc_csum(ip);
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
337         if (PREDICT_FALSE(icmp_debug_flag)) {
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 {
349             CNAT_DB_TIMEOUT_RST(db);
350         }
351         db->in2out_pkts++;
352         in2out_forwarding_count++;
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
367 static uword cnat_ipv4_icmp_q_inside_input_node_fn (vlib_main_t * vm,
368                               vlib_node_runtime_t * node,
369                               vlib_frame_t * frame)
370 {
371     return dispatch_pipeline (vm, node, frame);
372 }
373
374
375 VLIB_REGISTER_NODE (cnat_ipv4_icmp_q_inside_input_node) = {
376   .function = cnat_ipv4_icmp_q_inside_input_node_fn,
377   .name = "vcgn-v4-icmp-q-i2o",
378   .vector_size = sizeof (u32),
379   .type = VLIB_NODE_TYPE_INTERNAL,
380
381   .n_errors = ARRAY_LEN(cnat_ipv4_icmp_q_inside_input_error_strings),
382   .error_strings = cnat_ipv4_icmp_q_inside_input_error_strings,
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
394 clib_error_t *cnat_ipv4_icmp_q_inside_input_init (vlib_main_t *vm)
395 {
396   cnat_ipv4_icmp_q_inside_input_main_t * mp = &cnat_ipv4_icmp_q_inside_input_main;
397
398   mp->vlib_main = vm;
399   mp->vnet_main = vnet_get_main();
400
401   return 0;
402 }
403
404 VLIB_INIT_FUNCTION (cnat_ipv4_icmp_q_inside_input_init);