Added support for mis-ordered packets in rapid
[samplevnf.git] / VNFs / vCGNAPT / pipeline / pipeline_cgnapt_be.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef __INCLUDE_PIPELINE_CGNAPT_BE_H__
18 #define __INCLUDE_PIPELINE_CGNAPT_BE_H__
19
20 /**
21  * @file
22  * Pipeline CG-NAPT BE.
23  *
24  * Pipeline CG-NAPT Back End (BE).
25  * Responsible for packet processing.
26  *
27  */
28
29 #include "pipeline_common_be.h"
30 #include "vnf_common.h"
31 #include <rte_pipeline.h>
32 #include <rte_hash.h>
33 #include "pipeline_timer_be.h"
34 #include "pipeline_arpicmp_be.h"
35 #include "cgnapt_pcp_be.h"
36 #include "lib_arp.h"
37
38 #define PIPELINE_CGNAPT_KEY_MAX_SIZE  64
39
40 extern uint8_t CGNAPT_DEBUG;
41 #define CGNAPT_DBG_CMD_OFST 8
42 #define CGNAPT_DBG_CMD_STATS_SHOW 0
43 #define CGNAPT_DBG_CMD_STATS_CLEAR 1
44 #define CGNAPT_DBG_CMD_DBG_LEVEL 2
45 #define CGNAPT_DBG_CMD_DBG_SHOW 3
46 #define CGNAPT_DBG_CMD_LS_ENTRY 4
47 #define CGNAPT_DBG_CMD_DYN 5
48 #define CGNAPT_DBG_CMD_IF_STATS 6
49 #define CGNAPT_DBG_CMD_INSTRUMENTATION 7
50 #define CGNAPT_DBG_CMD_ITER_COM_TBL 8
51 #define CGNAPT_DBG_CMD_MAPS_INFO 9
52 #define CGNAPT_DBG_CMD_OFST1 10
53 #define CGNAPT_DBG_CMD_IPV6 11
54 #define CGNAPT_DBG_CMD_PRINT_DS 12
55 #define CGNAPT_DBG_CMD_PRINT_NSP 13
56 #define CGNAPT_DBG_MAX_CLI_PER_PUB_IP 14
57 #define CGNAPT_DBG_PUB_IP_LIST 15
58 #define CGNAPT_DBG_TIMING_INST 16
59
60
61 #ifdef PCP_ENABLE
62
63 #define CGNAPT_DBG_PCP 17
64 /* PCP sub commands */
65 enum{
66 CGNAPT_PCP_CMD_STATS,
67 CGNAPT_PCP_CMD_PCP_ENABLE,
68 CGNAPT_PCP_CMD_GET_LIFETIME,
69 CGNAPT_PCP_CMD_SET_LIFETIME,
70 CGNAPT_PCP_CMD_OFST = 8,
71 };
72
73 #endif
74
75 /*
76  * CGNAPT_DBG_CMD_INSTRUMENTATION Sub commands
77 */
78  #define CGNAPT_CMD_INSTRUMENTATION_SUB0 0
79  #define CGNAPT_CMD_INSTRUMENTATION_SUB1 1
80  #define CGNAPT_CMD_INSTRUMENTATION_SUB2 2
81
82 /*
83  * CGNAPT_DBG_CMD_IF_STATS Sub commands
84 */
85 #define CGNAPT_IF_STATS_HWQ 0
86 #define CGNAPT_IF_STATS_SWQ 1
87 #define CGNAPT_IF_STATS_OTH 2
88
89 /* Version command info */
90 #define CGNAPT_VER_CMD_OFST 8
91 #define CGNAPT_VER_CMD_VER 1
92
93 /* Network Specific Prefix commnd */
94 #define CGNAPT_NSP_CMD_OFST 8
95
96 /* #define PIPELINE_CGNAPT_INSTRUMENTATION */
97 #ifdef PIPELINE_CGNAPT_INSTRUMENTATION
98 void *instrumentation_port_in_arg;
99 struct rte_mempool *cgnapt_test_pktmbuf_pool;
100
101 #define INST_ARRAY_SIZE 100000
102 #define CGNAPT_INST5_SIG 0xAA
103 #define CGNAPT_INST5_WAIT 200
104 #define CGNAPT_INST5_OFST 10
105
106 uint64_t *inst_start_time;
107 uint64_t *inst_end_time;
108 uint32_t *inst_diff_time;
109
110 uint32_t cgnapt_inst_index;
111 uint32_t cgnapt_inst5_flag;
112 uint32_t cgnapt_inst5_wait;
113 uint8_t cgnapt_num_func_to_inst;
114
115 #endif
116
117 #define CGNAPT_VERSION "1.8"
118 #define CGNAPT_DYN_TIMEOUT (3*10)       /* 30 secs */
119 #define MAX_DYN_ENTRY (70000 * 16)
120
121 #define NAPT_ENTRY_STALE 1
122 #define NAPT_ENTRY_VALID 0
123
124 /* For max_port_per_client */
125 #define MAX_PORT_INVALID_KEY -1
126 #define MAX_PORT_NOT_REACHED  0
127 #define MAX_PORT_REACHED      1
128 /* increment */
129 #define MAX_PORT_INC_SUCCESS  1
130 #define MAX_PORT_INC_REACHED  0
131 #define MAX_PORT_INC_ERROR   -1
132 /* decrement */
133 #define MAX_PORT_DEC_SUCCESS  1
134 #define MAX_PORT_DEC_REACHED  0
135 #define MAX_PORT_DEC_ERROR   -1
136 /* add_entry */
137 #define MAX_PORT_ADD_SUCCESS    1
138 #define MAX_PORT_ADD_UNSUCCESS  0
139 #define MAX_PORT_ADD_ERROR     -1
140 /* del_entry */
141 #define MAX_PORT_DEL_SUCCESS    1
142 #define MAX_PORT_DEL_UNSUCCESS  0
143 #define MAX_PORT_DEL_ERROR     -1
144
145 #define PIPELINE_CGNAPT_TABLE_AH_HIT(f_ah, f_pkt_work, f_pkt4_work)     \
146 static int                                                              \
147 f_ah(                                                                   \
148         struct rte_pipeline *rte_p,                                     \
149         struct rte_mbuf **pkts,                                         \
150         uint64_t pkts_mask,                                             \
151         struct rte_pipeline_table_entry **entries,                      \
152         void *arg)                                                      \
153 {                                                                       \
154         uint64_t pkts_in_mask = pkts_mask;                              \
155         uint64_t pkts_out_mask = pkts_mask;                             \
156         uint64_t time = rte_rdtsc();                                    \
157                                                                         \
158         if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {                 \
159                 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);   \
160                 uint32_t i;                                             \
161                                                                         \
162                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {         \
163                         uint64_t mask = f_pkt4_work(&pkts[i],           \
164                                 &entries[i], i, arg);                   \
165                         pkts_out_mask ^= mask << i;                     \
166                 }                                                       \
167                                                                         \
168                 for ( ; i < n_pkts; i++) {                              \
169                         uint64_t mask = f_pkt_work(pkts[i],             \
170                                 entries[i], i, arg);                    \
171                         pkts_out_mask ^= mask << i;                     \
172                 }                                                       \
173         } else                                                          \
174                 for ( ; pkts_in_mask; ) {                               \
175                         uint32_t pos = __builtin_ctzll(pkts_in_mask);   \
176                         uint64_t pkt_mask = 1LLU << pos;                \
177                         uint64_t mask = f_pkt_work(pkts[pos],           \
178                                 entries[pos], pos, arg);                \
179                                                                         \
180                         pkts_in_mask &= ~pkt_mask;                      \
181                         pkts_out_mask ^= mask << pos;                   \
182                 }                                                       \
183                                                                         \
184         rte_pipeline_ah_packet_drop(rte_p, pkts_out_mask ^ pkts_mask);  \
185                                                                         \
186         return 0;                                                       \
187 }
188
189 #define PIPELINE_CGNAPT_PORT_OUT_AH(f_ah, f_pkt_work, f_pkt4_work)      \
190 static int                                                              \
191 f_ah(                                                                   \
192         __rte_unused struct rte_pipeline *rte_p,                        \
193         struct rte_mbuf **pkt,                                          \
194         uint32_t *pkts_mask,                                            \
195         void *arg)                                                      \
196 {                                                                       \
197         f_pkt4_work(pkt, arg);                                          \
198         f_pkt_work(*pkt, arg);                                          \
199                                                                         \
200         int i = *pkts_mask; i++;                                        \
201         return 0;                                                       \
202 }
203
204 #define PIPELINE_CGNAPT_PORT_OUT_BAH(f_ah, f_pkt_work, f_pkt4_work)     \
205 static int                                                              \
206 f_ah(                                                                   \
207 __rte_unused struct rte_pipeline *rte_p,                                \
208 struct rte_mbuf **pkt,                                                  \
209 uint32_t *pkts_mask,                                                    \
210 void *arg)                                                              \
211 {                                                                       \
212         f_pkt4_work(pkt, arg);                                          \
213                                                                         \
214         f_pkt_work(*pkt, arg);                                          \
215                                                                         \
216         int i = *pkts_mask; i++;                                        \
217         return 0;                                                       \
218 }
219
220 #define PIPELINE_CGNAPT_KEY_PORT_IN_AH(f_ah, f_pkt_work, f_pkt4_work)   \
221 static int                                                              \
222 f_ah(                                                                   \
223         struct rte_pipeline *rte_p,                                     \
224         struct rte_mbuf **pkts,                                         \
225         uint32_t n_pkts,                                                \
226         void *arg)                                                      \
227 {                                                                       \
228         uint32_t i;                                                     \
229                                                                         \
230         if (CGNAPT_DEBUG > 1)                                           \
231                 printf("cgnapt_key hit fn: %"PRIu32"\n", n_pkts);       \
232                                                                         \
233         pkt_burst_cnt = 0;                                              \
234         for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4)                   \
235                 f_pkt4_work(&pkts[i], arg);                             \
236                                                                         \
237         for ( ; i < n_pkts; i++)                                        \
238                 f_pkt_work(pkts[i], arg);                               \
239                                                                         \
240                                                                         \
241         return 0;                                                       \
242 }                                                                       \
243
244
245 #define PIPELINE_CGNAPT_TABLE_AH_MISS(f_ah, f_pkt_work, f_pkt4_work)    \
246 static int                                                              \
247 f_ah(                                                                   \
248         struct rte_pipeline *rte_p,                                     \
249         struct rte_mbuf **pkts,                                         \
250         uint64_t pkts_mask,                                             \
251         struct rte_pipeline_table_entry **entries,                      \
252         void *arg)                                                      \
253 {                                                                       \
254         uint64_t pkts_in_mask = pkts_mask;                              \
255         uint64_t pkts_out_mask = pkts_mask;                             \
256         uint64_t time = rte_rdtsc();                                    \
257                                                                         \
258         if ((pkts_in_mask & (pkts_in_mask + 1)) == 0) {                 \
259                 uint64_t n_pkts = __builtin_popcountll(pkts_in_mask);   \
260                 uint32_t i;                                             \
261                                                                         \
262                 for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {         \
263                         uint64_t mask = f_pkt4_work(&pkts[i],           \
264                                 &entries[i], i, arg);                   \
265                         pkts_out_mask ^= mask << i;                     \
266                 }                                                       \
267                                                                         \
268                 for ( ; i < n_pkts; i++) {                              \
269                         uint64_t mask = f_pkt_work(pkts[i],             \
270                                 entries[i], i, arg);                    \
271                         pkts_out_mask ^= mask << i;                     \
272                 }                                                       \
273         } else                                                          \
274                 for ( ; pkts_in_mask; ) {                               \
275                         uint32_t pos = __builtin_ctzll(pkts_in_mask);   \
276                         uint64_t pkt_mask = 1LLU << pos;                \
277                         uint64_t mask = f_pkt_work(pkts[pos],           \
278                                 entries[pos], pos, arg);                \
279                                                                         \
280                         pkts_in_mask &= ~pkt_mask;                      \
281                         pkts_out_mask ^= mask << pos;                   \
282                 }                                                       \
283                                                                         \
284         rte_pipeline_ah_packet_drop(rte_p, pkts_out_mask ^ pkts_mask);  \
285                                                                         \
286         return 0;                                                       \
287 }
288
289 /* IPv4 offsets */
290 #define SRC_ADR_OFST_IP4 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_SRC_ADR_OFST)
291 #define DST_ADR_OFST_IP4 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_DST_ADR_OFST)
292 #define SRC_PRT_OFST_IP4_TCP (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_SIZE)
293 #define SRC_PRT_OFST_IP4_UDP SRC_PRT_OFST_IP4_TCP
294 #define DST_PRT_OFST_IP4_TCP (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_SIZE + 2)
295 #define DST_PRT_OFST_IP4_UDP DST_PRT_OFST_IP4_TCP
296 #define PROT_OFST_IP4 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_PROTOCOL_OFST)
297 #define IDEN_OFST_IP4_ICMP (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_SIZE + 4)
298 #define SEQN_OFST_IP4_ICMP (MBUF_HDR_ROOM + ETH_HDR_SIZE + IP_HDR_SIZE + 6)
299
300 /*NAT64*/
301
302 /* IPv6 offsets */
303 #define SRC_ADR_OFST_IP6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IPV6_HDR_SRC_ADR_OFST)
304 #define DST_ADR_OFST_IP6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IPV6_HDR_DST_ADR_OFST)
305 #define SRC_PRT_OFST_IP6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IPV6_HDR_SIZE)
306 #define DST_PRT_OFST_IP6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IPV6_HDR_SIZE + 2)
307 #define PROT_OFST_IP6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + IPV6_HDR_PROTOCOL_OFST)
308
309 /* After IPv6 to IPv4 conversion */
310 #define SRC_ADR_OFST_IP6t4 (20 + MBUF_HDR_ROOM + ETH_HDR_SIZE + \
311                                         IP_HDR_SRC_ADR_OFST)
312 #define DST_ADR_OFST_IP6t4 (20 + MBUF_HDR_ROOM + ETH_HDR_SIZE + \
313                                         IP_HDR_DST_ADR_OFST)
314 #define SRC_PRT_OFST_IP6t4 (20 + MBUF_HDR_ROOM + ETH_HDR_SIZE + \
315                                         IP_HDR_SIZE)
316 #define DST_PRT_OFST_IP6t4 (20 + MBUF_HDR_ROOM + ETH_HDR_SIZE + \
317                                         IP_HDR_SIZE + 2)
318 #define PROT_OFST_IP6t4 (20 + MBUF_HDR_ROOM + ETH_HDR_SIZE + \
319                                         IP_HDR_PROTOCOL_OFST)
320 #define ETH_OFST_IP6t4 (20 + MBUF_HDR_ROOM)
321
322 /* After IPv4 to IPv6 conversion */
323 #define DST_PRT_OFST_IP4t6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + \
324                                 IPV6_HDR_SIZE + 2 - 20)
325 #define DST_ADR_OFST_IP4t6 (MBUF_HDR_ROOM + ETH_HDR_SIZE + \
326                                 IPV6_HDR_DST_ADR_OFST - 20)
327
328 #define TRAFFIC_TYPE_IPV4  4
329 #define TRAFFIC_TYPE_IPV6  6
330
331 #define CGNAPT_MAX_PUB_IP 256
332
333
334 /**
335  * A structure defining public ip and associated client count.
336  */
337 struct public_ip {
338         uint32_t ip;
339         rte_atomic16_t count;   /* how many clients are using the public_ip */
340 } all_public_ip[CGNAPT_MAX_PUB_IP];
341
342 /**
343  * Command to dump number of clients using an IP address.
344  */
345 void print_num_ip_clients(void);
346
347 extern struct rte_hash *napt_common_table;
348 extern struct public_ip all_public_ip[CGNAPT_MAX_PUB_IP];
349
350 /**
351  * A structure defining pipeline_cgnapt - placeholder for all
352  * CGNAPT pipeline variables
353  *
354  *
355  */
356 struct pipeline_cgnapt {
357         struct pipeline p;
358         pipeline_msg_req_handler custom_handlers[PIPELINE_CGNAPT_MSG_REQS];
359
360         uint32_t n_flows;
361         uint32_t key_offset;
362         uint32_t key_size;
363         uint32_t hash_offset;
364
365         uint32_t n_entries;
366
367         /* Dynamic NAPT Start */
368         uint8_t is_static_cgnapt;
369         uint16_t max_port_per_client;
370         uint16_t max_clients_per_ip;
371
372         struct pub_ip_port_set *pub_ip_port_set;
373         uint8_t pub_ip_count;
374         struct pub_ip_range *pub_ip_range;
375         uint8_t pub_ip_range_count;
376
377         struct napt_port_alloc_elem *allocated_ports;
378         struct napt_port_alloc_elem *free_ports;
379         struct rte_ring *port_alloc_ring;
380
381         uint64_t *port_map;
382         uint16_t port_map_array_size;
383
384         uint64_t n_cgnapt_entry_deleted;
385         uint64_t n_cgnapt_entry_added;
386         uint64_t naptedPktCount;
387         uint64_t naptDroppedPktCount;
388
389         uint64_t inaptedPktCount;
390         uint64_t enaptedPktCount;
391         uint64_t receivedPktCount;
392         uint64_t missedPktCount;
393         uint64_t dynCgnaptCount;
394         uint64_t arpicmpPktCount;
395
396         uint64_t app_params_addr;
397         uint8_t pipeline_num;
398         uint8_t pkt_burst_cnt;
399         uint8_t hw_checksum_reqd;
400         uint8_t traffic_type;
401         uint8_t links_map[PIPELINE_MAX_PORT_IN];
402         uint8_t outport_id[PIPELINE_MAX_PORT_IN];
403
404         struct pipeline_cgnapt_entry_key
405                         cgnapt_dyn_ent_table[RTE_PORT_IN_BURST_SIZE_MAX];
406         uint32_t cgnapt_dyn_ent_index[RTE_PORT_IN_BURST_SIZE_MAX];
407
408         /* table lookup keys */
409         struct pipeline_cgnapt_entry_key keys[RTE_HASH_LOOKUP_BULK_MAX];
410         /* pointers to table lookup keys */
411         void *key_ptrs[RTE_HASH_LOOKUP_BULK_MAX];
412         /* table lookup results */
413         int32_t lkup_indx[RTE_HASH_LOOKUP_BULK_MAX];
414         /* entries used for pkts fwd */
415         struct rte_pipeline_table_entry *entries[RTE_HASH_LOOKUP_BULK_MAX];
416         uint64_t valid_packets; /* bitmap of valid packets to process */
417         uint64_t invalid_packets;/* bitmap of invalid packets to be dropped */
418
419         uint8_t vnf_set;        /* to identify as separate LB-CGNAPT set */
420
421         /* For internal debugging purpose */
422 #ifdef CGNAPT_TIMING_INST
423         uint64_t in_port_exit_timestamp;
424         uint64_t external_time_sum;
425         uint64_t internal_time_sum;
426         uint32_t time_measurements;
427         uint32_t max_time_mesurements;
428         uint8_t time_measurements_on;
429 #endif
430
431 #ifdef CGNAPT_DEBUGGING
432
433         uint32_t naptDebugCount;
434
435         uint64_t naptDroppedPktCount1;
436         uint64_t naptDroppedPktCount2;
437         uint64_t naptDroppedPktCount3;
438         uint64_t naptDroppedPktCount4;
439         uint64_t naptDroppedPktCount5;
440         uint64_t naptDroppedPktCount6;
441
442         uint64_t kpc1, kpc2;
443
444         uint64_t missedpktcount1;
445         uint64_t missedpktcount2;
446         uint64_t missedpktcount3;
447         uint64_t missedpktcount4;
448         uint64_t missedpktcount5;
449         uint64_t missedpktcount6;
450         uint64_t missedpktcount7;
451         uint64_t missedpktcount8;
452         uint64_t missedpktcount9;
453         uint64_t missedpktcount10;
454
455         uint64_t missedpktcount11;
456         uint64_t missedpktcount12;
457
458
459         uint64_t max_port_dec_err1;
460         uint64_t max_port_dec_err2;
461         uint64_t max_port_dec_err3;
462         uint64_t max_port_dec_success;
463
464         uint64_t pfb_err;
465         uint64_t pfb_ret;
466         uint64_t pfb_get;
467         uint64_t pfb_suc;
468         uint64_t gfp_suc;
469         uint64_t gfp_get;
470         uint64_t gfp_ret;
471         uint64_t gfp_err;
472 #endif
473 } __rte_cache_aligned;
474
475 /**
476  * A structure defining the CG-NAPT input port handler arg.
477  */
478 struct pipeline_cgnapt_in_port_h_arg {
479         struct pipeline_cgnapt *p;
480         uint8_t in_port_id;
481 };
482
483 enum {
484         CGNAPT_PRV_PORT_ID,
485         CGNAPT_PUB_PORT_ID,
486 };
487
488 uint16_t cgnapt_meta_offset;
489 uint8_t dual_stack_enable;
490 uint16_t dest_if_offset;
491 uint8_t nat_only_config_flag;
492 uint8_t CGNAPT_DEBUG;
493
494 #if (RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN)
495 /* x86 == little endian   */
496 /* network  == big endian */
497 #define CHECK_ENDIAN_16(x) rte_be_to_cpu_16(x)
498 #else
499 #define CHECK_ENDIAN_16(x) (x)
500 #endif
501 #define IP_VHL_DEF (0x40 | 0x05)
502 struct rte_mempool *cgnapt_icmp_pktmbuf_tx_pool;
503 struct rte_mbuf *cgnapt_icmp_pkt;
504 struct rte_pipeline *myP;
505 uint8_t icmp_pool_init;
506
507 #define MAX_NUM_LOCAL_MAC_ADDRESS 16
508
509 /***** NAT64 NSP declarations *****/
510 /**
511  * A structure defining nsp node.
512  */
513 struct cgnapt_nsp_node {
514         struct pipeline_cgnapt_nsp_t nsp;
515         struct cgnapt_nsp_node *next;
516 };
517
518 struct cgnapt_nsp_node *nsp_ll;
519
520 /***** Common Table declarations *****/
521 #define IP_VERSION_4 4
522 #define IP_VERSION_6 6
523 #define MAX_NAPT_ENTRIES 16777216       /* 0x1000000 */
524 #define NUM_NAPT_PORT_BULK_ALLOC 250
525
526
527 struct rte_hash *napt_common_table;
528 struct cgnapt_table_entry *napt_hash_tbl_entries;
529
530 /***** Multiple NAT IP declarations *****/
531
532 /**
533  * A structure defining public ip and associated port range set
534  */
535 struct pub_ip_port_set {
536         uint32_t ip;
537         uint16_t start_port;
538         uint16_t end_port;
539 };
540
541 /**
542  * A structure defining public ip range
543  */
544 struct pub_ip_range {
545         uint32_t start_ip;
546         uint32_t end_ip;
547 };
548
549 /***** Common Port Allocation declarations *****/
550
551 int create_napt_common_table(uint32_t nFlows);
552 struct rte_mempool *napt_port_pool;
553
554 #define MAX_CGNAPT_SETS 16
555
556 /**
557  * A structure defining a bulk port allocation element.
558  */
559 struct napt_port_alloc_elem {
560         uint32_t count;
561         uint32_t ip_addr[NUM_NAPT_PORT_BULK_ALLOC];
562         uint16_t ports[NUM_NAPT_PORT_BULK_ALLOC];
563 };
564
565 int napt_port_alloc_init(struct pipeline_cgnapt *p_nat);
566 void release_iport(uint16_t port, uint32_t public_ip,
567                          struct pipeline_cgnapt *p_nat);
568 int get_free_iport(struct pipeline_cgnapt *p_nat, uint32_t *public_ip);
569
570 /***************************** Function declarations *************************/
571
572 void
573 pkt4_work_cgnapt_ipv6_prv(struct rte_mbuf **pkts,
574                                 uint32_t in_pkt_num,
575                                 void *arg, struct pipeline_cgnapt *p_nat);
576 void
577 pkt_work_cgnapt_ipv6_prv(struct rte_mbuf *pkts,
578                          uint32_t in_pkt_num,
579                          void *arg, struct pipeline_cgnapt *p_nat);
580
581 void
582 pkt4_work_cgnapt_ipv6_pub(struct rte_mbuf **pkts,
583                                 uint32_t in_pkt_num,
584                                 void *arg, struct pipeline_cgnapt *p_nat);
585 void
586 pkt_work_cgnapt_ipv6_pub(struct rte_mbuf *pkt,
587                          uint32_t in_pkt_num,
588                          void *arg, struct pipeline_cgnapt *p_nat);
589
590 void
591 pkt4_work_cgnapt_ipv4_prv(struct rte_mbuf **pkts,
592                                 uint32_t in_pkt_num,
593                                 void *arg, struct pipeline_cgnapt *p_nat);
594
595 void
596 pkt_work_cgnapt_ipv4_prv(struct rte_mbuf **pkts,
597                          uint32_t in_pkt_num,
598                          void *arg, struct pipeline_cgnapt *p_nat);
599
600 void
601 pkt4_work_cgnapt_ipv4_pub(struct rte_mbuf **pkts,
602                                 uint32_t in_pkt_num,
603                                 void *arg, struct pipeline_cgnapt *p_nat);
604 void
605 pkt_work_cgnapt_ipv4_pub(struct rte_mbuf **pkts,
606                          uint32_t in_pkt_num,
607                          void *arg, struct pipeline_cgnapt *p_nat);
608
609 /* in port handler key functions */
610 void
611 pkt4_work_cgnapt_key_ipv4_prv(struct rte_mbuf **pkts,
612                                                 uint32_t pkt_num,
613                                                 void *arg, struct pipeline_cgnapt *p_nat);
614
615 void
616 pkt_work_cgnapt_key_ipv4_prv(struct rte_mbuf *pkt,
617                                          uint32_t pkt_num,
618                                          void *arg, struct pipeline_cgnapt *p_nat);
619
620 void
621 pkt4_work_cgnapt_key_ipv4_pub(struct rte_mbuf **pkts,
622                                                 uint32_t pkt_num,
623                                                 void *arg, struct pipeline_cgnapt *p_nat);
624
625 void
626 pkt_work_cgnapt_key_ipv4_pub(struct rte_mbuf *pkt,
627                                          uint32_t pkt_num,
628                                          void *arg, struct pipeline_cgnapt *p_nat);
629 void
630 pkt4_work_cgnapt_key_ipv6_pub(struct rte_mbuf **pkts,
631                                                 uint32_t pkt_num,
632                                                 void *arg, struct pipeline_cgnapt *p_nat);
633 void
634 pkt_work_cgnapt_key_ipv6_pub(struct rte_mbuf *pkts,
635                                          uint32_t pkt_num,
636                                          void *arg, struct pipeline_cgnapt *p_nat);
637 void
638 pkt4_work_cgnapt_key_ipv6_prv(struct rte_mbuf **pkts,
639                                                 uint32_t pkt_num,
640                                                 void *arg, struct pipeline_cgnapt *p_nat);
641 void
642 pkt_work_cgnapt_key_ipv6_prv(struct rte_mbuf *pkt,
643                                          uint32_t pkt_num,
644                                          void *arg, struct pipeline_cgnapt *p_nat);
645
646 void send_icmp_dest_unreachable_msg(void);
647 unsigned short cksum_calc(unsigned short *addr, int len);
648 void print_mbuf(const char *rx_tx, unsigned int portid, struct rte_mbuf *mbuf,
649                 unsigned int line);
650
651
652 /* Max port per client declarations */
653 /**
654  * A structure defining maximun ports per client
655  */
656 struct max_port_per_client {
657         uint32_t prv_ip;
658         uint32_t prv_phy_port;
659         uint8_t max_port_cnt;
660 };
661
662 /**
663  * A structure defining maximun ports per client key
664  */
665 struct max_port_per_client_key {
666         uint32_t prv_ip;
667         uint32_t prv_phy_port;
668 };
669
670 struct rte_hash *max_port_per_client_hash;
671 struct max_port_per_client *max_port_per_client_array;
672
673
674 int init_max_port_per_client(struct pipeline_cgnapt *p_nat);
675 int is_max_port_per_client_reached(uint32_t prv_ip_param,
676                                          uint32_t prv_phy_port_param,
677                                          struct pipeline_cgnapt *p_nat);
678 int increment_max_port_counter(uint32_t prv_ip_param,
679                                                  uint32_t prv_phy_port_param,
680                                                  struct pipeline_cgnapt *p_nat);
681 int decrement_max_port_counter(uint32_t prv_ip_param,
682                                                  uint32_t prv_phy_port_param,
683                                                  struct pipeline_cgnapt *p_nat);
684 int max_port_per_client_add_entry(uint32_t prv_ip_param,
685                                         uint32_t prv_phy_port_param,
686                                         struct pipeline_cgnapt *p_nat);
687 int max_port_per_client_del_entry(uint32_t prv_ip_param,
688                                         uint32_t prv_phy_port_param,
689                                         struct pipeline_cgnapt *p_nat);
690
691 /* Print functions */
692 void print_pkt(struct rte_mbuf *pkt);
693 void log_pkt(struct rte_mbuf *pkt);
694 void print_key(struct pipeline_cgnapt_entry_key *key);
695 void print_entry1(struct rte_pipeline_table_entry *entry);
696 void print_cgnapt_entry(struct cgnapt_table_entry *entry);
697 void my_print_entry(struct cgnapt_table_entry *ent);
698
699 /* CLI custom handler back-end helper functions */
700
701 void *pipeline_cgnapt_msg_req_custom_handler(
702         struct pipeline *p,
703         void *msg);
704
705 void *pipeline_cgnapt_msg_req_entry_add_handler(
706         struct pipeline *p,
707         void *msg);
708
709 void *pipeline_cgnapt_msg_req_entry_del_handler(
710         struct pipeline *p,
711         void *msg);
712
713 void *pipeline_cgnapt_msg_req_entry_sync_handler(
714         struct pipeline *p,
715         void *msg);
716
717 void *pipeline_cgnapt_msg_req_entry_dbg_handler(
718         struct pipeline *p,
719         void *msg);
720
721 void *pipeline_cgnapt_msg_req_entry_addm_handler(
722         struct pipeline *p,
723         void *msg);
724
725 void *pipeline_cgnapt_msg_req_ver_handler(
726         struct pipeline *p,
727         void *msg);
728
729 void *pipeline_cgnapt_msg_req_nsp_add_handler(
730         struct pipeline *p,
731         void *msg);
732
733 void *pipeline_cgnapt_msg_req_nsp_del_handler(
734         struct pipeline *p,
735         void *msg);
736 #ifdef PCP_ENABLE
737 extern void *pipeline_cgnapt_msg_req_pcp_handler(
738         struct pipeline *p,
739         void *msg);
740 #endif
741
742 int pipeline_cgnapt_msg_req_entry_addm_pair(
743         struct pipeline *p, void *msg,
744         uint32_t src_ip, uint16_t src_port,
745         uint32_t dest_ip, uint16_t dest_port,
746         uint16_t rx_port, uint32_t ttl,
747         uint8_t type, uint8_t src_ipv6[16]);
748
749 /* CGNAPT Functions */
750 uint64_t pkt_miss_cgnapt(
751         struct pipeline_cgnapt_entry_key *key,
752         struct rte_mbuf *pkt,
753         struct rte_pipeline_table_entry **table_entry,
754         uint64_t *pkts_mask,
755         uint32_t pkt_num,
756         void *arg);
757
758 struct cgnapt_table_entry *add_dynamic_cgnapt_entry(
759         struct pipeline *p,
760         struct pipeline_cgnapt_entry_key *key,
761         //#ifdef PCP_ENABLE
762         uint32_t timeout,
763         //#endif
764         uint8_t pkt_type,
765         uint8_t *src_addr,
766         uint8_t *err);
767
768 void calculate_hw_checksum(
769         struct rte_mbuf *pkt,
770         uint8_t ip_ver,
771         uint8_t protocol);
772
773 uint64_t nextPowerOf2(uint64_t n);
774 uint8_t local_dest_mac_present(uint8_t out_port);
775
776 enum PKT_TYPE {
777 PKT_TYPE_IPV4,
778 PKT_TYPE_IPV6,
779 PKT_TYPE_IPV6to4,
780 PKT_TYPE_IPV4to6,
781 };
782 void hw_checksum(struct rte_mbuf *pkt, enum PKT_TYPE ver);
783 void sw_checksum(struct rte_mbuf *pkt, enum PKT_TYPE ver);
784 int rte_get_pkt_ver(struct rte_mbuf *pkt);
785 void print_common_table(void);
786 #if CT_CGNAT
787 extern int add_dynamic_cgnapt_entry_alg(
788         struct pipeline *p,
789         struct pipeline_cgnapt_entry_key *key,
790         struct cgnapt_table_entry **entry_ptr1,
791         struct cgnapt_table_entry **entry_ptr2);
792 #endif
793 #endif