nat: nat44-ed configuration refactor & cleanup
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed_affinity.h
1 /*
2  * Copyright (c) 2018 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 /**
16  * @file
17  * @brief NAT plugin client-IP based session affinity for load-balancing
18  */
19
20 #ifndef __included_nat44_ed_affinity_h__
21 #define __included_nat44_ed_affinity_h__
22
23 #include <vnet/ip/ip.h>
24 #include <vppinfra/bihash_16_8.h>
25 #include <vppinfra/dlist.h>
26
27 typedef struct
28 {
29   union
30   {
31     struct
32     {
33       ip4_address_t service_addr;
34       ip4_address_t client_addr;
35       /* align by making this 4 octets even though its a 1 octet field */
36       u32 proto;
37       /* align by making this 4 octets even though its a 2 octets field */
38       u32 service_port;
39     };
40     u64 as_u64[2];
41   };
42 } nat_affinity_key_t;
43
44 typedef CLIB_PACKED(struct
45 {
46   nat_affinity_key_t key;
47   u32 sticky_time;
48   u32 ref_cnt;
49   u32 per_service_index;
50   u8 backend_index;
51   f64 expire;
52 }) nat_affinity_t;
53
54 typedef struct
55 {
56   nat_affinity_t *affinity_pool;
57   clib_bihash_16_8_t affinity_hash;
58   clib_spinlock_t affinity_lock;
59   dlist_elt_t *list_pool;
60   vlib_main_t *vlib_main;
61 } nat_affinity_main_t;
62
63 extern nat_affinity_main_t nat_affinity_main;
64
65 /**
66  * @brief Get new affinity per service list head index.
67  *
68  * @returns new affinity per service list head index.
69  */
70 u32 nat_affinity_get_per_service_list_head_index (void);
71
72 /**
73  * @brief Flush all service affinity data.
74  *
75  * @param affinity_per_service_list_head_index Per sevice list head index.
76  */
77 void nat_affinity_flush_service (u32 affinity_per_service_list_head_index);
78
79 /**
80  * @brief NAT affinity enable
81  */
82 void nat_affinity_enable ();
83
84 /**
85  * @brief NAT affinity disable
86  */
87 void nat_affinity_disable ();
88
89 /**
90  * @brief Initialize NAT client-IP based affinity.
91  *
92  * @param vm vlib main.
93  *
94  * @return error code.
95  */
96 clib_error_t *nat_affinity_init (vlib_main_t * vm);
97
98 /**
99  * @brief Find service backend index for client-IP and take a reference
100  *  counting lock.
101  *
102  * @param client_addr Client IP address.
103  * @param service_addr Service IP address.
104  * @param proto IP protocol number.
105  * @param service_port Service L4 port number.
106  * @param backend_index Service backend index for client-IP if found.
107  *
108  * @return 0 on success, non-zero value otherwise.
109  */
110 int nat_affinity_find_and_lock (vlib_main_t *vm, ip4_address_t client_addr,
111                                 ip4_address_t service_addr, u8 proto,
112                                 u16 service_port, u8 *backend_index);
113
114 /**
115  * @brief Create affinity record and take reference counting lock.
116  * @param client_addr Client IP address.
117  * @param service_addr Service IP address.
118  * @param proto IP protocol number.
119  * @param service_port Service L4 port number.
120  * @param backend_index Service backend index for client-IP.
121  * @param sticky_time Affinity sticky time in seconds.
122  * @param affinity_per_service_list_head_index Per sevice list head index.
123  *
124  * @return 0 on success, non-zero value otherwise.
125  */
126 int nat_affinity_create_and_lock (ip4_address_t client_addr,
127                                   ip4_address_t service_addr, u8 proto,
128                                   u16 service_port, u8 backend_index,
129                                   u32 sticky_time,
130                                   u32 affinity_per_service_list_head_index);
131 /**
132  * @brief Release a reference counting lock for affinity.
133  *
134  * @param client_addr Client IP address.
135  * @param service_addr Service IP address.
136  * @param proto IP protocol number.
137  */
138 void nat_affinity_unlock (ip4_address_t client_addr,
139                           ip4_address_t service_addr, u8 proto,
140                           u16 service_port);
141
142 #endif /* __included_nat44_ed_affinity_h__ */
143
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "gnu")
149  * End:
150  */