ofproto-dpif-xlate: Make xlate_actions() caller supply flow_wildcards.
[cascardo/ovs.git] / ofproto / ofproto-dpif-xlate.h
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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_XLATE_H
16 #define OFPROTO_DPIF_XLATE_H 1
17
18 #include "dp-packet.h"
19 #include "flow.h"
20 #include "meta-flow.h"
21 #include "odp-util.h"
22 #include "ofpbuf.h"
23 #include "ofproto-dpif-mirror.h"
24 #include "ofproto-dpif-rid.h"
25 #include "ofproto-dpif.h"
26 #include "ofproto.h"
27 #include "stp.h"
28 #include "ovs-lldp.h"
29
30 struct bfd;
31 struct bond;
32 struct dpif;
33 struct lacp;
34 struct dpif_ipfix;
35 struct dpif_sflow;
36 struct mac_learning;
37 struct mcast_snooping;
38 struct xlate_cache;
39
40 struct xlate_out {
41     enum slow_path_reason slow; /* 0 if fast path may be used. */
42     bool fail_open;             /* Initial rule is fail open? */
43     bool has_learn;             /* Actions include NXAST_LEARN? */
44     bool has_normal;            /* Actions output to OFPP_NORMAL? */
45     bool has_fin_timeout;       /* Actions include NXAST_FIN_TIMEOUT? */
46     ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
47     mirror_mask_t mirrors;      /* Bitmap of associated mirrors. */
48
49     /* Recirculation IDs on which references are held. */
50     unsigned n_recircs;
51     union {
52         uint32_t recirc[2];   /* When n_recircs == 1 or 2 */
53         uint32_t *recircs;    /* When 'n_recircs' > 2 */
54     };
55
56     uint64_t odp_actions_stub[256 / 8];
57     struct ofpbuf odp_actions_buf;
58     struct ofpbuf *odp_actions;
59 };
60
61 /* Helpers to abstract the recirculation union away. */
62 static inline void
63 xlate_out_add_recirc(struct xlate_out *xout, uint32_t id)
64 {
65     if (OVS_LIKELY(xout->n_recircs < ARRAY_SIZE(xout->recirc))) {
66         xout->recirc[xout->n_recircs++] = id;
67     } else {
68         if (xout->n_recircs == ARRAY_SIZE(xout->recirc)) {
69             uint32_t *recircs = xmalloc(sizeof xout->recirc + sizeof id);
70
71             memcpy(recircs, xout->recirc, sizeof xout->recirc);
72             xout->recircs = recircs;
73         } else {
74             xout->recircs = xrealloc(xout->recircs,
75                                      (xout->n_recircs + 1) * sizeof id);
76         }
77         xout->recircs[xout->n_recircs++] = id;
78     }
79 }
80
81 static inline const uint32_t *
82 xlate_out_get_recircs(const struct xlate_out *xout)
83 {
84     if (OVS_LIKELY(xout->n_recircs <= ARRAY_SIZE(xout->recirc))) {
85         return xout->recirc;
86     } else {
87         return xout->recircs;
88     }
89 }
90
91 static inline void
92 xlate_out_take_recircs(struct xlate_out *xout)
93 {
94     if (OVS_UNLIKELY(xout->n_recircs > ARRAY_SIZE(xout->recirc))) {
95         free(xout->recircs);
96     }
97     xout->n_recircs = 0;
98 }
99
100 static inline void
101 xlate_out_free_recircs(struct xlate_out *xout)
102 {
103     if (OVS_LIKELY(xout->n_recircs <= ARRAY_SIZE(xout->recirc))) {
104         for (int i = 0; i < xout->n_recircs; i++) {
105             recirc_free_id(xout->recirc[i]);
106         }
107     } else {
108         for (int i = 0; i < xout->n_recircs; i++) {
109             recirc_free_id(xout->recircs[i]);
110         }
111         free(xout->recircs);
112     }
113 }
114
115 struct xlate_in {
116     struct ofproto_dpif *ofproto;
117
118     /* Flow to which the OpenFlow actions apply.  xlate_actions() will modify
119      * this flow when actions change header fields. */
120     struct flow flow;
121
122     /* The packet corresponding to 'flow', or a null pointer if we are
123      * revalidating without a packet to refer to. */
124     const struct dp_packet *packet;
125
126     /* Should OFPP_NORMAL update the MAC learning table?  Should "learn"
127      * actions update the flow table?
128      *
129      * We want to update these tables if we are actually processing a packet,
130      * or if we are accounting for packets that the datapath has processed, but
131      * not if we are just revalidating. */
132     bool may_learn;
133
134     /* The rule initiating translation or NULL. If both 'rule' and 'ofpacts'
135      * are NULL, xlate_actions() will do the initial rule lookup itself. */
136     struct rule_dpif *rule;
137
138     /* The actions to translate.  If 'rule' is not NULL, these may be NULL. */
139     const struct ofpact *ofpacts;
140     size_t ofpacts_len;
141
142     /* Union of the set of TCP flags seen so far in this flow.  (Used only by
143      * NXAST_FIN_TIMEOUT.  Set to zero to avoid updating updating rules'
144      * timeouts.) */
145     uint16_t tcp_flags;
146
147     /* If nonnull, flow translation calls this function just before executing a
148      * resubmit or OFPP_TABLE action.  In addition, disables logging of traces
149      * when the recursion depth is exceeded.
150      *
151      * 'rule' is the rule being submitted into.  It will be null if the
152      * resubmit or OFPP_TABLE action didn't find a matching rule.
153      *
154      * 'recurse' is the resubmit recursion depth at time of invocation.
155      *
156      * This is normally null so the client has to set it manually after
157      * calling xlate_in_init(). */
158     void (*resubmit_hook)(struct xlate_in *, struct rule_dpif *rule,
159                           int recurse);
160
161     /* If nonnull, flow translation calls this function to report some
162      * significant decision, e.g. to explain why OFPP_NORMAL translation
163      * dropped a packet.  'recurse' is the resubmit recursion depth at time of
164      * invocation. */
165     void (*report_hook)(struct xlate_in *, int recurse,
166                         const char *format, va_list args);
167
168     /* If nonnull, flow translation credits the specified statistics to each
169      * rule reached through a resubmit or OFPP_TABLE action.
170      *
171      * This is normally null so the client has to set it manually after
172      * calling xlate_in_init(). */
173     const struct dpif_flow_stats *resubmit_stats;
174
175     /* If nonnull, flow translation populates this cache with references to all
176      * modules that are affected by translation. This 'xlate_cache' may be
177      * passed to xlate_push_stats() to perform the same function as
178      * xlate_actions() without the full cost of translation.
179      *
180      * This is normally null so the client has to set it manually after
181      * calling xlate_in_init(). */
182     struct xlate_cache *xcache;
183
184     /* Allows callers to optionally supply their own buffer for the resulting
185      * odp_actions stored in xlate_out.  If NULL, the default buffer will be
186      * used. */
187     struct ofpbuf *odp_actions;
188
189     /* If nonnull, flow translation populates this with wildcards relevant in
190      * translation.  Any fields that were used to calculate the action are set,
191      * to allow caching and kernel wildcarding to work.  For example, if the
192      * flow lookup involved performing the "normal" action on IPv4 and ARP
193      * packets, 'wc' would have the 'in_port' (always set), 'dl_type' (flow
194      * match), 'vlan_tci' (normal action), and 'dl_dst' (normal action) fields
195      * set. */
196     struct flow_wildcards *wc;
197
198     /* The recirculation context related to this translation, as returned by
199      * xlate_lookup. */
200     const struct recirc_id_node *recirc;
201 };
202
203 void xlate_ofproto_set(struct ofproto_dpif *, const char *name, struct dpif *,
204                        const struct mac_learning *, struct stp *,
205                        struct rstp *, const struct mcast_snooping *,
206                        const struct mbridge *, const struct dpif_sflow *,
207                        const struct dpif_ipfix *, const struct netflow *,
208                        bool forward_bpdu, bool has_in_band,
209                        const struct dpif_backer_support *support);
210 void xlate_remove_ofproto(struct ofproto_dpif *);
211
212 void xlate_bundle_set(struct ofproto_dpif *, struct ofbundle *,
213                       const char *name, enum port_vlan_mode, int vlan,
214                       unsigned long *trunks, bool use_priority_tags,
215                       const struct bond *, const struct lacp *,
216                       bool floodable);
217 void xlate_bundle_remove(struct ofbundle *);
218
219 void xlate_ofport_set(struct ofproto_dpif *, struct ofbundle *,
220                       struct ofport_dpif *, ofp_port_t, odp_port_t,
221                       const struct netdev *, const struct cfm *, const struct bfd *,
222                       const struct lldp *, struct ofport_dpif *peer,
223                       int stp_port_no, const struct rstp_port *rstp_port,
224                       const struct ofproto_port_queue *qdscp,
225                       size_t n_qdscp, enum ofputil_port_config,
226                       enum ofputil_port_state, bool is_tunnel,
227                       bool may_enable);
228 void xlate_ofport_remove(struct ofport_dpif *);
229
230 struct ofproto_dpif * xlate_lookup_ofproto(const struct dpif_backer *,
231                                            const struct flow *,
232                                            ofp_port_t *ofp_in_port);
233 int xlate_lookup(const struct dpif_backer *, const struct flow *,
234                  struct ofproto_dpif **, struct dpif_ipfix **,
235                  struct dpif_sflow **, struct netflow **,
236                  ofp_port_t *ofp_in_port);
237
238 void xlate_actions(struct xlate_in *, struct xlate_out *);
239 void xlate_in_init(struct xlate_in *, struct ofproto_dpif *,
240                    const struct flow *, ofp_port_t in_port, struct rule_dpif *,
241                    uint16_t tcp_flags, const struct dp_packet *packet,
242                    struct flow_wildcards *);
243 void xlate_out_uninit(struct xlate_out *);
244 void xlate_actions_for_side_effects(struct xlate_in *);
245
246 int xlate_send_packet(const struct ofport_dpif *, struct dp_packet *);
247
248 struct xlate_cache *xlate_cache_new(void);
249 void xlate_push_stats(struct xlate_cache *, const struct dpif_flow_stats *);
250 void xlate_cache_clear(struct xlate_cache *);
251 void xlate_cache_delete(struct xlate_cache *);
252
253 void xlate_txn_start(void);
254 void xlate_txn_commit(void);
255
256 #endif /* ofproto-dpif-xlate.h */