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