24d509d671040d7a92fbd1575c29bdf62b1abef6
[cascardo/ovs.git] / ofproto / ofproto-dpif.h
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
2  *
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 #ifndef OFPROTO_DPIF_H
16 #define OFPROTO_DPIF_H 1
17
18 #include <stdint.h>
19
20 #include "fail-open.h"
21 #include "hmapx.h"
22 #include "odp-util.h"
23 #include "ofp-util.h"
24 #include "ovs-thread.h"
25 #include "ofproto-provider.h"
26 #include "timer.h"
27 #include "util.h"
28 #include "ovs-thread.h"
29
30 /* Priority for internal rules created to handle recirculation */
31 #define RECIRC_RULE_PRIORITY 20
32
33 union user_action_cookie;
34 struct dpif_flow_stats;
35 struct ofproto;
36 struct ofproto_dpif;
37 struct ofproto_packet_in;
38 struct ofport_dpif;
39 struct dpif_backer;
40 struct OVS_LOCKABLE rule_dpif;
41 struct OVS_LOCKABLE group_dpif;
42
43 enum rule_dpif_lookup_verdict {
44     RULE_DPIF_LOOKUP_VERDICT_MATCH,         /* A match occurred. */
45     RULE_DPIF_LOOKUP_VERDICT_CONTROLLER,    /* A miss occurred and the packet
46                                              * should be passed to
47                                              * the controller. */
48     RULE_DPIF_LOOKUP_VERDICT_DROP,          /* A miss occurred and the packet
49                                              * should be dropped. */
50     RULE_DPIF_LOOKUP_VERDICT_DEFAULT,       /* A miss occurred and the packet
51                                              * should handled by the default
52                                              * miss behaviour.
53                                              * For pre-OF1.3 it should be
54                                              * forwarded to the controller.
55                                              * For OF1.3+ it should be
56                                              * dropped. */
57 };
58
59
60 /* Ofproto-dpif -- DPIF based ofproto implementation.
61  *
62  * Ofproto-dpif provides an ofproto implementation for those platforms which
63  * implement the netdev and dpif interface defined in netdev.h and dpif.h.  The
64  * most important of which is the Linux Kernel Module (dpif-linux), but
65  * alternatives are supported such as a userspace only implementation
66  * (dpif-netdev), and a dummy implementation used for unit testing.
67  *
68  * Ofproto-dpif is divided into three major chunks.
69  *
70  * - ofproto-dpif.c
71  *   The main ofproto-dpif module is responsible for implementing the
72  *   provider interface, installing and removing datapath flows, maintaining
73  *   packet statistics, running protocols (BFD, LACP, STP, etc), and
74  *   configuring relevant submodules.
75  *
76  * - ofproto-dpif-upcall.c
77  *   Ofproto-dpif-upcall is responsible for retrieving upcalls from the kernel,
78  *   processing miss upcalls, and handing more complex ones up to the main
79  *   ofproto-dpif module.  Miss upcall processing boils down to figuring out
80  *   what each packet's actions are, executing them (i.e. asking the kernel to
81  *   forward it), and handing it up to ofproto-dpif to decided whether or not
82  *   to install a kernel flow.
83  *
84  * - ofproto-dpif-xlate.c
85  *   Ofproto-dpif-xlate is responsible for translating OpenFlow actions into
86  *   datapath actions. */
87
88 size_t ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *);
89 bool ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *);
90
91 uint8_t rule_dpif_lookup(struct ofproto_dpif *, struct flow *,
92                          struct flow_wildcards *, struct rule_dpif **rule,
93                          bool take_ref, const struct dpif_flow_stats *);
94
95 enum rule_dpif_lookup_verdict rule_dpif_lookup_from_table(
96     struct ofproto_dpif *, struct flow *, struct flow_wildcards *,
97     bool force_controller_on_miss, uint8_t *table_id, struct rule_dpif **rule,
98     bool take_ref, const struct dpif_flow_stats *);
99
100 static inline void rule_dpif_ref(struct rule_dpif *);
101 static inline void rule_dpif_unref(struct rule_dpif *);
102
103 void rule_dpif_credit_stats(struct rule_dpif *rule ,
104                             const struct dpif_flow_stats *);
105
106 static inline bool rule_dpif_is_fail_open(const struct rule_dpif *);
107 static inline bool rule_dpif_is_table_miss(const struct rule_dpif *);
108 static inline bool rule_dpif_is_internal(const struct rule_dpif *);
109
110 uint8_t rule_dpif_get_table(const struct rule_dpif *);
111
112 bool table_is_internal(uint8_t table_id);
113
114 const struct rule_actions *rule_dpif_get_actions(const struct rule_dpif *);
115 uint32_t rule_dpif_get_recirc_id(struct rule_dpif *);
116 void rule_set_recirc_id(struct rule *, uint32_t id);
117
118 ovs_be64 rule_dpif_get_flow_cookie(const struct rule_dpif *rule);
119
120 void rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
121                                uint16_t hard_timeout);
122
123 void choose_miss_rule(enum ofputil_port_config,
124                       struct rule_dpif *miss_rule,
125                       struct rule_dpif *no_packet_in_rule,
126                       struct rule_dpif **rule, bool take_ref);
127
128 void group_dpif_credit_stats(struct group_dpif *,
129                              struct ofputil_bucket *,
130                              const struct dpif_flow_stats *);
131 bool group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
132                        struct group_dpif **group);
133
134 void group_dpif_get_buckets(const struct group_dpif *group,
135                             const struct list **buckets);
136 enum ofp11_group_type group_dpif_get_type(const struct group_dpif *group);
137
138 bool ofproto_has_vlan_splinters(const struct ofproto_dpif *);
139 ofp_port_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
140                                   ofp_port_t realdev_ofp_port,
141                                   ovs_be16 vlan_tci);
142 bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *,
143                      struct ofpbuf *packet);
144
145 int ofproto_dpif_execute_actions(struct ofproto_dpif *, const struct flow *,
146                                  struct rule_dpif *, const struct ofpact *,
147                                  size_t ofpacts_len, struct ofpbuf *);
148 void ofproto_dpif_send_packet_in(struct ofproto_dpif *,
149                                  struct ofproto_packet_in *);
150 bool ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *);
151 int ofproto_dpif_send_packet(const struct ofport_dpif *, struct ofpbuf *);
152 void ofproto_dpif_flow_mod(struct ofproto_dpif *, struct ofputil_flow_mod *);
153 struct rule_dpif *ofproto_dpif_refresh_rule(struct rule_dpif *);
154
155 struct ofport_dpif *odp_port_to_ofport(const struct dpif_backer *, odp_port_t);
156
157 /*
158  * Recirculation
159  * =============
160  *
161  * Recirculation is a technique to allow a frame to re-enter the packet
162  * processing path for one or multiple times to achieve more flexible packet
163  * processing in the data path. MPLS handling and selecting bond slave port
164  * of a bond ports.
165  *
166  * Data path and user space interface
167  * -----------------------------------
168  *
169  * Two new fields, recirc_id and dp_hash, are added to the current flow data
170  * structure. They are both of type uint32_t. In addition, a new action,
171  * RECIRC, are added.
172  *
173  * The value recirc_id is used to distinguish a packet from multiple
174  * iterations of recirculation. A packet initially received is considered of
175  * having recirc_id of 0. Recirc_id is managed by the user space, opaque to
176  * the data path.
177  *
178  * On the other hand, dp_hash can only be computed by the data path, opaque to
179  * the user space. In fact, user space may not able to recompute the hash
180  * value. The dp_hash value should be wildcarded when for a newly received
181  * packet. RECIRC action specifies whether the hash is computed. If computed,
182  * how many fields to be included in the hash computation. The computed hash
183  * value is stored into the dp_hash field prior to recirculation.
184  *
185  * The RECIRC action computes and set the dp_hash field, set the recirc_id
186  * field and then reprocess the packet as if it was received on the same input
187  * port. RECIRC action works like a function call; actions listed behind the
188  * RECIRC action will be executed after its execution.  RECIRC action can be
189  * nested, data path implementation limits the number of recirculation executed
190  * to prevent unreasonable nesting depth or infinite loop.
191  *
192  * Both flow fields and the RECIRC action are exposed as open flow fields via
193  * Nicira extensions.
194  *
195  * Post recirculation flow
196  * ------------------------
197  *
198  * At the open flow level, post recirculation rules are always hidden from the
199  * controller.  They are installed in table 254 which is set up as a hidden
200  * table during boot time. Those rules are managed by the local user space
201  * program only.
202  *
203  * To speed up the classifier look up process, recirc_id is always reflected
204  * into the metadata field, since recirc_id is required to be exactly matched.
205  *
206  * Classifier look up always starts with table 254. A post recirculation flow
207  * lookup should find its hidden rule within this table. On the other hand, A
208  * newly received packet should miss all post recirculation rules because its
209  * recirc_id is zero, then hit a pre-installed lower priority rule to redirect
210  * classifier to look up starting from table 0:
211  *
212  *       * , actions=resubmit(,0)
213  *
214  * Post recirculation data path flows are managed like other data path flows.
215  * They are created on demand. Miss handling, stats collection and revalidation
216  * work the same way as regular flows.
217  */
218
219 uint32_t ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto);
220 void ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id);
221 int ofproto_dpif_add_internal_flow(struct ofproto_dpif *,
222                                    const struct match *, int priority,
223                                    uint16_t idle_timeout,
224                                    const struct ofpbuf *ofpacts,
225                                    struct rule **rulep);
226 int ofproto_dpif_delete_internal_flow(struct ofproto_dpif *, struct match *,
227                                       int priority);
228
229 /* Number of implemented OpenFlow tables. */
230 enum { N_TABLES = 255 };
231 enum { TBL_INTERNAL = N_TABLES - 1 };    /* Used for internal hidden rules. */
232 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
233
234 \f
235 /* struct rule_dpif has struct rule as it's first member. */
236 #define RULE_CAST(RULE) ((struct rule *)RULE)
237 #define GROUP_CAST(GROUP) ((struct ofgroup *)GROUP)
238
239 static inline struct group_dpif* group_dpif_ref(struct group_dpif *group)
240 {
241     if (group) {
242         ofproto_group_ref(GROUP_CAST(group));
243     }
244     return group;
245 }
246
247 static inline void group_dpif_unref(struct group_dpif *group)
248 {
249     if (group) {
250         ofproto_group_unref(GROUP_CAST(group));
251     }
252 }
253
254 static inline void rule_dpif_ref(struct rule_dpif *rule)
255 {
256     if (rule) {
257         ofproto_rule_ref(RULE_CAST(rule));
258     }
259 }
260
261 static inline bool rule_dpif_try_ref(struct rule_dpif *rule)
262 {
263     if (rule) {
264         return ofproto_rule_try_ref(RULE_CAST(rule));
265     }
266     return false;
267 }
268
269
270 static inline void rule_dpif_unref(struct rule_dpif *rule)
271 {
272     if (rule) {
273         ofproto_rule_unref(RULE_CAST(rule));
274     }
275 }
276
277 static inline bool rule_dpif_is_fail_open(const struct rule_dpif *rule)
278 {
279     return is_fail_open_rule(RULE_CAST(rule));
280 }
281
282 static inline bool rule_dpif_is_table_miss(const struct rule_dpif *rule)
283 {
284     return rule_is_table_miss(RULE_CAST(rule));
285 }
286
287 /* Returns true if 'rule' is an internal rule, false otherwise. */
288 static inline bool rule_dpif_is_internal(const struct rule_dpif *rule)
289 {
290     return RULE_CAST(rule)->table_id == TBL_INTERNAL;
291 }
292
293 #undef RULE_CAST
294
295 #endif /* ofproto-dpif.h */