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