blob: 0da30261befbd67b4b64ed76ae7a51202ecfddff [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 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#ifndef VNET_VNET_URI_TRANSPORT_H_
17#define VNET_VNET_URI_TRANSPORT_H_
18
19#include <vnet/vnet.h>
20#include <vnet/ip/ip.h>
21#include <vppinfra/bihash_16_8.h>
22#include <vppinfra/bihash_48_8.h>
23
24/*
25 * Protocol independent transport properties associated to a session
26 */
27typedef struct _transport_connection
28{
29 ip46_address_t rmt_ip; /**< Remote IP */
30 ip46_address_t lcl_ip; /**< Local IP */
31 u16 lcl_port; /**< Local port */
32 u16 rmt_port; /**< Remote port */
33 u8 proto; /**< Transport protocol id */
34
35 u32 s_index; /**< Parent session index */
36 u32 c_index; /**< Connection index in transport pool */
37 u8 is_ip4; /**< Flag if IP4 connection */
38 u32 thread_index; /**< Worker-thread index */
39
40 /** Macros for 'derived classes' where base is named "connection" */
41#define c_lcl_ip connection.lcl_ip
42#define c_rmt_ip connection.rmt_ip
43#define c_lcl_ip4 connection.lcl_ip.ip4
44#define c_rmt_ip4 connection.rmt_ip.ip4
45#define c_lcl_ip6 connection.lcl_ip.ip6
46#define c_rmt_ip6 connection.rmt_ip.ip6
47#define c_lcl_port connection.lcl_port
48#define c_rmt_port connection.rmt_port
49#define c_proto connection.proto
50#define c_state connection.state
51#define c_s_index connection.s_index
52#define c_c_index connection.c_index
53#define c_is_ip4 connection.is_ip4
54#define c_thread_index connection.thread_index
55} transport_connection_t;
56
57/*
58 * Transport protocol virtual function table
59 */
60typedef struct _transport_proto_vft
61{
62 /*
63 * Setup
64 */
65 u32 (*bind) (vlib_main_t *, u32, ip46_address_t *, u16);
66 u32 (*unbind) (vlib_main_t *, u32);
67 int (*open) (ip46_address_t * addr, u16 port_host_byte_order);
68 void (*close) (u32 conn_index, u32 thread_index);
69 void (*cleanup) (u32 conn_index, u32 thread_index);
70
71 /*
72 * Transmission
73 */
74 u32 (*push_header) (transport_connection_t * tconn, vlib_buffer_t * b);
75 u16 (*send_mss) (transport_connection_t * tc);
76 u32 (*send_space) (transport_connection_t * tc);
Florin Corasd79b41e2017-03-04 05:37:52 -080077 u32 (*tx_fifo_offset) (transport_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -050078
79 /*
80 * Connection retrieval
81 */
82 transport_connection_t *(*get_connection) (u32 conn_idx, u32 thread_idx);
83 transport_connection_t *(*get_listener) (u32 conn_index);
84 transport_connection_t *(*get_half_open) (u32 conn_index);
85
86 /*
87 * Format
88 */
89 u8 *(*format_connection) (u8 * s, va_list * args);
90 u8 *(*format_listener) (u8 * s, va_list * args);
91 u8 *(*format_half_open) (u8 * s, va_list * args);
92
93} transport_proto_vft_t;
94
Florin Corasd79b41e2017-03-04 05:37:52 -080095/* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -050096/* 16 octets */
Florin Corasd79b41e2017-03-04 05:37:52 -080097typedef CLIB_PACKED (struct {
98 union
99 {
100 struct
101 {
102 ip4_address_t src; ip4_address_t dst;
103 u16 src_port;
104 u16 dst_port;
105 /* align by making this 4 octets even though its a 1-bit field
106 * NOTE: avoid key overlap with other transports that use 5 tuples for
107 * session identification.
108 */
109 u32 proto;
110 };
111 u64 as_u64[2];
112 };
113}) v4_connection_key_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
Florin Corasd79b41e2017-03-04 05:37:52 -0800115typedef CLIB_PACKED (struct {
116 union
117 {
118 struct
119 {
120 /* 48 octets */
121 ip6_address_t src; ip6_address_t dst;
122 u16 src_port;
123 u16 dst_port; u32 proto; u8 unused_for_now[8];
124 }; u64 as_u64[6];
125 };
126}) v6_connection_key_t;
127/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500128
129typedef clib_bihash_kv_16_8_t session_kv4_t;
130typedef clib_bihash_kv_48_8_t session_kv6_t;
131
132always_inline void
133make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
134 u16 lcl_port, u16 rmt_port, u8 proto)
135{
136 v4_connection_key_t key;
137 memset (&key, 0, sizeof (v4_connection_key_t));
138
139 key.src.as_u32 = lcl->as_u32;
140 key.dst.as_u32 = rmt->as_u32;
141 key.src_port = lcl_port;
142 key.dst_port = rmt_port;
143 key.proto = proto;
144
145 kv->key[0] = key.as_u64[0];
146 kv->key[1] = key.as_u64[1];
147 kv->value = ~0ULL;
148}
149
150always_inline void
151make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
152 u8 proto)
153{
154 v4_connection_key_t key;
155 memset (&key, 0, sizeof (v4_connection_key_t));
156
157 key.src.as_u32 = lcl->as_u32;
158 key.dst.as_u32 = 0;
159 key.src_port = lcl_port;
160 key.dst_port = 0;
161 key.proto = proto;
162
163 kv->key[0] = key.as_u64[0];
164 kv->key[1] = key.as_u64[1];
165 kv->value = ~0ULL;
166}
167
168always_inline void
169make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
170{
171 return make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port,
172 t->rmt_port, t->proto);
173}
174
175always_inline void
176make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
177 u16 lcl_port, u16 rmt_port, u8 proto)
178{
179 v6_connection_key_t key;
180 memset (&key, 0, sizeof (v6_connection_key_t));
181
182 key.src.as_u64[0] = lcl->as_u64[0];
183 key.src.as_u64[1] = lcl->as_u64[1];
184 key.dst.as_u64[0] = rmt->as_u64[0];
185 key.dst.as_u64[1] = rmt->as_u64[1];
186 key.src_port = lcl_port;
187 key.dst_port = rmt_port;
188 key.proto = proto;
189
190 kv->key[0] = key.as_u64[0];
191 kv->key[1] = key.as_u64[1];
Gabriel Ganneede470b2017-03-06 15:31:18 +0100192 kv->key[2] = 0;
193 kv->key[3] = 0;
194 kv->key[4] = 0;
195 kv->key[5] = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500196 kv->value = ~0ULL;
197}
198
199always_inline void
200make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
201 u8 proto)
202{
203 v6_connection_key_t key;
204 memset (&key, 0, sizeof (v6_connection_key_t));
205
206 key.src.as_u64[0] = lcl->as_u64[0];
207 key.src.as_u64[1] = lcl->as_u64[1];
208 key.dst.as_u64[0] = 0;
209 key.dst.as_u64[1] = 0;
210 key.src_port = lcl_port;
211 key.dst_port = 0;
212 key.proto = proto;
213
214 kv->key[0] = key.as_u64[0];
215 kv->key[1] = key.as_u64[1];
Gabriel Ganneede470b2017-03-06 15:31:18 +0100216 kv->key[2] = 0;
217 kv->key[3] = 0;
218 kv->key[4] = 0;
219 kv->key[5] = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500220 kv->value = ~0ULL;
221}
222
223always_inline void
224make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
225{
226 make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port,
227 t->rmt_port, t->proto);
228}
229
230typedef struct _transport_endpoint
231{
232 ip46_address_t ip;
233 u16 port;
234 u8 is_ip4;
235 u32 vrf;
236} transport_endpoint_t;
237
238typedef clib_bihash_24_8_t transport_endpoint_table_t;
239
240#define TRANSPORT_ENDPOINT_INVALID_INDEX ((u32)~0)
241
242u32
243transport_endpoint_lookup (transport_endpoint_table_t * ht,
244 ip46_address_t * ip, u16 port);
245void transport_endpoint_table_add (transport_endpoint_table_t * ht,
246 transport_endpoint_t * te, u32 value);
247void transport_endpoint_table_del (transport_endpoint_table_t * ht,
248 transport_endpoint_t * te);
249
250#endif /* VNET_VNET_URI_TRANSPORT_H_ */
251
252/*
253 * fd.io coding-style-patch-verification: ON
254 *
255 * Local Variables:
256 * eval: (c-set-style "gnu")
257 * End:
258 */