blob: 7ea7af1589bc936db64606d535ab82e4e4ac1b28 [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>
Florin Corase69f4952017-03-07 10:06:24 -080023#include <vnet/tcp/tcp_debug.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024/*
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 */
Florin Coras6cf30ad2017-04-04 23:08:23 -070033 u8 proto; /**< Transport protocol id (also session type) */
Dave Barach68b0fb02017-02-28 15:15:56 -050034
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
Florin Corase69f4952017-03-07 10:06:24 -080040#if TRANSPORT_DEBUG
Florin Coras6792ec02017-03-13 03:49:51 -070041 elog_track_t elog_track; /**< Event logging */
Florin Corase69f4952017-03-07 10:06:24 -080042#endif
43
Dave Barach68b0fb02017-02-28 15:15:56 -050044 /** Macros for 'derived classes' where base is named "connection" */
45#define c_lcl_ip connection.lcl_ip
46#define c_rmt_ip connection.rmt_ip
47#define c_lcl_ip4 connection.lcl_ip.ip4
48#define c_rmt_ip4 connection.rmt_ip.ip4
49#define c_lcl_ip6 connection.lcl_ip.ip6
50#define c_rmt_ip6 connection.rmt_ip.ip6
51#define c_lcl_port connection.lcl_port
52#define c_rmt_port connection.rmt_port
53#define c_proto connection.proto
54#define c_state connection.state
55#define c_s_index connection.s_index
56#define c_c_index connection.c_index
57#define c_is_ip4 connection.is_ip4
58#define c_thread_index connection.thread_index
Florin Corase69f4952017-03-07 10:06:24 -080059#define c_elog_track connection.elog_track
Dave Barach68b0fb02017-02-28 15:15:56 -050060} transport_connection_t;
61
62/*
63 * Transport protocol virtual function table
64 */
65typedef struct _transport_proto_vft
66{
67 /*
68 * Setup
69 */
Florin Corase69f4952017-03-07 10:06:24 -080070 u32 (*bind) (u32, ip46_address_t *, u16);
71 u32 (*unbind) (u32);
Dave Barach68b0fb02017-02-28 15:15:56 -050072 int (*open) (ip46_address_t * addr, u16 port_host_byte_order);
73 void (*close) (u32 conn_index, u32 thread_index);
74 void (*cleanup) (u32 conn_index, u32 thread_index);
75
76 /*
77 * Transmission
78 */
79 u32 (*push_header) (transport_connection_t * tconn, vlib_buffer_t * b);
80 u16 (*send_mss) (transport_connection_t * tc);
81 u32 (*send_space) (transport_connection_t * tc);
Florin Corasd79b41e2017-03-04 05:37:52 -080082 u32 (*tx_fifo_offset) (transport_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -050083
84 /*
85 * Connection retrieval
86 */
87 transport_connection_t *(*get_connection) (u32 conn_idx, u32 thread_idx);
88 transport_connection_t *(*get_listener) (u32 conn_index);
89 transport_connection_t *(*get_half_open) (u32 conn_index);
90
91 /*
92 * Format
93 */
94 u8 *(*format_connection) (u8 * s, va_list * args);
95 u8 *(*format_listener) (u8 * s, va_list * args);
96 u8 *(*format_half_open) (u8 * s, va_list * args);
Dave Barach68b0fb02017-02-28 15:15:56 -050097} transport_proto_vft_t;
98
Florin Corasd79b41e2017-03-04 05:37:52 -080099/* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500100/* 16 octets */
Florin Corasd79b41e2017-03-04 05:37:52 -0800101typedef CLIB_PACKED (struct {
102 union
103 {
104 struct
105 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700106 ip4_address_t src;
107 ip4_address_t dst;
Florin Corasd79b41e2017-03-04 05:37:52 -0800108 u16 src_port;
109 u16 dst_port;
110 /* align by making this 4 octets even though its a 1-bit field
111 * NOTE: avoid key overlap with other transports that use 5 tuples for
112 * session identification.
113 */
114 u32 proto;
115 };
116 u64 as_u64[2];
117 };
118}) v4_connection_key_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500119
Florin Corasd79b41e2017-03-04 05:37:52 -0800120typedef CLIB_PACKED (struct {
121 union
122 {
123 struct
124 {
125 /* 48 octets */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700126 ip6_address_t src;
127 ip6_address_t dst;
Florin Corasd79b41e2017-03-04 05:37:52 -0800128 u16 src_port;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700129 u16 dst_port;
130 u32 proto;
131 u8 unused_for_now[8];
132 };
133 u64 as_u64[6];
Florin Corasd79b41e2017-03-04 05:37:52 -0800134 };
135}) v6_connection_key_t;
136/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500137
138typedef clib_bihash_kv_16_8_t session_kv4_t;
139typedef clib_bihash_kv_48_8_t session_kv6_t;
140
141always_inline void
142make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
143 u16 lcl_port, u16 rmt_port, u8 proto)
144{
145 v4_connection_key_t key;
146 memset (&key, 0, sizeof (v4_connection_key_t));
147
148 key.src.as_u32 = lcl->as_u32;
149 key.dst.as_u32 = rmt->as_u32;
150 key.src_port = lcl_port;
151 key.dst_port = rmt_port;
152 key.proto = proto;
153
154 kv->key[0] = key.as_u64[0];
155 kv->key[1] = key.as_u64[1];
156 kv->value = ~0ULL;
157}
158
159always_inline void
160make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
161 u8 proto)
162{
163 v4_connection_key_t key;
164 memset (&key, 0, sizeof (v4_connection_key_t));
165
166 key.src.as_u32 = lcl->as_u32;
167 key.dst.as_u32 = 0;
168 key.src_port = lcl_port;
169 key.dst_port = 0;
170 key.proto = proto;
171
172 kv->key[0] = key.as_u64[0];
173 kv->key[1] = key.as_u64[1];
174 kv->value = ~0ULL;
175}
176
177always_inline void
178make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
179{
180 return make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port,
181 t->rmt_port, t->proto);
182}
183
184always_inline void
185make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
186 u16 lcl_port, u16 rmt_port, u8 proto)
187{
188 v6_connection_key_t key;
189 memset (&key, 0, sizeof (v6_connection_key_t));
190
191 key.src.as_u64[0] = lcl->as_u64[0];
192 key.src.as_u64[1] = lcl->as_u64[1];
193 key.dst.as_u64[0] = rmt->as_u64[0];
194 key.dst.as_u64[1] = rmt->as_u64[1];
195 key.src_port = lcl_port;
196 key.dst_port = rmt_port;
197 key.proto = proto;
198
199 kv->key[0] = key.as_u64[0];
200 kv->key[1] = key.as_u64[1];
Gabriel Ganneede470b2017-03-06 15:31:18 +0100201 kv->key[2] = 0;
202 kv->key[3] = 0;
203 kv->key[4] = 0;
204 kv->key[5] = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500205 kv->value = ~0ULL;
206}
207
208always_inline void
209make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
210 u8 proto)
211{
212 v6_connection_key_t key;
213 memset (&key, 0, sizeof (v6_connection_key_t));
214
215 key.src.as_u64[0] = lcl->as_u64[0];
216 key.src.as_u64[1] = lcl->as_u64[1];
217 key.dst.as_u64[0] = 0;
218 key.dst.as_u64[1] = 0;
219 key.src_port = lcl_port;
220 key.dst_port = 0;
221 key.proto = proto;
222
223 kv->key[0] = key.as_u64[0];
224 kv->key[1] = key.as_u64[1];
Gabriel Ganneede470b2017-03-06 15:31:18 +0100225 kv->key[2] = 0;
226 kv->key[3] = 0;
227 kv->key[4] = 0;
228 kv->key[5] = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500229 kv->value = ~0ULL;
230}
231
232always_inline void
233make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
234{
235 make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port,
236 t->rmt_port, t->proto);
237}
238
239typedef struct _transport_endpoint
240{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700241 ip46_address_t ip; /** ip address */
242 u16 port; /** port in host order */
243 u8 is_ip4; /** 1 if ip4 */
244 u32 vrf; /** fib table the endpoint is associated with */
Dave Barach68b0fb02017-02-28 15:15:56 -0500245} transport_endpoint_t;
246
247typedef clib_bihash_24_8_t transport_endpoint_table_t;
248
249#define TRANSPORT_ENDPOINT_INVALID_INDEX ((u32)~0)
250
251u32
252transport_endpoint_lookup (transport_endpoint_table_t * ht,
253 ip46_address_t * ip, u16 port);
254void transport_endpoint_table_add (transport_endpoint_table_t * ht,
255 transport_endpoint_t * te, u32 value);
256void transport_endpoint_table_del (transport_endpoint_table_t * ht,
257 transport_endpoint_t * te);
258
259#endif /* VNET_VNET_URI_TRANSPORT_H_ */
260
261/*
262 * fd.io coding-style-patch-verification: ON
263 *
264 * Local Variables:
265 * eval: (c-set-style "gnu")
266 * End:
267 */