2 *---------------------------------------------------------------------------
3 * cnat_ipv4_udp_inside_input.c - cnat_ipv4_udp_inside_input node functions
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:
11 * http://www.apache.org/licenses/LICENSE-2.0
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 *---------------------------------------------------------------------------
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vppinfra/error.h>
24 #include <vnet/buffer.h>
26 #include "cnat_global.h"
28 #include "cnat_ipv4_udp.h"
29 #include "cnat_pcp_server.h"
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")
41 #define _(sym,str) sym,
42 foreach_cnat_ipv4_udp_inside_input_error
44 CNAT_IPV4_UDP_INSIDE_INPUT_N_ERROR,
45 } cnat_ipv4_udp_inside_input_t;
47 static char * cnat_ipv4_udp_inside_input_error_strings[] = {
48 #define _(sym,string) string,
49 foreach_cnat_ipv4_udp_inside_input_error
54 u32 cached_next_index;
55 /* $$$$ add data here */
57 /* convenience variables */
58 vlib_main_t * vlib_main;
59 vnet_main_t * vnet_main;
60 } cnat_ipv4_udp_inside_input_main_t;
66 CNAT_V4_UDP_I2O_T = CNAT_REWRITE_OUTPUT,
68 } cnat_ipv4_udp_inside_input_next_t;
70 #define CNAT_V4_UDP_I2O_D CNAT_V4_I2O_FIXME
71 #define CNAT_V4_PCP_T CNAT_V4_I2O_FIXME
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;
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.
82 #define stage0 generic_stage0
86 is_pcp_pkt(u32 addr, u16 port)
88 return CNAT_NO_CONFIG;
92 is_pcp_pkt(spp_ctx_t *ctx, u32 addr, u16 port)
94 cnat_vrfmap_t *my_vrfmap = NULL;
97 my_vrfmap_index = vrf_map_array[ctx->ru.rx.uidb_index];
99 if (PREDICT_TRUE(my_vrfmap_index != VRF_MAP_ENTRY_EMPTY)) {
101 my_vrfmap = cnat_map_by_vrf + my_vrfmap_index;
103 if (PREDICT_FALSE( port == my_vrfmap->pcp_server_port)) {
104 if(PREDICT_TRUE(addr == my_vrfmap->pcp_server_addr)) {
110 return CNAT_NO_CONFIG;
114 void swap_ip_src_udp_port(ipv4_header *ip,
116 cnat_main_db_entry_t *db)
121 CNAT_UPDATE_L3_L4_CHECKSUM_DECLARE
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))
134 /* #define UDP_PACKET_DEBUG 1 */
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");
146 clib_host_to_net_u32(db->out2in_key.k.ipv4);
148 clib_host_to_net_u16(new_l3_c);
151 clib_net_to_host_u16(ip->frag_flags_offset);
153 if(PREDICT_FALSE(frag_offset & IP_FRAG_OFFSET_MASK)) {
154 return; /* No need to update UDP fields */
158 clib_host_to_net_u16(db->out2in_key.k.port);
161 * No easy way to avoid this if check except by using
162 * complex logic - may not be worth it.
164 if (PREDICT_TRUE(udp->udp_checksum)) {
166 clib_host_to_net_u16(new_l4_c);
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;
175 if (PREDICT_FALSE(udp_inside_packet_dump_enable)) {
176 printf("\nIn2Out UDP packet after translation");
183 stage1(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
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);
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);
200 tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.port) << 32;
202 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
203 vnet_buffer(b0)->vcgn_uii.key.k.vrf,
205 tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.vrf) << 48;
207 CNAT_V4_GET_HASH(tmp, bucket, CNAT_MAIN_HASH_MASK)
209 prefetch_target = (u8 *)(&cnat_in2out_hash[bucket]);
210 vnet_buffer(b0)->vcgn_uii.bucket = bucket;
212 /* Prefetch the hash bucket */
213 CLIB_PREFETCH(prefetch_target, CLIB_CACHE_LINE_BYTES, LOAD);
217 stage2(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
220 #define SPP_LOG2_CACHE_LINE_BYTES 6
221 #define SPP_CACHE_LINE_BYTES (1 << SPP_LOG2_CACHE_LINE_BYTES)
224 stage3(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
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;
230 /* read the hash bucket */
231 u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket
232 = cnat_in2out_hash[bucket].next;
234 if (PREDICT_TRUE(db_index != EMPTY)) {
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.
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);
254 stage4(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
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;
261 * Note: if the search already failed (empty bucket),
262 * the answer is already in the pipeline context structure
264 if (PREDICT_TRUE(db_index != EMPTY)) {
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
272 db = cnat_main_db + db_index;
273 if (PREDICT_TRUE(db->in2out_key.key64 ==
274 vnet_buffer(b0)->vcgn_uii.key.key64)) {
277 db_index = db->in2out_hash.next;
278 } while (db_index != EMPTY);
280 /* Stick the answer back into the pipeline context structure */
281 vnet_buffer(b0)->vcgn_uii.bucket = db_index;
285 static u64 pkt_num = 0;
286 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
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;
305 if(PREDICT_FALSE(is_pcp_pkt(ip->dest_addr, udp->dest_port) ==
309 disposition = CNAT_V4_PCP_T;
310 counter = CNAT_V4_PCP_PKT;
315 if (PLATFORM_HANDLE_TTL_DECREMENT) {
316 if (PREDICT_FALSE(ip->ttl <= 1)) {
317 /* Try to generate ICMP error msg, as TTL is <= 1 */
319 if (icmpv4_generate_with_throttling
320 (ctx, ip, ctx->ru.rx.uidb_index)) {
322 disposition = CNAT_REWRITE_OUTPUT;
323 counter = CNAT_V4_UDP_I2O_TTL_GEN;
325 /* Could not generated ICMP - drop the packet */
326 disposition = CNAT_V4_UDP_I2O_D;
327 counter = CNAT_V4_UDP_I2O_TTL_DROP;
332 if (PREDICT_TRUE(db_index != EMPTY)) {
333 cnat_main_db_entry_t *db = cnat_main_db + db_index;
335 dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
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)) {
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;
349 dest_info.k.port = clib_net_to_host_u16(udp->dest_port);
354 if (PLATFORM_HANDLE_TTL_DECREMENT) {
356 * Decrement TTL and update IPv4 checksum
358 ipv4_decr_ttl_n_calc_csum(ip);
361 if(PREDICT_TRUE(!PLATFORM_DBL_SUPPORT)) {
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);
370 if(PREDICT_TRUE((db->dst_ipv4 == dest_info.k.ipv4) &&
371 (db->dst_port == dest_info.k.port))) {
373 CNAT_DB_TIMEOUT_RST(db);
376 if (PREDICT_FALSE(db->nsessions == 0)) {
377 /* Should be a static entry
378 * Note this session as the first session and log
380 cnat_add_dest_n_log(db, &dest_info);
382 * update db counter, timer
385 CNAT_DB_TIMEOUT_RST(db);
387 } else if(PREDICT_TRUE(db->nsessions == 1)) {
388 /* Destn is not same as in main db. Multiple session
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);
394 dest_info.k.vrf = db->in2out_key.k.vrf;
395 session_db = cnat_handle_1to2_session(db, &dest_info);
397 if(PREDICT_TRUE(session_db != NULL)) {
399 CNAT_DB_TIMEOUT_RST(session_db);
401 /* could not create session db - drop packet */
402 disposition = CNAT_V4_UDP_I2O_D;
403 counter = CNAT_V4_UDP_I2O_SESSION_DROP;
408 /* More than 2 sessions exists */
410 dest_info.k.vrf = db->in2out_key.k.vrf;
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
417 session_db = cnat_create_session_db_entry(&dest_info,
420 if(PREDICT_FALSE(session_db != NULL)) {
422 CNAT_DB_TIMEOUT_RST(session_db);
424 /* could not create session db - drop packet */
425 disposition = CNAT_V4_UDP_I2O_D;
426 counter = CNAT_V4_UDP_I2O_SESSION_DROP;
434 * 1. update src ipv4 addr and src udp port
435 * 2. update ipv4 checksum and udp checksum
437 swap_ip_src_udp_port(ip, udp, db);
439 * update db counter, timer
445 * need to set outside vrf
446 * from db->out2in_key.k.vrf
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)
453 in2out_forwarding_count++;
456 disposition = CNAT_V4_UDP_I2O_E;
457 counter = CNAT_V4_UDP_I2O_MISS_PKT;
463 em->counters[node_counter_base_index + counter] += 1;
468 #include <vnet/pipeline.h>
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)
474 return dispatch_pipeline (vm, node, frame);
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,
484 .n_errors = ARRAY_LEN(cnat_ipv4_udp_inside_input_error_strings),
485 .error_strings = cnat_ipv4_udp_inside_input_error_strings,
487 .n_next_nodes = CNAT_N_NEXT,
489 /* edit / add dispositions here */
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",
498 clib_error_t *cnat_ipv4_udp_inside_input_init (vlib_main_t *vm)
500 cnat_ipv4_udp_inside_input_main_t * mp = &cnat_ipv4_udp_inside_input_main;
503 mp->vnet_main = vnet_get_main();
508 VLIB_INIT_FUNCTION (cnat_ipv4_udp_inside_input_init);