nat: static mappings in flow hash
[vpp.git] / src / plugins / nat / lib / nat_proto.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #ifndef included_nat_proto_h__
16 #define included_nat_proto_h__
17
18 #include <vnet/ip/ip.h>
19
20 #define foreach_nat_protocol                                                  \
21   _ (OTHER, 0, other, "other")                                                \
22   _ (UDP, 1, udp, "udp")                                                      \
23   _ (TCP, 2, tcp, "tcp")                                                      \
24   _ (ICMP, 3, icmp, "icmp")
25
26 typedef enum
27 {
28 #define _(N, i, n, s) NAT_PROTOCOL_##N = i,
29   foreach_nat_protocol
30 #undef _
31 } nat_protocol_t;
32
33 always_inline nat_protocol_t
34 ip_proto_to_nat_proto (ip_protocol_t ip_proto)
35 {
36   static const nat_protocol_t lookup_table[256] = {
37     [IP_PROTOCOL_TCP] = NAT_PROTOCOL_TCP,
38     [IP_PROTOCOL_UDP] = NAT_PROTOCOL_UDP,
39     [IP_PROTOCOL_ICMP] = NAT_PROTOCOL_ICMP,
40     [IP_PROTOCOL_ICMP6] = NAT_PROTOCOL_ICMP,
41   };
42
43   return lookup_table[ip_proto];
44 }
45
46 static_always_inline ip_protocol_t
47 nat_proto_to_ip_proto (nat_protocol_t nat_proto)
48 {
49   ASSERT (nat_proto <= NAT_PROTOCOL_ICMP);
50
51   static const u8 lookup_table[256] = {
52     [NAT_PROTOCOL_OTHER] = ~0,
53     [NAT_PROTOCOL_TCP] = IP_PROTOCOL_TCP,
54     [NAT_PROTOCOL_UDP] = IP_PROTOCOL_UDP,
55     [NAT_PROTOCOL_ICMP] = IP_PROTOCOL_ICMP,
56   };
57
58   ASSERT (NAT_PROTOCOL_OTHER == nat_proto || NAT_PROTOCOL_TCP == nat_proto ||
59           NAT_PROTOCOL_UDP == nat_proto || NAT_PROTOCOL_ICMP == nat_proto);
60
61   return lookup_table[nat_proto];
62 }
63
64 u8 *format_nat_protocol (u8 *s, va_list *args);
65
66 uword unformat_nat_protocol (unformat_input_t *input, va_list *args);
67
68 #endif /* included_nat_proto_h__ */
69 /*
70  * fd.io coding-style-patch-verification: ON
71  *
72  * Local Variables:
73  * eval: (c-set-style "gnu")
74  * End:
75  */