session: generalize handling of network transports
[vpp.git] / src / vnet / session / transport.h
1 /*
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 <vnet/tcp/tcp_debug.h>
22
23 /*
24  * Protocol independent transport properties associated to a session
25  */
26 typedef struct _transport_connection
27 {
28   ip46_address_t rmt_ip;        /**< Remote IP */
29   ip46_address_t lcl_ip;        /**< Local IP */
30   u16 lcl_port;                 /**< Local port */
31   u16 rmt_port;                 /**< Remote port */
32   u8 proto;                     /**< Protocol id */
33   u8 is_ip4;                    /**< Flag if IP4 connection */
34   u32 fib_index;                        /**< Network namespace */
35
36   u32 s_index;                  /**< Parent session index */
37   u32 c_index;                  /**< Connection index in transport pool */
38   u32 thread_index;             /**< Worker-thread index */
39
40   fib_node_index_t rmt_fei;     /**< FIB entry index for rmt */
41   dpo_id_t rmt_dpo;             /**< Forwarding DPO for rmt */
42
43 #if TRANSPORT_DEBUG
44   elog_track_t elog_track;      /**< Event logging */
45   u32 cc_stat_tstamp;           /**< CC stats timestamp */
46 #endif
47
48   /** Macros for 'derived classes' where base is named "connection" */
49 #define c_lcl_ip connection.lcl_ip
50 #define c_rmt_ip connection.rmt_ip
51 #define c_lcl_ip4 connection.lcl_ip.ip4
52 #define c_rmt_ip4 connection.rmt_ip.ip4
53 #define c_lcl_ip6 connection.lcl_ip.ip6
54 #define c_rmt_ip6 connection.rmt_ip.ip6
55 #define c_lcl_port connection.lcl_port
56 #define c_rmt_port connection.rmt_port
57 #define c_proto connection.proto
58 #define c_fib_index connection.fib_index
59 #define c_s_index connection.s_index
60 #define c_c_index connection.c_index
61 #define c_is_ip4 connection.is_ip4
62 #define c_thread_index connection.thread_index
63 #define c_elog_track connection.elog_track
64 #define c_cc_stat_tstamp connection.cc_stat_tstamp
65 #define c_rmt_fei connection.rmt_fei
66 #define c_rmt_dpo connection.rmt_dpo
67 } transport_connection_t;
68
69 typedef enum _transport_proto
70 {
71   TRANSPORT_PROTO_TCP,
72   TRANSPORT_PROTO_UDP,
73   TRANSPORT_N_PROTO
74 } transport_proto_t;
75
76 u8 *format_transport_proto (u8 * s, va_list * args);
77 u8 *format_transport_proto_short (u8 * s, va_list * args);
78 uword unformat_transport_proto (unformat_input_t * input, va_list * args);
79
80 #define foreach_transport_connection_fields                             \
81   _(u32, sw_if_index)   /**< interface endpoint is associated with  */  \
82   _(ip46_address_t, ip) /**< ip address */                              \
83   _(u32, fib_index)     /**< fib table endpoint is associated with */   \
84   _(u8, is_ip4) /**< 1 if ip4 */                                        \
85   _(u16, port)          /**< port in net order */                       \
86
87 typedef struct _transport_endpoint
88 {
89 #define _(type, name) type name;
90   foreach_transport_connection_fields
91 #undef _
92 } transport_endpoint_t;
93
94 typedef clib_bihash_24_8_t transport_endpoint_table_t;
95
96 #define ENDPOINT_INVALID_INDEX ((u32)~0)
97
98 always_inline u8
99 transport_connection_fib_proto (transport_connection_t * tc)
100 {
101   return tc->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
102 }
103
104 always_inline u8
105 transport_endpoint_fib_proto (transport_endpoint_t * tep)
106 {
107   return tep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
108 }
109
110 always_inline u8
111 transport_is_stream (u8 proto)
112 {
113   return (proto == TRANSPORT_PROTO_TCP);
114 }
115
116 always_inline u8
117 transport_is_dgram (u8 proto)
118 {
119   return (proto == TRANSPORT_PROTO_UDP);
120 }
121
122 int transport_alloc_local_port (u8 proto, ip46_address_t * ip);
123 int transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
124                                     ip46_address_t * lcl_addr,
125                                     u16 * lcl_port);
126 void transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port);
127 void transport_init (void);
128
129 #endif /* VNET_VNET_URI_TRANSPORT_H_ */
130
131 /*
132  * fd.io coding-style-patch-verification: ON
133  *
134  * Local Variables:
135  * eval: (c-set-style "gnu")
136  * End:
137  */