2 *---------------------------------------------------------------------------
3 * cnat_ipv4_icmp_query_inside_input.c - cnat_ipv4_icmp_query_inside_input node pipeline stage 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 *---------------------------------------------------------------------------
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vppinfra/error.h>
23 #include <vnet/buffer.h>
25 #include "cnat_ipv4_icmp.h"
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")
35 #define _(sym,str) sym,
36 foreach_cnat_ipv4_icmp_q_inside_input_error
38 CNAT_IPV4_ICMP_Q_INSIDE_INPUT_N_ERROR,
39 } cnat_ipv4_icmp_q_inside_input_t;
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
48 u32 cached_next_index;
49 /* $$$$ add data here */
51 /* convenience variables */
52 vlib_main_t * vlib_main;
53 vnet_main_t * vnet_main;
54 } cnat_ipv4_icmp_q_inside_input_main_t;
60 CNAT_V4_ICMP_Q_I2O_NEXT,
61 } cnat_ipv4_icmp_q_inside_input_next_t;
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;
68 inline void swap_ip_src_icmp_id(ipv4_header *ip,
70 cnat_main_db_entry_t *db, u16 vrf)
79 if(is_static_dest_nat_enabled(vrf) == CNAT_SUCCESS) {
81 if(cnat_static_dest_db_get_translation(ip->dest_addr, &postmap_ip, vrf, direction) == CNAT_SUCCESS) {
82 CNAT_UPDATE_L3_CHECKSUM_DECLARE
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);
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;
95 spp_host_to_net_byte_order_16(new_l3_c);
102 CNAT_UPDATE_L3_L4_CHECKSUM_DECLARE
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))
116 clib_host_to_net_u32(db->out2in_key.k.ipv4);
118 clib_host_to_net_u16(new_l3_c);
122 clib_host_to_net_u16(db->out2in_key.k.port);
124 clib_host_to_net_u16(new_l4_c);
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.
131 #define stage0 generic_stage0
134 stage1(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
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);
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);
151 tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.port) << 32;
153 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
154 vnet_buffer(b0)->vcgn_uii.key.k.vrf,
156 tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.vrf) << 48;
158 CNAT_V4_GET_HASH(tmp, bucket, CNAT_MAIN_HASH_MASK)
160 prefetch_target = (u8 *)(&cnat_in2out_hash[bucket]);
161 vnet_buffer(b0)->vcgn_uii.bucket = bucket;
163 /* Prefetch the hash bucket */
164 CLIB_PREFETCH(prefetch_target, CLIB_CACHE_LINE_BYTES, LOAD);
167 #define SPP_LOG2_CACHE_LINE_BYTES 6
168 #define SPP_CACHE_LINE_BYTES (1 << SPP_LOG2_CACHE_LINE_BYTES)
171 stage2(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
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;
177 /* read the hash bucket */
178 u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket
179 = cnat_in2out_hash[bucket].next;
181 if (PREDICT_TRUE(db_index != EMPTY)) {
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.
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);
201 stage3(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
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;
208 * Note: if the search already failed (empty bucket),
209 * the answer is already in the pipeline context structure
211 if (PREDICT_TRUE(db_index != EMPTY)) {
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
219 db = cnat_main_db + db_index;
220 if (PREDICT_TRUE(db->in2out_key.key64 ==
221 vnet_buffer(b0)->vcgn_uii.key.key64)) {
224 db_index = db->in2out_hash.next;
225 } while (db_index != EMPTY);
227 /* Stick the answer back into the pipeline context structure */
228 vnet_buffer(b0)->vcgn_uii.bucket = db_index;
232 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
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;
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;
251 if (PLATFORM_HANDLE_TTL_DECREMENT) {
252 if (PREDICT_FALSE(ip->ttl <= 1)) {
253 /* Try to generate ICMP error msg, as TTL is <= 1 */
255 if (icmpv4_generate_with_throttling
256 (ctx, ip, ctx->ru.rx.uidb_index)) {
259 disposition = CNAT_V4_ICMP_Q_I2O_T;
260 counter = CNAT_V4_ICMP_Q_I2O_TTL_GEN;
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;
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);
275 if(PREDICT_TRUE(!PLATFORM_DBL_SUPPORT)) {
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;
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);
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;
294 } else if (PREDICT_FALSE(db->nsessions == 0)) {
295 /* Should be a static entry
296 * Note this session as the first session and log
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
306 session_db = cnat_create_session_db_entry(&dest_info,
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;
319 if (PREDICT_FALSE(icmp_debug_flag)) {
320 printf("\nDUMPING ICMP PKT BEFORE\n");
324 if (PLATFORM_HANDLE_TTL_DECREMENT) {
326 * Decrement TTL and update IPv4 checksum
328 ipv4_decr_ttl_n_calc_csum(ip);
332 * 1. update src ipv4 addr and src icmp identifier
333 * 2. update ipv4 checksum and icmp checksum
335 swap_ip_src_icmp_id(ip, icmp, db, db->in2out_key.k.vrf);
337 if (PREDICT_FALSE(icmp_debug_flag)) {
338 printf("\nDUMPING ICMP PKT AFTER\n");
343 * update db counter, timer
346 if(PREDICT_FALSE(session_db != 0)) {
347 CNAT_DB_TIMEOUT_RST(session_db);
349 CNAT_DB_TIMEOUT_RST(db);
352 in2out_forwarding_count++;
355 disposition = CNAT_V4_ICMP_Q_I2O_E;
356 counter = CNAT_V4_ICMP_Q_I2O_MISS_PKT;
361 em->counters[node_counter_base_index + counter] += 1;
365 #include <vnet/pipeline.h>
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)
371 return dispatch_pipeline (vm, node, frame);
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,
381 .n_errors = ARRAY_LEN(cnat_ipv4_icmp_q_inside_input_error_strings),
382 .error_strings = cnat_ipv4_icmp_q_inside_input_error_strings,
384 .n_next_nodes = CNAT_V4_ICMP_Q_I2O_NEXT,
386 /* edit / add dispositions here */
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",
394 clib_error_t *cnat_ipv4_icmp_q_inside_input_init (vlib_main_t *vm)
396 cnat_ipv4_icmp_q_inside_input_main_t * mp = &cnat_ipv4_icmp_q_inside_input_main;
399 mp->vnet_main = vnet_get_main();
404 VLIB_INIT_FUNCTION (cnat_ipv4_icmp_q_inside_input_init);