Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cnat_ipv4_udp_inside_input.c
1 /*
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_udp_inside_input.c - cnat_ipv4_udp_inside_input node 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
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vppinfra/error.h>
24 #include <vnet/buffer.h>
25
26 #include "cnat_global.h"
27 #include "cnat_db.h"
28 #include "cnat_ipv4_udp.h"
29 #include "cnat_pcp_server.h"
30
31
32 #define foreach_cnat_ipv4_udp_inside_input_error                \
33 _(CNAT_V4_UDP_I2O_T_PKT, "v4 udp i2o transmit")                 \
34 _(CNAT_V4_UDP_I2O_MISS_PKT, "v4 udp i2o db miss")               \
35 _(CNAT_V4_UDP_I2O_TTL_GEN, "v4 udp i2o TTL gen")                \
36 _(CNAT_V4_UDP_I2O_TTL_DROP, "v4 udp i2o TTL drop")              \
37 _(CNAT_V4_PCP_PKT, "v4 pcp pkt")                                \
38 _(CNAT_V4_UDP_I2O_SESSION_DROP, "v4 udp i2o session drop")
39
40 typedef enum {
41 #define _(sym,str) sym,
42   foreach_cnat_ipv4_udp_inside_input_error 
43 #undef _
44   CNAT_IPV4_UDP_INSIDE_INPUT_N_ERROR,
45 } cnat_ipv4_udp_inside_input_t;
46
47 static char * cnat_ipv4_udp_inside_input_error_strings[] = {
48 #define _(sym,string) string,
49   foreach_cnat_ipv4_udp_inside_input_error
50 #undef _
51 };
52
53 typedef struct {
54   u32 cached_next_index;
55   /* $$$$ add data here */
56
57   /* convenience variables */
58   vlib_main_t * vlib_main;
59   vnet_main_t * vnet_main;
60 } cnat_ipv4_udp_inside_input_main_t;
61
62 typedef enum {
63     CNAT_V4_I2O_FIXME,
64     CNAT_V4_UDP_I2O_E,
65     CNAT_REWRITE_OUTPUT,
66     CNAT_V4_UDP_I2O_T = CNAT_REWRITE_OUTPUT, 
67     CNAT_N_NEXT,
68 } cnat_ipv4_udp_inside_input_next_t;
69
70 #define CNAT_V4_UDP_I2O_D CNAT_V4_I2O_FIXME
71 #define CNAT_V4_PCP_T CNAT_V4_I2O_FIXME
72
73 cnat_ipv4_udp_inside_input_main_t cnat_ipv4_udp_inside_input_main;
74 vlib_node_registration_t cnat_ipv4_udp_inside_input_node;
75
76 #define NSTAGES 6
77
78 /*
79  * Use the generic buffer metadata + first line of packet data prefetch
80  * stage function from <api/pipeline.h>. This is usually a Good Idea.
81  */
82 #define stage0 generic_stage0
83
84 #ifndef TOBE_PORTED
85 static inline u32
86 is_pcp_pkt(u32 addr, u16 port)
87 {
88     return CNAT_NO_CONFIG;
89 }
90 #else
91 static inline u32
92 is_pcp_pkt(spp_ctx_t *ctx, u32 addr, u16 port)
93 {
94     cnat_vrfmap_t *my_vrfmap = NULL;
95     u16  my_vrfmap_index;
96
97     my_vrfmap_index = vrf_map_array[ctx->ru.rx.uidb_index];
98
99     if (PREDICT_TRUE(my_vrfmap_index != VRF_MAP_ENTRY_EMPTY)) {
100
101       my_vrfmap = cnat_map_by_vrf + my_vrfmap_index;
102
103       if (PREDICT_FALSE( port ==  my_vrfmap->pcp_server_port)) {
104              if(PREDICT_TRUE(addr == my_vrfmap->pcp_server_addr)) {
105                return CNAT_SUCCESS;
106              }
107       }
108     }
109
110     return CNAT_NO_CONFIG;
111 }
112 #endif
113
114 inline void swap_ip_src_udp_port(ipv4_header *ip,
115                                  udp_hdr_type_t *udp,
116                                  cnat_main_db_entry_t *db)
117 {
118     /*
119      * declare varibale
120      */
121     CNAT_UPDATE_L3_L4_CHECKSUM_DECLARE
122     /*
123      * calculate checksum
124      */
125     CNAT_UPDATE_L3_L4_CHECKSUM(((u16)(db->in2out_key.k.ipv4)),
126                                ((u16)(db->in2out_key.k.ipv4 >> 16)),
127                                (db->in2out_key.k.port),
128                                (clib_net_to_host_u16(ip->checksum)),
129                                (clib_net_to_host_u16(udp->udp_checksum)),
130                                ((u16)(db->out2in_key.k.ipv4)),
131                                ((u16)(db->out2in_key.k.ipv4 >> 16)),
132                                (db->out2in_key.k.port))
133
134 /* #define UDP_PACKET_DEBUG 1 */
135
136 // Temporary debugs which will be suppressed later
137 #ifdef UDP_PACKET_DEBUG
138     if (PREDICT_FALSE(udp_inside_packet_dump_enable)) {
139     printf("\nIn2Out UDP packet before translation");
140     print_udp_pkt(ip);
141     }
142 #endif
143
144     //set ip header
145     ip->src_addr =
146         clib_host_to_net_u32(db->out2in_key.k.ipv4);
147     ip->checksum =
148         clib_host_to_net_u16(new_l3_c);
149
150     u16 frag_offset =
151         clib_net_to_host_u16(ip->frag_flags_offset);
152
153     if(PREDICT_FALSE(frag_offset & IP_FRAG_OFFSET_MASK)) {
154         return; /* No need to update UDP fields */
155     }
156     //set udp header
157     udp->src_port =
158         clib_host_to_net_u16(db->out2in_key.k.port);
159
160     /*
161      * No easy way to avoid this if check except by using
162      * complex logic - may not be worth it.
163      */
164     if (PREDICT_TRUE(udp->udp_checksum)) {
165     udp->udp_checksum =
166         clib_host_to_net_u16(new_l4_c);
167     }
168
169 // Temporary debugs which will be suppressed later
170 #ifdef UDP_PACKET_DEBUG
171     if (PREDICT_FALSE(udp_inside_checksum_disable)) {
172     printf("\nIn2Out UDP checksum 0x%x disabled by force", new_l4_c);
173     udp->udp_checksum = 0;
174     }
175     if (PREDICT_FALSE(udp_inside_packet_dump_enable)) {
176     printf("\nIn2Out UDP packet after translation");
177     print_udp_pkt(ip);
178     }
179 #endif
180 }
181
182 static inline void
183 stage1(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
184 {
185     u64 a, b, c;
186     u32 bucket;
187     u8 *prefetch_target;
188
189     vlib_buffer_t * b0 = vlib_get_buffer (vm, buffer_index);
190     ipv4_header *ip = vlib_buffer_get_current (b0);
191     u8   ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
192     udp_hdr_type_t *udp = (udp_hdr_type_t *)((u8*)ip + ipv4_hdr_len);
193   
194     u64 tmp = 0;
195     tmp = vnet_buffer(b0)->vcgn_uii.key.k.ipv4 =
196             clib_net_to_host_u32(ip->src_addr);
197     vnet_buffer(b0)->vcgn_uii.key.k.port =
198             clib_net_to_host_u16 (udp->src_port);
199
200     tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.port) << 32;
201
202     PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
203                              vnet_buffer(b0)->vcgn_uii.key.k.vrf,
204                              CNAT_UDP)
205     tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.vrf) << 48;
206
207     CNAT_V4_GET_HASH(tmp, bucket, CNAT_MAIN_HASH_MASK)
208
209     prefetch_target = (u8 *)(&cnat_in2out_hash[bucket]);
210     vnet_buffer(b0)->vcgn_uii.bucket = bucket;
211
212     /* Prefetch the hash bucket */
213     CLIB_PREFETCH(prefetch_target, CLIB_CACHE_LINE_BYTES, LOAD);
214 }
215
216 static inline void
217 stage2(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
218 { /* nothing */ }
219
220 #define SPP_LOG2_CACHE_LINE_BYTES 6
221 #define SPP_CACHE_LINE_BYTES (1 << SPP_LOG2_CACHE_LINE_BYTES)
222
223 static inline void
224 stage3(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
225 {
226     vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
227     uword prefetch_target0, prefetch_target1;
228     u32 bucket = vnet_buffer(b0)->vcgn_uii.bucket;
229   
230     /* read the hash bucket */
231     u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket
232                  = cnat_in2out_hash[bucket].next;
233
234     if (PREDICT_TRUE(db_index != EMPTY)) {
235         /*
236          * Prefetch database keys. We save space by not cache-line
237          * aligning the DB entries. We don't want to waste LSU
238          * bandwidth prefetching stuff we won't need.
239          */
240         prefetch_target0 = (uword)(cnat_main_db + db_index);
241         CLIB_PREFETCH((void*)prefetch_target0, CLIB_CACHE_LINE_BYTES, LOAD);
242         /* Just beyond DB key #2 */
243         prefetch_target1 = prefetch_target0 +
244         STRUCT_OFFSET_OF(cnat_main_db_entry_t, user_ports);
245         /* If the targets are in different lines, do the second prefetch */
246         if (PREDICT_FALSE((prefetch_target0 & ~(SPP_CACHE_LINE_BYTES-1)) !=
247                       (prefetch_target1 & ~(SPP_CACHE_LINE_BYTES-1)))) {
248             CLIB_PREFETCH((void *)prefetch_target1, CLIB_CACHE_LINE_BYTES, LOAD);
249         }
250     }
251 }
252
253 static inline void
254 stage4(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
255 {
256   cnat_main_db_entry_t *db;
257   vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
258   u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
259
260   /*
261    * Note: if the search already failed (empty bucket),
262    * the answer is already in the pipeline context structure
263    */
264   if (PREDICT_TRUE(db_index != EMPTY)) {
265
266     /*
267      * Note: hash collisions suck. We can't easily prefetch around them.
268      * The first trip around the track will be fast. After that, maybe
269      * not so much...
270      */
271     do {
272       db = cnat_main_db + db_index;
273       if (PREDICT_TRUE(db->in2out_key.key64 ==
274                   vnet_buffer(b0)->vcgn_uii.key.key64)) {
275         break;
276       }
277       db_index = db->in2out_hash.next;
278     } while (db_index != EMPTY);
279
280     /* Stick the answer back into the pipeline context structure */
281     vnet_buffer(b0)->vcgn_uii.bucket = db_index;
282   }
283 }
284
285 static u64 pkt_num = 0;
286 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
287                               u32 bi)
288 {
289     vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
290     u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
291     spp_ctx_t *ctx = (spp_ctx_t *) &vnet_buffer(b0)->vcgn_uii;
292     int disposition = CNAT_V4_UDP_I2O_T;
293     int counter = CNAT_V4_UDP_I2O_T_PKT;
294     ipv4_header *ip = (ipv4_header *)vlib_buffer_get_current(b0);
295     u8   ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
296     udp_hdr_type_t *udp = (udp_hdr_type_t *)((u8*)ip + ipv4_hdr_len);
297     vlib_node_t *n = vlib_get_node (vm, cnat_ipv4_udp_inside_input_node.index);
298     u32 node_counter_base_index = n->error_heap_index;
299     vlib_error_main_t * em = &vm->error_main;
300     cnat_session_entry_t *session_db = NULL;
301     cnat_key_t dest_info;
302
303     pkt_num++;
304
305     if(PREDICT_FALSE(is_pcp_pkt(ip->dest_addr, udp->dest_port) == 
306                      CNAT_SUCCESS)) 
307     {
308         PCP_INCR(input);
309         disposition = CNAT_V4_PCP_T;
310         counter = CNAT_V4_PCP_PKT;
311
312         goto pcp_pkt;
313     }
314
315     if (PLATFORM_HANDLE_TTL_DECREMENT) {
316         if (PREDICT_FALSE(ip->ttl <= 1)) {
317             /* Try to generate ICMP error msg, as TTL is <= 1 */
318
319             if (icmpv4_generate_with_throttling
320                     (ctx, ip, ctx->ru.rx.uidb_index)) {
321                 /* Generated ICMP */
322                 disposition = CNAT_REWRITE_OUTPUT;
323                 counter = CNAT_V4_UDP_I2O_TTL_GEN;
324             } else {
325                 /* Could not generated ICMP - drop the packet */
326                 disposition = CNAT_V4_UDP_I2O_D;
327                 counter = CNAT_V4_UDP_I2O_TTL_DROP;
328             }
329             goto drop_pkt;
330         }
331     }
332     if (PREDICT_TRUE(db_index != EMPTY)) {
333         cnat_main_db_entry_t *db = cnat_main_db + db_index;
334
335          dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
336
337          /* MUST revisit: it seems farg is set to 1 for few packets & because of
338           * this the port is not updated & it becomes 0. Commenting teporarily 
339           * this fargment check & setting dst port with udp dst port value */
340          dest_info.k.port = clib_net_to_host_u16(udp->dest_port);
341           #if 0  // DONOT REMOVE THIS if 0
342          if(PREDICT_FALSE(ctx->ru.rx.frag)) {
343 #ifdef TOBE_PORTED
344              /* Must have routed through cnat_v4_frag_in2out node */
345              u16 *feature_data_ports = (u16 *)&ctx->feature_data[4];
346              dest_info.k.port = *feature_data_ports;
347 #endif
348          } else {
349              dest_info.k.port = clib_net_to_host_u16(udp->dest_port);
350          }
351          #endif
352
353
354         if (PLATFORM_HANDLE_TTL_DECREMENT) {
355             /*
356              * Decrement TTL and update IPv4 checksum
357              */
358             ipv4_decr_ttl_n_calc_csum(ip);
359         }
360
361         if(PREDICT_TRUE(!PLATFORM_DBL_SUPPORT)) {
362
363             /* No DBL support, so just update the destn and proceed */
364             db->dst_ipv4 = dest_info.k.ipv4;
365             db->dst_port = dest_info.k.port;
366             CNAT_DB_TIMEOUT_RST(db);
367             goto update_pkt;
368         }
369
370         if(PREDICT_TRUE((db->dst_ipv4 == dest_info.k.ipv4) &&
371                           (db->dst_port == dest_info.k.port))) {
372
373             CNAT_DB_TIMEOUT_RST(db);
374             goto update_pkt;
375         } else {
376             if (PREDICT_FALSE(db->nsessions == 0)) {
377                 /* Should be a static entry
378                  * Note this session as the first session and log
379                  */
380                 cnat_add_dest_n_log(db, &dest_info);
381                 /*
382                  * update db counter, timer
383                  */
384
385                 CNAT_DB_TIMEOUT_RST(db);
386
387             } else if(PREDICT_TRUE(db->nsessions == 1)) {
388                 /* Destn is not same as in main db. Multiple session
389                  * scenario
390                  */
391                 //printf(">>> [pkt# %lu] src_ip: 0x%x, db ip: 0x%x, db port: %u; dest ip: 0x%x, dest port: %u\n",
392                 //    pkt_num, ntohl(ip->src_addr), db->dst_ipv4, db->dst_port, dest_info.k.ipv4, dest_info.k.port);
393
394                 dest_info.k.vrf = db->in2out_key.k.vrf;
395                 session_db = cnat_handle_1to2_session(db, &dest_info);
396
397                 if(PREDICT_TRUE(session_db != NULL)) {
398                     /* session exists */
399                     CNAT_DB_TIMEOUT_RST(session_db);
400                 } else {
401                     /* could not create session db - drop packet */
402                     disposition = CNAT_V4_UDP_I2O_D;
403                     counter = CNAT_V4_UDP_I2O_SESSION_DROP;
404                     goto drop_pkt;
405                 }
406
407             } else {
408                 /* More than 2 sessions exists */
409
410                 dest_info.k.vrf = db->in2out_key.k.vrf;
411
412                 /* If session already exists,
413                  * cnat_create_session_db_entry will return the existing db
414                  * else create a new db
415                  * If could not create, return NULL
416                  */
417                 session_db = cnat_create_session_db_entry(&dest_info,
418                                                              db, TRUE);
419
420                 if(PREDICT_FALSE(session_db != NULL)) {
421                     /* session exists */
422                     CNAT_DB_TIMEOUT_RST(session_db);
423                 } else {
424                     /* could not create session db - drop packet */
425                     disposition = CNAT_V4_UDP_I2O_D;
426                     counter = CNAT_V4_UDP_I2O_SESSION_DROP;
427                     goto drop_pkt;
428                 }
429             }
430         }
431
432 update_pkt:
433         /*
434          * 1. update src ipv4 addr and src udp port
435          * 2. update ipv4 checksum and udp checksum
436          */
437         swap_ip_src_udp_port(ip, udp, db);
438         /*
439          * update db counter, timer
440          */
441
442         db->in2out_pkts++;
443
444         /*
445          * need to set outside vrf
446          * from db->out2in_key.k.vrf
447          */
448                 
449         /* Temporarily keeping this commented */
450         //PLATFORM_CNAT_SET_TX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_TX], 
451         //              db->out2in_key.k.vrf)
452
453         in2out_forwarding_count++;
454
455     } else {
456         disposition = CNAT_V4_UDP_I2O_E;
457         counter = CNAT_V4_UDP_I2O_MISS_PKT;
458     }
459
460 drop_pkt:
461 pcp_pkt:
462
463     em->counters[node_counter_base_index + counter] += 1;
464
465     return  disposition;
466 }
467
468 #include <vnet/pipeline.h>
469
470 static uword cnat_ipv4_udp_inside_input_node_fn (vlib_main_t * vm,
471                               vlib_node_runtime_t * node,
472                               vlib_frame_t * frame)
473 {
474     return dispatch_pipeline (vm, node, frame);
475 }
476
477
478 VLIB_REGISTER_NODE (cnat_ipv4_udp_inside_input_node) = {
479   .function = cnat_ipv4_udp_inside_input_node_fn,
480   .name = "vcgn-v4-udp-i2o",
481   .vector_size = sizeof (u32),
482   .type = VLIB_NODE_TYPE_INTERNAL,
483
484   .n_errors = ARRAY_LEN(cnat_ipv4_udp_inside_input_error_strings),
485   .error_strings = cnat_ipv4_udp_inside_input_error_strings,
486
487   .n_next_nodes = CNAT_N_NEXT,
488
489   /* edit / add dispositions here */
490   .next_nodes = {
491         [CNAT_V4_I2O_FIXME] = "error-drop",
492         // [CNAT_V4_UDP_I2O_T] = "ip4-input",
493         [CNAT_V4_UDP_I2O_E] = "vcgn-v4-udp-i2o-e",
494         [CNAT_REWRITE_OUTPUT] = "ip4-input",
495   },
496 };
497
498 clib_error_t *cnat_ipv4_udp_inside_input_init (vlib_main_t *vm)
499 {
500   cnat_ipv4_udp_inside_input_main_t * mp = &cnat_ipv4_udp_inside_input_main;
501
502   mp->vlib_main = vm;
503   mp->vnet_main = vnet_get_main();
504
505   return 0;
506 }
507
508 VLIB_INIT_FUNCTION (cnat_ipv4_udp_inside_input_init);