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:
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.
17 * @brief NAT plugin client-IP based session affinity for load-balancing
20 #ifndef __included_nat_affinity_h__
21 #define __included_nat_affinity_h__
23 #include <vnet/ip/ip.h>
24 #include <vppinfra/bihash_16_8.h>
25 #include <vppinfra/dlist.h>
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 */
37 /* align by making this 4 octets even though its a 2 octets field */
45 typedef CLIB_PACKED(struct
47 nat_affinity_key_t key;
50 u32 per_service_index;
58 nat_affinity_t *affinity_pool;
59 clib_bihash_16_8_t affinity_hash;
60 clib_spinlock_t affinity_lock;
61 dlist_elt_t *list_pool;
62 vlib_main_t *vlib_main;
63 } nat_affinity_main_t;
65 extern nat_affinity_main_t nat_affinity_main;
68 * @brief Get new affinity per service list head index.
70 * @returns new affinity per service list head index.
72 u32 nat_affinity_get_per_service_list_head_index (void);
75 * @brief Flush all service affinity data.
77 * @param affinity_per_service_list_head_index Per sevice list head index.
79 void nat_affinity_flush_service (u32 affinity_per_service_list_head_index);
82 * @brief Initialize NAT client-IP based affinity.
84 * @param vm vlib main.
88 clib_error_t *nat_affinity_init (vlib_main_t * vm);
91 * @brief Find service backend index for client-IP and take a reference
94 * @param client_addr Client IP address.
95 * @param service_addr Service IP address.
96 * @param proto IP protocol number.
97 * @param service_port Service L4 port number.
98 * @param backend_index Service backend index for client-IP if found.
100 * @return 0 on success, non-zero value otherwise.
102 int nat_affinity_find_and_lock (ip4_address_t client_addr,
103 ip4_address_t service_addr, u8 proto,
104 u16 service_port, u8 * backend_index);
107 * @brief Create affinity record and take reference counting lock.
108 * @param client_addr Client IP address.
109 * @param service_addr Service IP address.
110 * @param proto IP protocol number.
111 * @param service_port Service L4 port number.
112 * @param backend_index Service backend index for client-IP.
113 * @param sticky_time Affinity sticky time in seconds.
114 * @param affinity_per_service_list_head_index Per sevice list head index.
116 * @return 0 on success, non-zero value otherwise.
118 int nat_affinity_create_and_lock (ip4_address_t client_addr,
119 ip4_address_t service_addr, u8 proto,
120 u16 service_port, u8 backend_index,
122 u32 affinity_per_service_list_head_index);
124 * @brief Release a reference counting lock for affinity.
126 * @param client_addr Client IP address.
127 * @param service_addr Service IP address.
128 * @param proto IP protocol number.
130 void nat_affinity_unlock (ip4_address_t client_addr,
131 ip4_address_t service_addr, u8 proto,
134 #endif /* __included_nat_affinity_h__ */
137 * fd.io coding-style-patch-verification: ON
140 * eval: (c-set-style "gnu")