Add support for connection tracking.
[cascardo/ovs.git] / lib / odp-util.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef ODP_UTIL_H
18 #define ODP_UTIL_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include "flow.h"
25 #include "hash.h"
26 #include "hmap.h"
27 #include "odp-netlink.h"
28 #include "openflow/openflow.h"
29 #include "util.h"
30
31 struct ds;
32 struct nlattr;
33 struct ofpbuf;
34 struct simap;
35 struct pkt_metadata;
36
37 #define SLOW_PATH_REASONS                                               \
38     /* These reasons are mutually exclusive. */                         \
39     SPR(SLOW_CFM,        "cfm",        "Consists of CFM packets")       \
40     SPR(SLOW_BFD,        "bfd",        "Consists of BFD packets")       \
41     SPR(SLOW_LACP,       "lacp",       "Consists of LACP packets")      \
42     SPR(SLOW_STP,        "stp",        "Consists of STP packets")       \
43     SPR(SLOW_LLDP,       "lldp",       "Consists of LLDP packets")    \
44     SPR(SLOW_CONTROLLER, "controller",                                  \
45         "Sends \"packet-in\" messages to the OpenFlow controller")      \
46     SPR(SLOW_ACTION,     "action",                                      \
47         "Uses action(s) not supported by datapath")
48
49 /* Indexes for slow-path reasons.  Client code uses "enum slow_path_reason"
50  * values instead of these, these are just a way to construct those. */
51 enum {
52 #define SPR(ENUM, STRING, EXPLANATION) ENUM##_INDEX,
53     SLOW_PATH_REASONS
54 #undef SPR
55 };
56
57 /* Reasons why a subfacet might not be fast-pathable.
58  *
59  * Each reason is a separate bit to allow reasons to be combined. */
60 enum slow_path_reason {
61 #define SPR(ENUM, STRING, EXPLANATION) ENUM = 1 << ENUM##_INDEX,
62     SLOW_PATH_REASONS
63 #undef SPR
64 };
65
66 /* Mask of all slow_path_reasons. */
67 enum {
68     SLOW_PATH_REASON_MASK = 0
69 #define SPR(ENUM, STRING, EXPLANATION) | 1 << ENUM##_INDEX 
70     SLOW_PATH_REASONS
71 #undef SPR
72 };
73
74 const char *slow_path_reason_to_explanation(enum slow_path_reason);
75
76 #define ODPP_LOCAL ODP_PORT_C(OVSP_LOCAL)
77 #define ODPP_NONE  ODP_PORT_C(UINT32_MAX)
78
79 void format_odp_actions(struct ds *, const struct nlattr *odp_actions,
80                         size_t actions_len);
81 int odp_actions_from_string(const char *, const struct simap *port_names,
82                             struct ofpbuf *odp_actions);
83
84 /* A map from odp port number to its name. */
85 struct odp_portno_names {
86     struct hmap_node hmap_node; /* A node in a port number to name hmap. */
87     odp_port_t port_no;         /* Port number in the datapath. */
88     char *name;                 /* Name associated with the above 'port_no'. */
89 };
90
91 void odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
92                           char *port_name);
93 void odp_portno_names_destroy(struct hmap *portno_names);
94 /* The maximum number of bytes that odp_flow_key_from_flow() appends to a
95  * buffer.  This is the upper bound on the length of a nlattr-formatted flow
96  * key that ovs-vswitchd fully understands.
97  *
98  * OVS doesn't insist that ovs-vswitchd and the datapath have exactly the same
99  * idea of a flow, so therefore this value isn't necessarily an upper bound on
100  * the length of a flow key that the datapath can pass to ovs-vswitchd.
101  *
102  * The longest nlattr-formatted flow key appended by odp_flow_key_from_flow()
103  * would be:
104  *
105  *                                     struct  pad  nl hdr  total
106  *                                     ------  ---  ------  -----
107  *  OVS_KEY_ATTR_PRIORITY                4    --     4      8
108  *  OVS_KEY_ATTR_TUNNEL                  0    --     4      4
109  *  - OVS_TUNNEL_KEY_ATTR_ID             8    --     4     12
110  *  - OVS_TUNNEL_KEY_ATTR_IPV4_SRC       4    --     4      8
111  *  - OVS_TUNNEL_KEY_ATTR_IPV4_DST       4    --     4      8
112  *  - OVS_TUNNEL_KEY_ATTR_TOS            1    3      4      8
113  *  - OVS_TUNNEL_KEY_ATTR_TTL            1    3      4      8
114  *  - OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT  0    --     4      4
115  *  - OVS_TUNNEL_KEY_ATTR_CSUM           0    --     4      4
116  *  - OVS_TUNNEL_KEY_ATTR_OAM            0    --     4      4
117  *  - OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS    256  --     4      260
118  *  - OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS     -    --     -      - (shared with _GENEVE_OPTS)
119  *  OVS_KEY_ATTR_IN_PORT                 4    --     4      8
120  *  OVS_KEY_ATTR_SKB_MARK                4    --     4      8
121  *  OVS_KEY_ATTR_DP_HASH                 4    --     4      8
122  *  OVS_KEY_ATTR_RECIRC_ID               4    --     4      8
123  *  OVS_KEY_ATTR_CT_STATE                4    --     4      8
124  *  OVS_KEY_ATTR_CT_ZONE                 2     2     4      8
125  *  OVS_KEY_ATTR_ETHERNET               12    --     4     16
126  *  OVS_KEY_ATTR_ETHERTYPE               2     2     4      8  (outer VLAN ethertype)
127  *  OVS_KEY_ATTR_VLAN                    2     2     4      8
128  *  OVS_KEY_ATTR_ENCAP                   0    --     4      4  (VLAN encapsulation)
129  *  OVS_KEY_ATTR_ETHERTYPE               2     2     4      8  (inner VLAN ethertype)
130  *  OVS_KEY_ATTR_IPV6                   40    --     4     44
131  *  OVS_KEY_ATTR_ICMPV6                  2     2     4      8
132  *  OVS_KEY_ATTR_ND                     28    --     4     32
133  *  ----------------------------------------------------------
134  *  total                                                 504
135  *
136  * We include some slack space in case the calculation isn't quite right or we
137  * add another field and forget to adjust this value.
138  */
139 #define ODPUTIL_FLOW_KEY_BYTES 512
140 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 34);
141
142 /* A buffer with sufficient size and alignment to hold an nlattr-formatted flow
143  * key.  An array of "struct nlattr" might not, in theory, be sufficiently
144  * aligned because it only contains 16-bit types. */
145 struct odputil_keybuf {
146     uint32_t keybuf[DIV_ROUND_UP(ODPUTIL_FLOW_KEY_BYTES, 4)];
147 };
148
149 enum odp_key_fitness odp_tun_key_from_attr(const struct nlattr *, bool udpif,
150                                            struct flow_tnl *);
151
152 int odp_ufid_from_string(const char *s_, ovs_u128 *ufid);
153 void odp_format_ufid(const ovs_u128 *ufid, struct ds *);
154 void odp_flow_format(const struct nlattr *key, size_t key_len,
155                      const struct nlattr *mask, size_t mask_len,
156                      const struct hmap *portno_names, struct ds *,
157                      bool verbose);
158 void odp_flow_key_format(const struct nlattr *, size_t, struct ds *);
159 int odp_flow_from_string(const char *s,
160                          const struct simap *port_names,
161                          struct ofpbuf *, struct ofpbuf *);
162
163 /* Indicates support for various fields. This defines how flows will be
164  * serialised. */
165 struct odp_support {
166     /* Maximum number of MPLS label stack entries to serialise in a mask. */
167     size_t max_mpls_depth;
168
169     /* If this is true, then recirculation fields will always be serialised. */
170     bool recirc;
171
172     /* If true, serialise the corresponding OVS_KEY_ATTR_CONN_* field. */
173     bool ct_state;
174     bool ct_zone;
175 };
176
177 struct odp_flow_key_parms {
178     /* The flow and mask to be serialized. In the case of masks, 'flow'
179      * is used as a template to determine how to interpret 'mask'.  For
180      * example, the 'dl_type' of 'mask' describes the mask, but it doesn't
181      * indicate whether the other fields should be interpreted as ARP, IPv4,
182      * IPv6, etc. */
183     const struct flow *flow;
184     const struct flow *mask;
185
186    /* 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
187     * number rather than a datapath port number).  Instead, if 'odp_in_port'
188     * is anything other than ODPP_NONE, it is included in 'buf' as the input
189     * port. */
190     odp_port_t odp_in_port;
191
192     /* Indicates support for various fields. If the datapath supports a field,
193      * then it will always be serialised. */
194     struct odp_support support;
195
196     /* The netlink formatted version of the flow. It is used in cases where
197      * the mask cannot be constructed from the OVS internal representation
198      * and needs to see the original form. */
199     const struct ofpbuf *key_buf;
200 };
201
202 void odp_flow_key_from_flow(const struct odp_flow_key_parms *, struct ofpbuf *);
203 void odp_flow_key_from_mask(const struct odp_flow_key_parms *, struct ofpbuf *);
204
205 uint32_t odp_flow_key_hash(const struct nlattr *, size_t);
206
207 /* Estimated space needed for metadata. */
208 enum { ODP_KEY_METADATA_SIZE = 9 * 8 };
209 void odp_key_from_pkt_metadata(struct ofpbuf *, const struct pkt_metadata *);
210 void odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
211                               struct pkt_metadata *md);
212
213 /* How well a kernel-provided flow key (a sequence of OVS_KEY_ATTR_*
214  * attributes) matches OVS userspace expectations.
215  *
216  * These values are arranged so that greater values are "more important" than
217  * lesser ones.  In particular, a single flow key can fit the descriptions for
218  * both ODP_FIT_TOO_LITTLE and ODP_FIT_TOO_MUCH.  Such a key is treated as
219  * ODP_FIT_TOO_LITTLE. */
220 enum odp_key_fitness {
221     ODP_FIT_PERFECT,            /* The key had exactly the fields we expect. */
222     ODP_FIT_TOO_MUCH,           /* The key had fields we don't understand. */
223     ODP_FIT_TOO_LITTLE,         /* The key lacked fields we expected to see. */
224     ODP_FIT_ERROR,              /* The key was invalid. */
225 };
226 enum odp_key_fitness odp_flow_key_to_flow(const struct nlattr *, size_t,
227                                           struct flow *);
228 enum odp_key_fitness odp_flow_key_to_mask(const struct nlattr *mask_key,
229                                           size_t mask_key_len,
230                                           const struct nlattr *flow_key,
231                                           size_t flow_key_len,
232                                           struct flow *mask,
233                                           const struct flow *flow);
234
235 enum odp_key_fitness odp_flow_key_to_flow_udpif(const struct nlattr *, size_t,
236                                                 struct flow *);
237 enum odp_key_fitness odp_flow_key_to_mask_udpif(const struct nlattr *mask_key,
238                                                 size_t mask_key_len,
239                                                 const struct nlattr *flow_key,
240                                                 size_t flow_key_len,
241                                                 struct flow *mask,
242                                                 const struct flow *flow);
243
244 const char *odp_key_fitness_to_string(enum odp_key_fitness);
245
246 void commit_odp_tunnel_action(const struct flow *, struct flow *base,
247                               struct ofpbuf *odp_actions);
248 void commit_masked_set_action(struct ofpbuf *odp_actions,
249                               enum ovs_key_attr key_type, const void *key,
250                               const void *mask, size_t key_size);
251 enum slow_path_reason commit_odp_actions(const struct flow *,
252                                          struct flow *base,
253                                          struct ofpbuf *odp_actions,
254                                          struct flow_wildcards *wc,
255                                          bool use_masked);
256 \f
257 /* ofproto-dpif interface.
258  *
259  * The following types and functions are logically part of ofproto-dpif.
260  * ofproto-dpif puts values of these types into the flows that it installs in
261  * the kernel datapath, though, so ovs-dpctl needs to interpret them so that
262  * it can print flows in a more human-readable manner. */
263
264 enum user_action_cookie_type {
265     USER_ACTION_COOKIE_UNSPEC,
266     USER_ACTION_COOKIE_SFLOW,        /* Packet for per-bridge sFlow sampling. */
267     USER_ACTION_COOKIE_SLOW_PATH,    /* Userspace must process this flow. */
268     USER_ACTION_COOKIE_FLOW_SAMPLE,  /* Packet for per-flow sampling. */
269     USER_ACTION_COOKIE_IPFIX,        /* Packet for per-bridge IPFIX sampling. */
270 };
271
272 /* user_action_cookie is passed as argument to OVS_ACTION_ATTR_USERSPACE.
273  * Since it is passed to kernel as u64, its size has to be 8 bytes. */
274 union user_action_cookie {
275     uint16_t type;              /* enum user_action_cookie_type. */
276
277     struct {
278         uint16_t type;          /* USER_ACTION_COOKIE_SFLOW. */
279         ovs_be16 vlan_tci;      /* Destination VLAN TCI. */
280         uint32_t output;        /* SFL_FLOW_SAMPLE_TYPE 'output' value. */
281     } sflow;
282
283     struct {
284         uint16_t type;          /* USER_ACTION_COOKIE_SLOW_PATH. */
285         uint16_t unused;
286         uint32_t reason;        /* enum slow_path_reason. */
287     } slow_path;
288
289     struct {
290         uint16_t type;          /* USER_ACTION_COOKIE_FLOW_SAMPLE. */
291         uint16_t probability;   /* Sampling probability. */
292         uint32_t collector_set_id; /* ID of IPFIX collector set. */
293         uint32_t obs_domain_id; /* Observation Domain ID. */
294         uint32_t obs_point_id;  /* Observation Point ID. */
295     } flow_sample;
296
297     struct {
298         uint16_t   type;            /* USER_ACTION_COOKIE_IPFIX. */
299         odp_port_t output_odp_port; /* The output odp port. */
300     } ipfix;
301 };
302 BUILD_ASSERT_DECL(sizeof(union user_action_cookie) == 16);
303
304 size_t odp_put_userspace_action(uint32_t pid,
305                                 const void *userdata, size_t userdata_size,
306                                 odp_port_t tunnel_out_port,
307                                 bool include_actions,
308                                 struct ofpbuf *odp_actions);
309 void odp_put_tunnel_action(const struct flow_tnl *tunnel,
310                            struct ofpbuf *odp_actions);
311
312 void odp_put_tnl_push_action(struct ofpbuf *odp_actions,
313                              struct ovs_action_push_tnl *data);
314 #endif /* odp-util.h */