358096e731aee24744ff49d5113b2574df07bde4
[sliver-openvswitch.git] / lib / odp-util.h
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 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 <linux/openvswitch.h>
25 #include "hash.h"
26 #include "openflow/openflow.h"
27 #include "util.h"
28
29 struct ds;
30 struct flow;
31 struct nlattr;
32 struct ofpbuf;
33 struct simap;
34
35 #define OVSP_NONE UINT32_MAX
36
37 static inline uint32_t
38 ofp_port_to_odp_port(uint16_t ofp_port)
39 {
40     switch (ofp_port) {
41     case OFPP_LOCAL:
42         return OVSP_LOCAL;
43     case OFPP_NONE:
44     case OFPP_CONTROLLER:
45         return OVSP_NONE;
46     default:
47         return ofp_port;
48     }
49 }
50
51 static inline uint16_t
52 odp_port_to_ofp_port(uint32_t odp_port)
53 {
54     switch (odp_port) {
55     case OVSP_LOCAL:
56         return OFPP_LOCAL;
57     case OVSP_NONE:
58         return OFPP_NONE;
59     default:
60         return odp_port;
61     }
62 }
63
64 void format_odp_actions(struct ds *, const struct nlattr *odp_actions,
65                         size_t actions_len);
66 int odp_actions_from_string(const char *, const struct simap *port_names,
67                             struct ofpbuf *odp_actions);
68
69 /* The maximum number of bytes that odp_flow_key_from_flow() appends to a
70  * buffer.  This is the upper bound on the length of a nlattr-formatted flow
71  * key that ovs-vswitchd fully understands.
72  *
73  * OVS doesn't insist that ovs-vswitchd and the datapath have exactly the same
74  * idea of a flow, so therefore this value isn't necessarily an upper bound on
75  * the length of a flow key that the datapath can pass to ovs-vswitchd.
76  *
77  * The longest nlattr-formatted flow key appended by odp_flow_key_from_flow()
78  * would be:
79  *
80  *                         struct  pad  nl hdr  total
81  *                         ------  ---  ------  -----
82  *  OVS_KEY_ATTR_PRIORITY      4    --     4      8
83  *  OVS_KEY_ATTR_TUN_ID        8    --     4     12
84  *  OVS_KEY_ATTR_IPV4_TUNNEL  24    --     4     28
85  *  OVS_KEY_ATTR_IN_PORT       4    --     4      8
86  *  OVS_KEY_ATTR_ETHERNET     12    --     4     16
87  *  OVS_KEY_ATTR_ETHERTYPE     2     2     4      8  (outer VLAN ethertype)
88  *  OVS_KEY_ATTR_8021Q         4    --     4      8
89  *  OVS_KEY_ATTR_ENCAP         0    --     4      4  (VLAN encapsulation)
90  *  OVS_KEY_ATTR_ETHERTYPE     2     2     4      8  (inner VLAN ethertype)
91  *  OVS_KEY_ATTR_IPV6         40    --     4     44
92  *  OVS_KEY_ATTR_ICMPV6        2     2     4      8
93  *  OVS_KEY_ATTR_ND           28    --     4     32
94  *  -------------------------------------------------
95  *  total                                       184
96  *
97  * We include some slack space in case the calculation isn't quite right or we
98  * add another field and forget to adjust this value.
99  */
100 #define ODPUTIL_FLOW_KEY_BYTES 200
101
102 /* A buffer with sufficient size and alignment to hold an nlattr-formatted flow
103  * key.  An array of "struct nlattr" might not, in theory, be sufficiently
104  * aligned because it only contains 16-bit types. */
105 struct odputil_keybuf {
106     uint32_t keybuf[DIV_ROUND_UP(ODPUTIL_FLOW_KEY_BYTES, 4)];
107 };
108
109 void odp_flow_key_format(const struct nlattr *, size_t, struct ds *);
110 int odp_flow_key_from_string(const char *s, const struct simap *port_names,
111                              struct ofpbuf *);
112
113 void odp_flow_key_from_flow(struct ofpbuf *, const struct flow *,
114                             uint32_t odp_in_port);
115
116 uint32_t odp_flow_key_hash(const struct nlattr *, size_t);
117
118 /* How well a kernel-provided flow key (a sequence of OVS_KEY_ATTR_*
119  * attributes) matches OVS userspace expectations.
120  *
121  * These values are arranged so that greater values are "more important" than
122  * lesser ones.  In particular, a single flow key can fit the descriptions for
123  * both ODP_FIT_TOO_LITTLE and ODP_FIT_TOO_MUCH.  Such a key is treated as
124  * ODP_FIT_TOO_LITTLE. */
125 enum odp_key_fitness {
126     ODP_FIT_PERFECT,            /* The key had exactly the fields we expect. */
127     ODP_FIT_TOO_MUCH,           /* The key had fields we don't understand. */
128     ODP_FIT_TOO_LITTLE,         /* The key lacked fields we expected to see. */
129     ODP_FIT_ERROR,              /* The key was invalid. */
130 };
131 enum odp_key_fitness odp_flow_key_to_flow(const struct nlattr *, size_t,
132                                           struct flow *);
133 const char *odp_key_fitness_to_string(enum odp_key_fitness);
134
135 void commit_odp_actions(const struct flow *, struct flow *base,
136                         struct ofpbuf *odp_actions);
137 \f
138 /* ofproto-dpif interface.
139  *
140  * The following types and functions are logically part of ofproto-dpif.
141  * ofproto-dpif puts values of these types into the flows that it installs in
142  * the kernel datapath, though, so ovs-dpctl needs to interpret them so that
143  * it can print flows in a more human-readable manner. */
144
145 enum user_action_cookie_type {
146     USER_ACTION_COOKIE_UNSPEC,
147     USER_ACTION_COOKIE_SFLOW,        /* Packet for sFlow sampling. */
148     USER_ACTION_COOKIE_SLOW_PATH     /* Userspace must process this flow. */
149 };
150
151 /* user_action_cookie is passed as argument to OVS_ACTION_ATTR_USERSPACE.
152  * Since it is passed to kernel as u64, its size has to be 8 bytes. */
153 union user_action_cookie {
154     uint16_t type;              /* enum user_action_cookie_type. */
155
156     struct {
157         uint16_t type;          /* USER_ACTION_COOKIE_SFLOW. */
158         ovs_be16 vlan_tci;      /* Destination VLAN TCI. */
159         uint32_t output;        /* SFL_FLOW_SAMPLE_TYPE 'output' value. */
160     } sflow;
161
162     struct {
163         uint16_t type;          /* USER_ACTION_COOKIE_SLOW_PATH. */
164         uint16_t unused;
165         uint32_t reason;        /* enum slow_path_reason. */
166     } slow_path;
167 };
168 BUILD_ASSERT_DECL(sizeof(union user_action_cookie) == 8);
169
170 size_t odp_put_userspace_action(uint32_t pid,
171                                 const union user_action_cookie *,
172                                 struct ofpbuf *odp_actions);
173
174 /* Reasons why a subfacet might not be fast-pathable. */
175 enum slow_path_reason {
176     /* These reasons are mutually exclusive. */
177     SLOW_CFM = 1 << 0,          /* CFM packets need per-packet processing. */
178     SLOW_LACP = 1 << 1,         /* LACP packets need per-packet processing. */
179     SLOW_STP = 1 << 2,          /* STP packets need per-packet processing. */
180     SLOW_IN_BAND = 1 << 3,      /* In-band control needs every packet. */
181
182     /* Mutually exclusive with SLOW_CFM, SLOW_LACP, SLOW_STP.
183      * Could possibly appear with SLOW_IN_BAND. */
184     SLOW_CONTROLLER = 1 << 4,   /* Packets must go to OpenFlow controller. */
185
186     /* This can appear on its own, or, theoretically at least, along with any
187      * other combination of reasons. */
188     SLOW_MATCH = 1 << 5,        /* Datapath can't match specifically enough. */
189 };
190
191 #endif /* odp-util.h */