340311928567825b9eacfde0c1e3094bbbe6e60d
[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_MIX   0
329 #define TRAFFIC_TYPE_IPV4  4
330 #define TRAFFIC_TYPE_IPV6  6
331
332 #define CGNAPT_MAX_PUB_IP 256
333
334
335 /**
336  * A structure defining public ip and associated client count.
337  */
338 struct public_ip {
339         uint32_t ip;
340         rte_atomic16_t count;   /* how many clients are using the public_ip */
341 } all_public_ip[CGNAPT_MAX_PUB_IP];
342
343 /**
344  * Command to dump number of clients using an IP address.
345  */
346 void print_num_ip_clients(void);
347
348 extern struct rte_hash *napt_common_table;
349 extern struct public_ip all_public_ip[CGNAPT_MAX_PUB_IP];
350
351 /**
352  * A structure defining pipeline_cgnapt - placeholder for all
353  * CGNAPT pipeline variables
354  *
355  *
356  */
357 struct pipeline_cgnapt {
358         struct pipeline p;
359         pipeline_msg_req_handler custom_handlers[PIPELINE_CGNAPT_MSG_REQS];
360
361         uint32_t n_flows;
362         uint32_t key_offset;
363         uint32_t key_size;
364         uint32_t hash_offset;
365
366         uint32_t n_entries;
367
368         /* Dynamic NAPT Start */
369         uint8_t is_static_cgnapt;
370         uint16_t max_port_per_client;
371         uint16_t max_clients_per_ip;
372
373         struct pub_ip_port_set *pub_ip_port_set;
374         uint8_t pub_ip_count;
375         struct pub_ip_range *pub_ip_range;
376         uint8_t pub_ip_range_count;
377
378         struct napt_port_alloc_elem *allocated_ports;
379         struct napt_port_alloc_elem *free_ports;
380         struct rte_ring *port_alloc_ring;
381
382         uint64_t *port_map;
383         uint16_t port_map_array_size;
384
385         uint64_t n_cgnapt_entry_deleted;
386         uint64_t n_cgnapt_entry_added;
387         uint64_t naptedPktCount;
388         uint64_t naptDroppedPktCount;
389
390         uint64_t inaptedPktCount;
391         uint64_t enaptedPktCount;
392         uint64_t receivedPktCount;
393         uint64_t missedPktCount;
394         uint64_t dynCgnaptCount;
395         uint64_t arpicmpPktCount;
396
397         uint64_t app_params_addr;
398         uint8_t pipeline_num;
399         uint8_t pkt_burst_cnt;
400         uint8_t hw_checksum_reqd;
401         uint8_t traffic_type;
402         uint8_t links_map[PIPELINE_MAX_PORT_IN];
403         uint8_t outport_id[PIPELINE_MAX_PORT_IN];
404
405         struct pipeline_cgnapt_entry_key
406                         cgnapt_dyn_ent_table[RTE_PORT_IN_BURST_SIZE_MAX];
407         uint32_t cgnapt_dyn_ent_index[RTE_PORT_IN_BURST_SIZE_MAX];
408
409         /* table lookup keys */
410         struct pipeline_cgnapt_entry_key keys[RTE_HASH_LOOKUP_BULK_MAX];
411         /* pointers to table lookup keys */
412         void *key_ptrs[RTE_HASH_LOOKUP_BULK_MAX];
413         /* table lookup results */
414         int32_t lkup_indx[RTE_HASH_LOOKUP_BULK_MAX];
415         /* entries used for pkts fwd */
416         struct rte_pipeline_table_entry *entries[RTE_HASH_LOOKUP_BULK_MAX];
417         uint64_t valid_packets; /* bitmap of valid packets to process */
418         uint64_t invalid_packets;/* bitmap of invalid packets to be dropped */
419
420         uint8_t vnf_set;        /* to identify as separate LB-CGNAPT set */
421
422         /* Local ARP & ND Tables */
423         struct lib_arp_route_table_entry
424                 local_lib_arp_route_table[MAX_ARP_RT_ENTRY];
425         uint8_t local_lib_arp_route_ent_cnt;
426         struct lib_nd_route_table_entry
427                 local_lib_nd_route_table[MAX_ND_RT_ENTRY];
428         uint8_t local_lib_nd_route_ent_cnt;
429
430         /* For internal debugging purpose */
431 #ifdef CGNAPT_TIMING_INST
432         uint64_t in_port_exit_timestamp;
433         uint64_t external_time_sum;
434         uint64_t internal_time_sum;
435         uint32_t time_measurements;
436         uint32_t max_time_mesurements;
437         uint8_t time_measurements_on;
438 #endif
439
440 #ifdef CGNAPT_DEBUGGING
441
442         uint32_t naptDebugCount;
443
444         uint64_t naptDroppedPktCount1;
445         uint64_t naptDroppedPktCount2;
446         uint64_t naptDroppedPktCount3;
447         uint64_t naptDroppedPktCount4;
448         uint64_t naptDroppedPktCount5;
449         uint64_t naptDroppedPktCount6;
450
451         uint64_t kpc1, kpc2;
452
453         uint64_t missedpktcount1;
454         uint64_t missedpktcount2;
455         uint64_t missedpktcount3;
456         uint64_t missedpktcount4;
457         uint64_t missedpktcount5;
458         uint64_t missedpktcount6;
459         uint64_t missedpktcount7;
460         uint64_t missedpktcount8;
461         uint64_t missedpktcount9;
462         uint64_t missedpktcount10;
463
464         uint64_t missedpktcount11;
465         uint64_t missedpktcount12;
466
467
468         uint64_t max_port_dec_err1;
469         uint64_t max_port_dec_err2;
470         uint64_t max_port_dec_err3;
471         uint64_t max_port_dec_success;
472
473         uint64_t pfb_err;
474         uint64_t pfb_ret;
475         uint64_t pfb_get;
476         uint64_t pfb_suc;
477         uint64_t gfp_suc;
478         uint64_t gfp_get;
479         uint64_t gfp_ret;
480         uint64_t gfp_err;
481 #endif
482 } __rte_cache_aligned;
483
484 /**
485  * A structure defining the CG-NAPT input port handler arg.
486  */
487 struct pipeline_cgnapt_in_port_h_arg {
488         struct pipeline_cgnapt *p;
489         uint8_t in_port_id;
490 };
491
492 enum {
493         CGNAPT_PRV_PORT_ID,
494         CGNAPT_PUB_PORT_ID,
495 };
496
497 uint16_t cgnapt_meta_offset;
498 uint8_t dual_stack_enable;
499 uint16_t dest_if_offset;
500 uint8_t nat_only_config_flag;
501 uint8_t CGNAPT_DEBUG;
502
503 #if (RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN)
504 /* x86 == little endian   */
505 /* network  == big endian */
506 #define CHECK_ENDIAN_16(x) rte_be_to_cpu_16(x)
507 #else
508 #define CHECK_ENDIAN_16(x) (x)
509 #endif
510 #define IP_VHL_DEF (0x40 | 0x05)
511 struct rte_mempool *cgnapt_icmp_pktmbuf_tx_pool;
512 struct rte_mbuf *cgnapt_icmp_pkt;
513 struct rte_pipeline *myP;
514 uint8_t icmp_pool_init;
515
516 #define MAX_NUM_LOCAL_MAC_ADDRESS 16
517
518 /***** NAT64 NSP declarations *****/
519 /**
520  * A structure defining nsp node.
521  */
522 struct cgnapt_nsp_node {
523         struct pipeline_cgnapt_nsp_t nsp;
524         struct cgnapt_nsp_node *next;
525 };
526
527 struct cgnapt_nsp_node *nsp_ll;
528
529 /***** Common Table declarations *****/
530 #define IP_VERSION_4 4
531 #define IP_VERSION_6 6
532 #define MAX_NAPT_ENTRIES 16777216       /* 0x1000000 */
533 #define NUM_NAPT_PORT_BULK_ALLOC 250
534
535
536 struct rte_hash *napt_common_table;
537 struct cgnapt_table_entry *napt_hash_tbl_entries;
538
539 /***** Multiple NAT IP declarations *****/
540
541 /**
542  * A structure defining public ip and associated port range set
543  */
544 struct pub_ip_port_set {
545         uint32_t ip;
546         uint16_t start_port;
547         uint16_t end_port;
548 };
549
550 /**
551  * A structure defining public ip range
552  */
553 struct pub_ip_range {
554         uint32_t start_ip;
555         uint32_t end_ip;
556 };
557
558 /***** Common Port Allocation declarations *****/
559
560 int create_napt_common_table(uint32_t nFlows);
561 struct rte_mempool *napt_port_pool;
562
563 #define MAX_CGNAPT_SETS 16
564
565 /**
566  * A structure defining a bulk port allocation element.
567  */
568 struct napt_port_alloc_elem {
569         uint32_t count;
570         uint32_t ip_addr[NUM_NAPT_PORT_BULK_ALLOC];
571         uint16_t ports[NUM_NAPT_PORT_BULK_ALLOC];
572 };
573
574 int napt_port_alloc_init(struct pipeline_cgnapt *p_nat);
575 void release_iport(uint16_t port, uint32_t public_ip,
576                          struct pipeline_cgnapt *p_nat);
577 int get_free_iport(struct pipeline_cgnapt *p_nat, uint32_t *public_ip);
578
579 /***************************** Function declarations *************************/
580
581 void
582 pkt4_work_cgnapt_ipv6_prv(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_prv(struct rte_mbuf *pkts,
587                          uint32_t in_pkt_num,
588                          void *arg, struct pipeline_cgnapt *p_nat);
589
590 void
591 pkt4_work_cgnapt_ipv6_pub(struct rte_mbuf **pkts,
592                                 uint32_t in_pkt_num,
593                                 void *arg, struct pipeline_cgnapt *p_nat);
594 void
595 pkt_work_cgnapt_ipv6_pub(struct rte_mbuf *pkt,
596                          uint32_t in_pkt_num,
597                          void *arg, struct pipeline_cgnapt *p_nat);
598
599 void
600 pkt4_work_cgnapt_ipv4_prv(struct rte_mbuf **pkts,
601                                 uint32_t in_pkt_num,
602                                 void *arg, struct pipeline_cgnapt *p_nat);
603
604 void
605 pkt_work_cgnapt_ipv4_prv(struct rte_mbuf **pkts,
606                          uint32_t in_pkt_num,
607                          void *arg, struct pipeline_cgnapt *p_nat);
608
609 void
610 pkt4_work_cgnapt_ipv4_pub(struct rte_mbuf **pkts,
611                                 uint32_t in_pkt_num,
612                                 void *arg, struct pipeline_cgnapt *p_nat);
613 void
614 pkt_work_cgnapt_ipv4_pub(struct rte_mbuf **pkts,
615                          uint32_t in_pkt_num,
616                          void *arg, struct pipeline_cgnapt *p_nat);
617
618 /* in port handler key functions */
619 void
620 pkt4_work_cgnapt_key_ipv4_prv(struct rte_mbuf **pkts,
621                                                 uint32_t pkt_num,
622                                                 void *arg, struct pipeline_cgnapt *p_nat);
623
624 void
625 pkt_work_cgnapt_key_ipv4_prv(struct rte_mbuf *pkt,
626                                          uint32_t pkt_num,
627                                          void *arg, struct pipeline_cgnapt *p_nat);
628
629 void
630 pkt4_work_cgnapt_key_ipv4_pub(struct rte_mbuf **pkts,
631                                                 uint32_t pkt_num,
632                                                 void *arg, struct pipeline_cgnapt *p_nat);
633
634 void
635 pkt_work_cgnapt_key_ipv4_pub(struct rte_mbuf *pkt,
636                                          uint32_t pkt_num,
637                                          void *arg, struct pipeline_cgnapt *p_nat);
638 void
639 pkt4_work_cgnapt_key_ipv6_pub(struct rte_mbuf **pkts,
640                                                 uint32_t pkt_num,
641                                                 void *arg, struct pipeline_cgnapt *p_nat);
642 void
643 pkt_work_cgnapt_key_ipv6_pub(struct rte_mbuf *pkts,
644                                          uint32_t pkt_num,
645                                          void *arg, struct pipeline_cgnapt *p_nat);
646 void
647 pkt4_work_cgnapt_key_ipv6_prv(struct rte_mbuf **pkts,
648                                                 uint32_t pkt_num,
649                                                 void *arg, struct pipeline_cgnapt *p_nat);
650 void
651 pkt_work_cgnapt_key_ipv6_prv(struct rte_mbuf *pkt,
652                                          uint32_t pkt_num,
653                                          void *arg, struct pipeline_cgnapt *p_nat);
654
655 void send_icmp_dest_unreachable_msg(void);
656 unsigned short cksum_calc(unsigned short *addr, int len);
657 void print_mbuf(const char *rx_tx, unsigned int portid, struct rte_mbuf *mbuf,
658                 unsigned int line);
659
660
661 /* Max port per client declarations */
662 /**
663  * A structure defining maximun ports per client
664  */
665 struct max_port_per_client {
666         uint32_t prv_ip;
667         uint32_t prv_phy_port;
668         uint8_t max_port_cnt;
669 };
670
671 /**
672  * A structure defining maximun ports per client key
673  */
674 struct max_port_per_client_key {
675         uint32_t prv_ip;
676         uint32_t prv_phy_port;
677 };
678
679 struct rte_hash *max_port_per_client_hash;
680 struct max_port_per_client *max_port_per_client_array;
681
682
683 int init_max_port_per_client(struct pipeline_cgnapt *p_nat);
684 int is_max_port_per_client_reached(uint32_t prv_ip_param,
685                                          uint32_t prv_phy_port_param,
686                                          struct pipeline_cgnapt *p_nat);
687 int increment_max_port_counter(uint32_t prv_ip_param,
688                                                  uint32_t prv_phy_port_param,
689                                                  struct pipeline_cgnapt *p_nat);
690 int decrement_max_port_counter(uint32_t prv_ip_param,
691                                                  uint32_t prv_phy_port_param,
692                                                  struct pipeline_cgnapt *p_nat);
693 int max_port_per_client_add_entry(uint32_t prv_ip_param,
694                                         uint32_t prv_phy_port_param,
695                                         struct pipeline_cgnapt *p_nat);
696 int max_port_per_client_del_entry(uint32_t prv_ip_param,
697                                         uint32_t prv_phy_port_param,
698                                         struct pipeline_cgnapt *p_nat);
699
700 /* Print functions */
701 void print_pkt(struct rte_mbuf *pkt);
702 void log_pkt(struct rte_mbuf *pkt);
703 void print_key(struct pipeline_cgnapt_entry_key *key);
704 void print_entry1(struct rte_pipeline_table_entry *entry);
705 void print_cgnapt_entry(struct cgnapt_table_entry *entry);
706 void my_print_entry(struct cgnapt_table_entry *ent);
707
708 /* CLI custom handler back-end helper functions */
709
710 void *pipeline_cgnapt_msg_req_custom_handler(
711         struct pipeline *p,
712         void *msg);
713
714 void *pipeline_cgnapt_msg_req_entry_add_handler(
715         struct pipeline *p,
716         void *msg);
717
718 void *pipeline_cgnapt_msg_req_entry_del_handler(
719         struct pipeline *p,
720         void *msg);
721
722 void *pipeline_cgnapt_msg_req_entry_sync_handler(
723         struct pipeline *p,
724         void *msg);
725
726 void *pipeline_cgnapt_msg_req_entry_dbg_handler(
727         struct pipeline *p,
728         void *msg);
729
730 void *pipeline_cgnapt_msg_req_entry_addm_handler(
731         struct pipeline *p,
732         void *msg);
733
734 void *pipeline_cgnapt_msg_req_ver_handler(
735         struct pipeline *p,
736         void *msg);
737
738 void *pipeline_cgnapt_msg_req_nsp_add_handler(
739         struct pipeline *p,
740         void *msg);
741
742 void *pipeline_cgnapt_msg_req_nsp_del_handler(
743         struct pipeline *p,
744         void *msg);
745 #ifdef PCP_ENABLE
746 extern void *pipeline_cgnapt_msg_req_pcp_handler(
747         struct pipeline *p,
748         void *msg);
749 #endif
750
751 int pipeline_cgnapt_msg_req_entry_addm_pair(
752         struct pipeline *p, void *msg,
753         uint32_t src_ip, uint16_t src_port,
754         uint32_t dest_ip, uint16_t dest_port,
755         uint16_t rx_port, uint32_t ttl,
756         uint8_t type, uint8_t src_ipv6[16]);
757
758 /* CGNAPT Functions */
759 uint64_t pkt_miss_cgnapt(
760         struct pipeline_cgnapt_entry_key *key,
761         struct rte_mbuf *pkt,
762         struct rte_pipeline_table_entry **table_entry,
763         uint64_t *pkts_mask,
764         uint32_t pkt_num,
765         void *arg);
766
767 struct cgnapt_table_entry *add_dynamic_cgnapt_entry(
768         struct pipeline *p,
769         struct pipeline_cgnapt_entry_key *key,
770         //#ifdef PCP_ENABLE
771         uint32_t timeout,
772         //#endif
773         uint8_t pkt_type,
774         uint8_t *src_addr,
775         uint8_t *err);
776
777 void calculate_hw_checksum(
778         struct rte_mbuf *pkt,
779         uint8_t ip_ver,
780         uint8_t protocol);
781
782 uint64_t nextPowerOf2(uint64_t n);
783 struct ether_addr *get_local_link_hw_addr(uint8_t out_port);
784 uint8_t local_dest_mac_present(uint8_t out_port);
785
786 enum PKT_TYPE {
787 PKT_TYPE_IPV4,
788 PKT_TYPE_IPV6,
789 PKT_TYPE_IPV6to4,
790 PKT_TYPE_IPV4to6,
791 };
792 void hw_checksum(struct rte_mbuf *pkt, enum PKT_TYPE ver);
793 void sw_checksum(struct rte_mbuf *pkt, enum PKT_TYPE ver);
794 int rte_get_pkt_ver(struct rte_mbuf *pkt);
795 void print_common_table(void);
796 #if CT_CGNAT
797 extern int add_dynamic_cgnapt_entry_alg(
798         struct pipeline *p,
799         struct pipeline_cgnapt_entry_key *key,
800         struct cgnapt_table_entry **entry_ptr1,
801         struct cgnapt_table_entry **entry_ptr2);
802 #endif
803 #endif