2 * Copyright (c) 2021 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef included_pnat_h
17 #define included_pnat_h
20 #include <vnet/ip/ip4_packet.h>
21 #include <vppinfra/bihash_16_8.h>
23 #define PNAT_FLOW_HASH_BUCKETS 256
25 /* Definitions from pnat.api */
26 #include <pnat/pnat.api_types.h>
27 typedef vl_api_pnat_5tuple_t pnat_5tuple_t;
28 typedef vl_api_pnat_mask_t pnat_mask_t;
29 typedef vl_api_pnat_attachment_point_t pnat_attachment_point_t;
31 /* Rewrite instructions */
33 PNAT_INSTR_NONE = 1 << 0,
34 PNAT_INSTR_SOURCE_ADDRESS = 1 << 1,
35 PNAT_INSTR_SOURCE_PORT = 1 << 2,
36 PNAT_INSTR_DESTINATION_ADDRESS = 1 << 3,
37 PNAT_INSTR_DESTINATION_PORT = 1 << 4,
38 } pnat_instructions_t;
44 /* Session cache entries */
46 /* What to translate to */
47 pnat_instructions_t instructions;
49 /* Stored in network byte order */
50 ip4_address_t post_sa;
51 ip4_address_t post_da;
55 /* Used for trace/show commands */
57 pnat_5tuple_t rewrite;
60 /* Interface object */
63 pnat_mask_t lookup_mask[PNAT_ATTACHMENT_POINT_MAX];
64 pnat_mask_fast_t lookup_mask_fast[PNAT_ATTACHMENT_POINT_MAX];
66 /* Feature chain enabled on interface */
67 bool enabled[PNAT_ATTACHMENT_POINT_MAX];
76 clib_bihash_16_8_t flowhash; /* Bi-directional */
79 pnat_interface_t *interfaces;
80 u32 *interface_by_sw_if_index;
82 /* Translations pool */
83 pnat_translation_t *translations;
87 extern pnat_main_t pnat_main;
89 pnat_interface_t *pnat_interface_by_sw_if_index(u32 sw_if_index);
91 /* Packet trace information */
95 pnat_5tuple_t rewrite;
98 int pnat_binding_add(pnat_5tuple_t *match, pnat_5tuple_t *rewrite,
100 int pnat_binding_del(u32 binding_index);
101 int pnat_binding_attach(u32 sw_if_index, pnat_attachment_point_t attachment,
103 int pnat_binding_detach(u32 sw_if_index, pnat_attachment_point_t attachment,
105 u32 pnat_flow_lookup(u32 sw_if_index, pnat_attachment_point_t attachment,
106 pnat_5tuple_t *match);
109 pnat_calc_key(u32 sw_if_index, pnat_attachment_point_t attachment,
110 ip4_address_t src, ip4_address_t dst, u8 protocol, u16 sport,
111 u16 dport, pnat_mask_fast_t mask, clib_bihash_kv_16_8_t *kv) {
112 kv->key[0] = kv->key[1] = 0;
113 kv->key[0] = (u64)src.as_u32 << 32 | dst.as_u32;
114 kv->key[0] &= mask.as_u64[0];
116 (u64)protocol << 56 | (u64)sw_if_index << 36 | (u64)attachment << 32;
117 kv->key[1] |= sport << 16 | dport;
118 kv->key[1] &= mask.as_u64[1];