943d0fc72288a9da54bc8da35bf3c038590d2a85
[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         /* Local ARP & ND Tables */
422         struct lib_arp_route_table_entry
423                 local_lib_arp_route_table[MAX_ARP_RT_ENTRY];
424         uint8_t local_lib_arp_route_ent_cnt;
425         struct lib_nd_route_table_entry
426                 local_lib_nd_route_table[MAX_ND_RT_ENTRY];
427         uint8_t local_lib_nd_route_ent_cnt;
428
429         /* For internal debugging purpose */
430 #ifdef CGNAPT_TIMING_INST
431         uint64_t in_port_exit_timestamp;
432         uint64_t external_time_sum;
433         uint64_t internal_time_sum;
434         uint32_t time_measurements;
435         uint32_t max_time_mesurements;
436         uint8_t time_measurements_on;
437 #endif
438
439 #ifdef CGNAPT_DEBUGGING
440
441         uint32_t naptDebugCount;
442
443         uint64_t naptDroppedPktCount1;
444         uint64_t naptDroppedPktCount2;
445         uint64_t naptDroppedPktCount3;
446         uint64_t naptDroppedPktCount4;
447         uint64_t naptDroppedPktCount5;
448         uint64_t naptDroppedPktCount6;
449
450         uint64_t kpc1, kpc2;
451
452         uint64_t missedpktcount1;
453         uint64_t missedpktcount2;
454         uint64_t missedpktcount3;
455         uint64_t missedpktcount4;
456         uint64_t missedpktcount5;
457         uint64_t missedpktcount6;
458         uint64_t missedpktcount7;
459         uint64_t missedpktcount8;
460         uint64_t missedpktcount9;
461         uint64_t missedpktcount10;
462
463         uint64_t missedpktcount11;
464         uint64_t missedpktcount12;
465
466
467         uint64_t max_port_dec_err1;
468         uint64_t max_port_dec_err2;
469         uint64_t max_port_dec_err3;
470         uint64_t max_port_dec_success;
471
472         uint64_t pfb_err;
473         uint64_t pfb_ret;
474         uint64_t pfb_get;
475         uint64_t pfb_suc;
476         uint64_t gfp_suc;
477         uint64_t gfp_get;
478         uint64_t gfp_ret;
479         uint64_t gfp_err;
480 #endif
481 } __rte_cache_aligned;
482
483 /**
484  * A structure defining the CG-NAPT input port handler arg.
485  */
486 struct pipeline_cgnapt_in_port_h_arg {
487         struct pipeline_cgnapt *p;
488         uint8_t in_port_id;
489 };
490
491 enum {
492         CGNAPT_PRV_PORT_ID,
493         CGNAPT_PUB_PORT_ID,
494 };
495
496 uint16_t cgnapt_meta_offset;
497 uint8_t dual_stack_enable;
498 uint16_t dest_if_offset;
499 uint8_t nat_only_config_flag;
500 uint8_t CGNAPT_DEBUG;
501
502 #if (RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN)
503 /* x86 == little endian   */
504 /* network  == big endian */
505 #define CHECK_ENDIAN_16(x) rte_be_to_cpu_16(x)
506 #else
507 #define CHECK_ENDIAN_16(x) (x)
508 #endif
509 #define IP_VHL_DEF (0x40 | 0x05)
510 struct rte_mempool *cgnapt_icmp_pktmbuf_tx_pool;
511 struct rte_mbuf *cgnapt_icmp_pkt;
512 struct rte_pipeline *myP;
513 uint8_t icmp_pool_init;
514
515 #define MAX_NUM_LOCAL_MAC_ADDRESS 16
516
517 /***** NAT64 NSP declarations *****/
518 /**
519  * A structure defining nsp node.
520  */
521 struct cgnapt_nsp_node {
522         struct pipeline_cgnapt_nsp_t nsp;
523         struct cgnapt_nsp_node *next;
524 };
525
526 struct cgnapt_nsp_node *nsp_ll;
527
528 /***** Common Table declarations *****/
529 #define IP_VERSION_4 4
530 #define IP_VERSION_6 6
531 #define MAX_NAPT_ENTRIES 16777216       /* 0x1000000 */
532 #define NUM_NAPT_PORT_BULK_ALLOC 250
533
534
535 struct rte_hash *napt_common_table;
536 struct cgnapt_table_entry *napt_hash_tbl_entries;
537
538 /***** Multiple NAT IP declarations *****/
539
540 /**
541  * A structure defining public ip and associated port range set
542  */
543 struct pub_ip_port_set {
544         uint32_t ip;
545         uint16_t start_port;
546         uint16_t end_port;
547 };
548
549 /**
550  * A structure defining public ip range
551  */
552 struct pub_ip_range {
553         uint32_t start_ip;
554         uint32_t end_ip;
555 };
556
557 /***** Common Port Allocation declarations *****/
558
559 int create_napt_common_table(uint32_t nFlows);
560 struct rte_mempool *napt_port_pool;
561
562 #define MAX_CGNAPT_SETS 16
563
564 /**
565  * A structure defining a bulk port allocation element.
566  */
567 struct napt_port_alloc_elem {
568         uint32_t count;
569         uint32_t ip_addr[NUM_NAPT_PORT_BULK_ALLOC];
570         uint16_t ports[NUM_NAPT_PORT_BULK_ALLOC];
571 };
572
573 int napt_port_alloc_init(struct pipeline_cgnapt *p_nat);
574 void release_iport(uint16_t port, uint32_t public_ip,
575                          struct pipeline_cgnapt *p_nat);
576 int get_free_iport(struct pipeline_cgnapt *p_nat, uint32_t *public_ip);
577
578 /***************************** Function declarations *************************/
579
580 void
581 pkt4_work_cgnapt_ipv6_prv(struct rte_mbuf **pkts,
582                                 uint32_t in_pkt_num,
583                                 void *arg, struct pipeline_cgnapt *p_nat);
584 void
585 pkt_work_cgnapt_ipv6_prv(struct rte_mbuf *pkts,
586                          uint32_t in_pkt_num,
587                          void *arg, struct pipeline_cgnapt *p_nat);
588
589 void
590 pkt4_work_cgnapt_ipv6_pub(struct rte_mbuf **pkts,
591                                 uint32_t in_pkt_num,
592                                 void *arg, struct pipeline_cgnapt *p_nat);
593 void
594 pkt_work_cgnapt_ipv6_pub(struct rte_mbuf *pkt,
595                          uint32_t in_pkt_num,
596                          void *arg, struct pipeline_cgnapt *p_nat);
597
598 void
599 pkt4_work_cgnapt_ipv4_prv(struct rte_mbuf **pkts,
600                                 uint32_t in_pkt_num,
601                                 void *arg, struct pipeline_cgnapt *p_nat);
602
603 void
604 pkt_work_cgnapt_ipv4_prv(struct rte_mbuf **pkts,
605                          uint32_t in_pkt_num,
606                          void *arg, struct pipeline_cgnapt *p_nat);
607
608 void
609 pkt4_work_cgnapt_ipv4_pub(struct rte_mbuf **pkts,
610                                 uint32_t in_pkt_num,
611                                 void *arg, struct pipeline_cgnapt *p_nat);
612 void
613 pkt_work_cgnapt_ipv4_pub(struct rte_mbuf **pkts,
614                          uint32_t in_pkt_num,
615                          void *arg, struct pipeline_cgnapt *p_nat);
616
617 /* in port handler key functions */
618 void
619 pkt4_work_cgnapt_key_ipv4_prv(struct rte_mbuf **pkts,
620                                                 uint32_t pkt_num,
621                                                 void *arg, struct pipeline_cgnapt *p_nat);
622
623 void
624 pkt_work_cgnapt_key_ipv4_prv(struct rte_mbuf *pkt,
625                                          uint32_t pkt_num,
626                                          void *arg, struct pipeline_cgnapt *p_nat);
627
628 void
629 pkt4_work_cgnapt_key_ipv4_pub(struct rte_mbuf **pkts,
630                                                 uint32_t pkt_num,
631                                                 void *arg, struct pipeline_cgnapt *p_nat);
632
633 void
634 pkt_work_cgnapt_key_ipv4_pub(struct rte_mbuf *pkt,
635                                          uint32_t pkt_num,
636                                          void *arg, struct pipeline_cgnapt *p_nat);
637 void
638 pkt4_work_cgnapt_key_ipv6_pub(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_pub(struct rte_mbuf *pkts,
643                                          uint32_t pkt_num,
644                                          void *arg, struct pipeline_cgnapt *p_nat);
645 void
646 pkt4_work_cgnapt_key_ipv6_prv(struct rte_mbuf **pkts,
647                                                 uint32_t pkt_num,
648                                                 void *arg, struct pipeline_cgnapt *p_nat);
649 void
650 pkt_work_cgnapt_key_ipv6_prv(struct rte_mbuf *pkt,
651                                          uint32_t pkt_num,
652                                          void *arg, struct pipeline_cgnapt *p_nat);
653
654 void send_icmp_dest_unreachable_msg(void);
655 unsigned short cksum_calc(unsigned short *addr, int len);
656 void print_mbuf(const char *rx_tx, unsigned int portid, struct rte_mbuf *mbuf,
657                 unsigned int line);
658
659
660 /* Max port per client declarations */
661 /**
662  * A structure defining maximun ports per client
663  */
664 struct max_port_per_client {
665         uint32_t prv_ip;
666         uint32_t prv_phy_port;
667         uint8_t max_port_cnt;
668 };
669
670 /**
671  * A structure defining maximun ports per client key
672  */
673 struct max_port_per_client_key {
674         uint32_t prv_ip;
675         uint32_t prv_phy_port;
676 };
677
678 struct rte_hash *max_port_per_client_hash;
679 struct max_port_per_client *max_port_per_client_array;
680
681
682 int init_max_port_per_client(struct pipeline_cgnapt *p_nat);
683 int is_max_port_per_client_reached(uint32_t prv_ip_param,
684                                          uint32_t prv_phy_port_param,
685                                          struct pipeline_cgnapt *p_nat);
686 int increment_max_port_counter(uint32_t prv_ip_param,
687                                                  uint32_t prv_phy_port_param,
688                                                  struct pipeline_cgnapt *p_nat);
689 int decrement_max_port_counter(uint32_t prv_ip_param,
690                                                  uint32_t prv_phy_port_param,
691                                                  struct pipeline_cgnapt *p_nat);
692 int max_port_per_client_add_entry(uint32_t prv_ip_param,
693                                         uint32_t prv_phy_port_param,
694                                         struct pipeline_cgnapt *p_nat);
695 int max_port_per_client_del_entry(uint32_t prv_ip_param,
696                                         uint32_t prv_phy_port_param,
697                                         struct pipeline_cgnapt *p_nat);
698
699 /* Print functions */
700 void print_pkt(struct rte_mbuf *pkt);
701 void log_pkt(struct rte_mbuf *pkt);
702 void print_key(struct pipeline_cgnapt_entry_key *key);
703 void print_entry1(struct rte_pipeline_table_entry *entry);
704 void print_cgnapt_entry(struct cgnapt_table_entry *entry);
705 void my_print_entry(struct cgnapt_table_entry *ent);
706
707 /* CLI custom handler back-end helper functions */
708
709 void *pipeline_cgnapt_msg_req_custom_handler(
710         struct pipeline *p,
711         void *msg);
712
713 void *pipeline_cgnapt_msg_req_entry_add_handler(
714         struct pipeline *p,
715         void *msg);
716
717 void *pipeline_cgnapt_msg_req_entry_del_handler(
718         struct pipeline *p,
719         void *msg);
720
721 void *pipeline_cgnapt_msg_req_entry_sync_handler(
722         struct pipeline *p,
723         void *msg);
724
725 void *pipeline_cgnapt_msg_req_entry_dbg_handler(
726         struct pipeline *p,
727         void *msg);
728
729 void *pipeline_cgnapt_msg_req_entry_addm_handler(
730         struct pipeline *p,
731         void *msg);
732
733 void *pipeline_cgnapt_msg_req_ver_handler(
734         struct pipeline *p,
735         void *msg);
736
737 void *pipeline_cgnapt_msg_req_nsp_add_handler(
738         struct pipeline *p,
739         void *msg);
740
741 void *pipeline_cgnapt_msg_req_nsp_del_handler(
742         struct pipeline *p,
743         void *msg);
744 #ifdef PCP_ENABLE
745 extern void *pipeline_cgnapt_msg_req_pcp_handler(
746         struct pipeline *p,
747         void *msg);
748 #endif
749
750 int pipeline_cgnapt_msg_req_entry_addm_pair(
751         struct pipeline *p, void *msg,
752         uint32_t src_ip, uint16_t src_port,
753         uint32_t dest_ip, uint16_t dest_port,
754         uint16_t rx_port, uint32_t ttl,
755         uint8_t type, uint8_t src_ipv6[16]);
756
757 /* CGNAPT Functions */
758 uint64_t pkt_miss_cgnapt(
759         struct pipeline_cgnapt_entry_key *key,
760         struct rte_mbuf *pkt,
761         struct rte_pipeline_table_entry **table_entry,
762         uint64_t *pkts_mask,
763         uint32_t pkt_num,
764         void *arg);
765
766 struct cgnapt_table_entry *add_dynamic_cgnapt_entry(
767         struct pipeline *p,
768         struct pipeline_cgnapt_entry_key *key,
769         //#ifdef PCP_ENABLE
770         uint32_t timeout,
771         //#endif
772         uint8_t pkt_type,
773         uint8_t *src_addr,
774         uint8_t *err);
775
776 void calculate_hw_checksum(
777         struct rte_mbuf *pkt,
778         uint8_t ip_ver,
779         uint8_t protocol);
780
781 uint64_t nextPowerOf2(uint64_t n);
782 uint8_t local_dest_mac_present(uint8_t out_port);
783
784 enum PKT_TYPE {
785 PKT_TYPE_IPV4,
786 PKT_TYPE_IPV6,
787 PKT_TYPE_IPV6to4,
788 PKT_TYPE_IPV4to6,
789 };
790 void hw_checksum(struct rte_mbuf *pkt, enum PKT_TYPE ver);
791 void sw_checksum(struct rte_mbuf *pkt, enum PKT_TYPE ver);
792 int rte_get_pkt_ver(struct rte_mbuf *pkt);
793 void print_common_table(void);
794 #if CT_CGNAT
795 extern int add_dynamic_cgnapt_entry_alg(
796         struct pipeline *p,
797         struct pipeline_cgnapt_entry_key *key,
798         struct cgnapt_table_entry **entry_ptr1,
799         struct cgnapt_table_entry **entry_ptr2);
800 #endif
801 #endif