]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif-xlate.c
appveyor.yml: Remove from docs.
[ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
18080541 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
9583bc14
EJ
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#include <config.h>
16
17#include "ofproto/ofproto-dpif-xlate.h"
18
8449c4d6 19#include <errno.h>
a36de779
PS
20#include <arpa/inet.h>
21#include <net/if.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
8449c4d6 24
a36de779 25#include "tnl-arp-cache.h"
db7d4e46 26#include "bfd.h"
9583bc14
EJ
27#include "bitmap.h"
28#include "bond.h"
29#include "bundle.h"
30#include "byte-order.h"
db7d4e46 31#include "cfm.h"
9583bc14
EJ
32#include "connmgr.h"
33#include "coverage.h"
e14deea0 34#include "dp-packet.h"
9583bc14
EJ
35#include "dpif.h"
36#include "dynamic-string.h"
f7f1ea29 37#include "in-band.h"
db7d4e46 38#include "lacp.h"
9583bc14 39#include "learn.h"
46c88433 40#include "list.h"
0477baa9 41#include "ovs-lldp.h"
9583bc14 42#include "mac-learning.h"
6d95c4e8 43#include "mcast-snooping.h"
9583bc14
EJ
44#include "meta-flow.h"
45#include "multipath.h"
46#include "netdev-vport.h"
47#include "netlink.h"
48#include "nx-match.h"
49#include "odp-execute.h"
50#include "ofp-actions.h"
51#include "ofproto/ofproto-dpif-ipfix.h"
ec7ceaed 52#include "ofproto/ofproto-dpif-mirror.h"
60d02c72 53#include "ofproto/ofproto-dpif-monitor.h"
9583bc14
EJ
54#include "ofproto/ofproto-dpif-sflow.h"
55#include "ofproto/ofproto-dpif.h"
6f00e29b 56#include "ofproto/ofproto-provider.h"
a36de779
PS
57#include "ovs-router.h"
58#include "tnl-ports.h"
9583bc14 59#include "tunnel.h"
e6211adc 60#include "openvswitch/vlog.h"
9583bc14 61
46c88433 62COVERAGE_DEFINE(xlate_actions);
0f032e95 63COVERAGE_DEFINE(xlate_actions_oversize);
7d031d7e 64COVERAGE_DEFINE(xlate_actions_too_many_output);
9583bc14
EJ
65
66VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
67
8a553e9a
EJ
68/* Maximum depth of flow table recursion (due to resubmit actions) in a
69 * flow translation. */
70#define MAX_RESUBMIT_RECURSION 64
adcf00ba
AZ
71#define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
72 internal table. */
8a553e9a 73
98b07853
BP
74/* Maximum number of resubmit actions in a flow translation, whether they are
75 * recursive or not. */
76#define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
77
46c88433
EJ
78struct xbridge {
79 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
80 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
81
ca6ba700 82 struct ovs_list xbundles; /* Owned xbundles. */
46c88433
EJ
83 struct hmap xports; /* Indexed by ofp_port. */
84
85 char *name; /* Name used in log messages. */
89a8a7f0 86 struct dpif *dpif; /* Datapath interface. */
46c88433 87 struct mac_learning *ml; /* Mac learning handle. */
6d95c4e8 88 struct mcast_snooping *ms; /* Multicast Snooping handle. */
46c88433
EJ
89 struct mbridge *mbridge; /* Mirroring. */
90 struct dpif_sflow *sflow; /* SFlow handle, or null. */
91 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
ce3955be 92 struct netflow *netflow; /* Netflow handle, or null. */
9d189a50 93 struct stp *stp; /* STP or null if disabled. */
9efd308e 94 struct rstp *rstp; /* RSTP or null if disabled. */
46c88433 95
46c88433
EJ
96 bool has_in_band; /* Bridge has in band control? */
97 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
4b97b70d 98
b440dd8c
JS
99 /* Datapath feature support. */
100 struct dpif_backer_support support;
46c88433
EJ
101};
102
103struct xbundle {
104 struct hmap_node hmap_node; /* In global 'xbundles' map. */
105 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
106
ca6ba700 107 struct ovs_list list_node; /* In parent 'xbridges' list. */
46c88433
EJ
108 struct xbridge *xbridge; /* Parent xbridge. */
109
ca6ba700 110 struct ovs_list xports; /* Contains "struct xport"s. */
46c88433
EJ
111
112 char *name; /* Name used in log messages. */
113 struct bond *bond; /* Nonnull iff more than one port. */
114 struct lacp *lacp; /* LACP handle or null. */
115
116 enum port_vlan_mode vlan_mode; /* VLAN mode. */
117 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
118 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
119 * NULL if all VLANs are trunked. */
120 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
121 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
122};
123
124struct xport {
125 struct hmap_node hmap_node; /* Node in global 'xports' map. */
126 struct ofport_dpif *ofport; /* Key in global 'xports map. */
127
128 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
129 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
130
131 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
132
ca6ba700 133 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
46c88433
EJ
134 struct xbundle *xbundle; /* Parent xbundle or null. */
135
136 struct netdev *netdev; /* 'ofport''s netdev. */
137
138 struct xbridge *xbridge; /* Parent bridge. */
139 struct xport *peer; /* Patch port peer or null. */
140
141 enum ofputil_port_config config; /* OpenFlow port configuration. */
dd8cd4b4 142 enum ofputil_port_state state; /* OpenFlow port state. */
92cf817b 143 int stp_port_no; /* STP port number or -1 if not in use. */
f025bcb7 144 struct rstp_port *rstp_port; /* RSTP port or null. */
46c88433 145
55954f6e
EJ
146 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
147
46c88433
EJ
148 bool may_enable; /* May be enabled in bonds. */
149 bool is_tunnel; /* Is a tunnel port. */
150
151 struct cfm *cfm; /* CFM handle or null. */
152 struct bfd *bfd; /* BFD handle or null. */
0477baa9 153 struct lldp *lldp; /* LLDP handle or null. */
46c88433
EJ
154};
155
4d0acc70
EJ
156struct xlate_ctx {
157 struct xlate_in *xin;
158 struct xlate_out *xout;
159
46c88433 160 const struct xbridge *xbridge;
4d0acc70 161
621b8064 162 /* Flow tables version at the beginning of the translation. */
18721c4a 163 cls_version_t tables_version;
621b8064 164
4d0acc70
EJ
165 /* Flow at the last commit. */
166 struct flow base_flow;
167
168 /* Tunnel IP destination address as received. This is stored separately
169 * as the base_flow.tunnel is cleared on init to reflect the datapath
170 * behavior. Used to make sure not to send tunneled output to ourselves,
171 * which might lead to an infinite loop. This could happen easily
172 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
173 * actually set the tun_dst field. */
174 ovs_be32 orig_tunnel_ip_dst;
175
176 /* Stack for the push and pop actions. Each stack element is of type
177 * "union mf_subvalue". */
4d0acc70
EJ
178 struct ofpbuf stack;
179
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
182
49a73e0c
BP
183 /* Flow translation populates this with wildcards relevant in translation.
184 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
185 * null, this is a pointer to uninitialized scratch memory. This allows
186 * code to blindly write to 'ctx->wc' without worrying about whether the
187 * caller really wants wildcards. */
188 struct flow_wildcards *wc;
189
1520ef4f
BP
190 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
191 * this is the same pointer. When 'xin->odp_actions' is null, this points
192 * to a scratch ofpbuf. This allows code to add actions to
193 * 'ctx->odp_actions' without worrying about whether the caller really
194 * wants actions. */
195 struct ofpbuf *odp_actions;
196
98b07853
BP
197 /* Resubmit statistics, via xlate_table_action(). */
198 int recurse; /* Current resubmit nesting depth. */
199 int resubmits; /* Total number of resubmits. */
5a070238 200 bool in_group; /* Currently translating ofgroup, if true. */
029ca940 201 bool in_action_set; /* Currently translating action_set, if true. */
98b07853 202
4d0acc70 203 uint8_t table_id; /* OpenFlow table ID where flow was found. */
8b1e5560
JR
204 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
205 uint32_t orig_skb_priority; /* Priority when packet arrived. */
4d0acc70 206 uint32_t sflow_n_outputs; /* Number of output ports. */
4e022ec0 207 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
2031ef97 208 ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
4d0acc70 209 bool exit; /* No further actions should be processed. */
3d6151f3 210 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
7fdb60a7 211
e672ff9b
JR
212 /* These are used for non-bond recirculation. The recirculation IDs are
213 * stored in xout and must be associated with a datapath flow (ukey),
214 * otherwise they will be freed when the xout is uninitialized.
215 *
216 *
217 * Steps in Recirculation Translation
218 * ==================================
219 *
220 * At some point during translation, the code recognizes the need for
221 * recirculation. For example, recirculation is necessary when, after
222 * popping the last MPLS label, an action or a match tries to examine or
223 * modify a field that has been newly revealed following the MPLS label.
224 *
225 * The simplest part of the work to be done is to commit existing changes to
226 * the packet, which produces datapath actions corresponding to the changes,
227 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
228 *
229 * The main problem here is preserving state. When the datapath executes
230 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
231 * for the post-recirculation actions. At this point userspace has to
232 * resume the translation where it left off, which means that it has to
233 * execute the following:
234 *
235 * - The action that prompted recirculation, and any actions following
236 * it within the same flow.
237 *
238 * - If the action that prompted recirculation was invoked within a
239 * NXAST_RESUBMIT, then any actions following the resubmit. These
240 * "resubmit"s can be nested, so this has to go all the way up the
241 * control stack.
242 *
243 * - The OpenFlow 1.1+ action set.
244 *
245 * State that actions and flow table lookups can depend on, such as the
246 * following, must also be preserved:
247 *
248 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
249 *
250 * - Action set, stack
251 *
252 * - The table ID and cookie of the flow being translated at each level
253 * of the control stack (since OFPAT_CONTROLLER actions send these to
254 * the controller).
255 *
256 * Translation allows for the control of this state preservation via these
257 * members. When a need for recirculation is identified, the translation
258 * process:
259 *
260 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
261 * action set is part of what needs to be preserved, so this allows the
262 * action set and the additional state to share the 'action_set' buffer.
263 * Later steps can tell that setup for recirculation is in progress from
264 * the nonnegative value of 'recirc_action_offset'.
265 *
266 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
267 * translation process.
268 *
269 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
270 * holds the current table ID and cookie so that they can be restored
271 * during a post-recirculation upcall translation.
272 *
273 * 4. Adds the action that prompted recirculation and any actions following
274 * it within the same flow to 'action_set', so that they can be executed
275 * during a post-recirculation upcall translation.
276 *
277 * 5. Returns.
278 *
279 * 6. The action that prompted recirculation might be nested in a stack of
280 * nested "resubmit"s that have actions remaining. Each of these notices
281 * that we're exiting (from 'exit') and that recirculation setup is in
282 * progress (from 'recirc_action_offset') and responds by adding more
283 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
284 * actions that were yet unprocessed.
285 *
286 * The caller stores all the state produced by this process associated with
287 * the recirculation ID. For post-recirculation upcall translation, the
288 * caller passes it back in for the new translation to execute. The
289 * process yielded a set of ofpacts that can be translated directly, so it
290 * is not much of a special case at that point.
291 */
292 int recirc_action_offset; /* Offset in 'action_set' to actions to be
293 * executed after recirculation, or -1. */
294 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
295 * action, or -1. */
296
7bbdd84f
SH
297 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
298 * This is a trigger for recirculation in cases where translating an action
299 * or looking up a flow requires access to the fields of the packet after
1d741d6d 300 * the MPLS label stack that was originally present. */
7bbdd84f
SH
301 bool was_mpls;
302
7fdb60a7
SH
303 /* OpenFlow 1.1+ action set.
304 *
305 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
306 * When translation is otherwise complete, ofpacts_execute_action_set()
307 * converts it to a set of "struct ofpact"s that can be translated into
ed9c9e3e 308 * datapath actions. */
c61f3870 309 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
7fdb60a7 310 struct ofpbuf action_set; /* Action set. */
4d0acc70
EJ
311};
312
ed9c9e3e 313static void xlate_action_set(struct xlate_ctx *ctx);
704bb0bf 314static void xlate_commit_actions(struct xlate_ctx *ctx);
ed9c9e3e 315
1d741d6d
JR
316static void
317ctx_trigger_recirculation(struct xlate_ctx *ctx)
318{
319 ctx->exit = true;
320 ctx->recirc_action_offset = ctx->action_set.size;
321}
322
323static bool
324ctx_first_recirculation_action(const struct xlate_ctx *ctx)
325{
326 return ctx->recirc_action_offset == ctx->action_set.size;
327}
328
e672ff9b
JR
329static inline bool
330exit_recirculates(const struct xlate_ctx *ctx)
331{
332 /* When recirculating the 'recirc_action_offset' has a non-negative value.
333 */
334 return ctx->recirc_action_offset >= 0;
335}
336
337static void compose_recirculate_action(struct xlate_ctx *ctx);
338
9583bc14
EJ
339/* A controller may use OFPP_NONE as the ingress port to indicate that
340 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
341 * when an input bundle is needed for validation (e.g., mirroring or
342 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
3548d242
BP
343 * any 'port' structs, so care must be taken when dealing with it. */
344static struct xbundle ofpp_none_bundle = {
345 .name = "OFPP_NONE",
346 .vlan_mode = PORT_VLAN_TRUNK
347};
9583bc14 348
55954f6e
EJ
349/* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
350 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
351 * traffic egressing the 'ofport' with that priority should be marked with. */
352struct skb_priority_to_dscp {
353 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
354 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
355
356 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
357};
358
b256dc52
JS
359enum xc_type {
360 XC_RULE,
361 XC_BOND,
362 XC_NETDEV,
363 XC_NETFLOW,
364 XC_MIRROR,
365 XC_LEARN,
366 XC_NORMAL,
367 XC_FIN_TIMEOUT,
1e684d7d 368 XC_GROUP,
a36de779 369 XC_TNL_ARP,
b256dc52
JS
370};
371
372/* xlate_cache entries hold enough information to perform the side effects of
373 * xlate_actions() for a rule, without needing to perform rule translation
374 * from scratch. The primary usage of these is to submit statistics to objects
375 * that a flow relates to, although they may be used for other effects as well
376 * (for instance, refreshing hard timeouts for learned flows). */
377struct xc_entry {
378 enum xc_type type;
379 union {
380 struct rule_dpif *rule;
381 struct {
382 struct netdev *tx;
383 struct netdev *rx;
384 struct bfd *bfd;
385 } dev;
386 struct {
387 struct netflow *netflow;
388 struct flow *flow;
389 ofp_port_t iface;
390 } nf;
391 struct {
392 struct mbridge *mbridge;
393 mirror_mask_t mirrors;
394 } mirror;
395 struct {
396 struct bond *bond;
397 struct flow *flow;
398 uint16_t vid;
399 } bond;
400 struct {
4165b5e0
JS
401 struct ofproto_dpif *ofproto;
402 struct ofputil_flow_mod *fm;
403 struct ofpbuf *ofpacts;
b256dc52
JS
404 } learn;
405 struct {
406 struct ofproto_dpif *ofproto;
407 struct flow *flow;
408 int vlan;
409 } normal;
410 struct {
411 struct rule_dpif *rule;
412 uint16_t idle;
413 uint16_t hard;
414 } fin;
1e684d7d
RW
415 struct {
416 struct group_dpif *group;
417 struct ofputil_bucket *bucket;
418 } group;
a36de779
PS
419 struct {
420 char br_name[IFNAMSIZ];
421 ovs_be32 d_ip;
422 } tnl_arp_cache;
b256dc52
JS
423 } u;
424};
425
426#define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
427 entries = xcache->entries; \
428 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
429 entry; \
430 entry = ofpbuf_try_pull(&entries, sizeof *entry))
431
432struct xlate_cache {
433 struct ofpbuf entries;
434};
435
84f0f298
RW
436/* Xlate config contains hash maps of all bridges, bundles and ports.
437 * Xcfgp contains the pointer to the current xlate configuration.
438 * When the main thread needs to change the configuration, it copies xcfgp to
439 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
440 * does not block handler and revalidator threads. */
441struct xlate_cfg {
442 struct hmap xbridges;
443 struct hmap xbundles;
444 struct hmap xports;
445};
b1b72f2d 446static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
f439f23b 447static struct xlate_cfg *new_xcfg = NULL;
46c88433
EJ
448
449static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
450static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
451 struct xlate_ctx *);
adcf00ba 452static void xlate_normal(struct xlate_ctx *);
c1b3756c
BP
453static inline void xlate_report(struct xlate_ctx *, const char *, ...)
454 OVS_PRINTF_FORMAT(2, 3);
6d328fa2
SH
455static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
456 uint8_t table_id, bool may_packet_in,
457 bool honor_table_miss);
46c88433
EJ
458static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
459static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
460static void output_normal(struct xlate_ctx *, const struct xbundle *,
9583bc14 461 uint16_t vlan);
e93ef1c7
JR
462
463/* Optional bond recirculation parameter to compose_output_action(). */
464struct xlate_bond_recirc {
465 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
466 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
467 uint32_t hash_basis; /* Compute hash for recirc before. */
468};
469
470static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
471 const struct xlate_bond_recirc *xr);
9583bc14 472
84f0f298
RW
473static struct xbridge *xbridge_lookup(struct xlate_cfg *,
474 const struct ofproto_dpif *);
475static struct xbundle *xbundle_lookup(struct xlate_cfg *,
476 const struct ofbundle *);
477static struct xport *xport_lookup(struct xlate_cfg *,
478 const struct ofport_dpif *);
46c88433 479static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
480static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
481 uint32_t skb_priority);
482static void clear_skb_priorities(struct xport *);
16194afd 483static size_t count_skb_priorities(const struct xport *);
55954f6e
EJ
484static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
485 uint8_t *dscp);
46c88433 486
b256dc52
JS
487static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
488 enum xc_type type);
84f0f298
RW
489static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
490static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
491static void xlate_xport_init(struct xlate_cfg *, struct xport *);
9efd308e 492static void xlate_xbridge_set(struct xbridge *, struct dpif *,
9efd308e
DV
493 const struct mac_learning *, struct stp *,
494 struct rstp *, const struct mcast_snooping *,
495 const struct mbridge *,
496 const struct dpif_sflow *,
497 const struct dpif_ipfix *,
2f47cdf4 498 const struct netflow *,
84f0f298 499 bool forward_bpdu, bool has_in_band,
b440dd8c 500 const struct dpif_backer_support *);
84f0f298
RW
501static void xlate_xbundle_set(struct xbundle *xbundle,
502 enum port_vlan_mode vlan_mode, int vlan,
503 unsigned long *trunks, bool use_priority_tags,
504 const struct bond *bond, const struct lacp *lacp,
505 bool floodable);
506static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
507 const struct netdev *netdev, const struct cfm *cfm,
0477baa9
DF
508 const struct bfd *bfd, const struct lldp *lldp,
509 int stp_port_no, const struct rstp_port *rstp_port,
84f0f298
RW
510 enum ofputil_port_config config,
511 enum ofputil_port_state state, bool is_tunnel,
512 bool may_enable);
513static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
514static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
515static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
516static void xlate_xbridge_copy(struct xbridge *);
517static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
518static void xlate_xport_copy(struct xbridge *, struct xbundle *,
519 struct xport *);
520static void xlate_xcfg_free(struct xlate_cfg *);
b256dc52 521
34dd0d78 522static inline void
c1b3756c 523xlate_report(struct xlate_ctx *ctx, const char *format, ...)
34dd0d78
JR
524{
525 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
c1b3756c
BP
526 va_list args;
527
528 va_start(args, format);
529 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
530 va_end(args);
34dd0d78
JR
531 }
532}
84f0f298 533
d6bef3cc
BP
534static inline void
535xlate_report_actions(struct xlate_ctx *ctx, const char *title,
536 const struct ofpact *ofpacts, size_t ofpacts_len)
537{
538 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
539 struct ds s = DS_EMPTY_INITIALIZER;
540 ofpacts_format(ofpacts, ofpacts_len, &s);
541 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
542 ds_destroy(&s);
543 }
544}
545
84f0f298
RW
546static void
547xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
548{
549 list_init(&xbridge->xbundles);
550 hmap_init(&xbridge->xports);
551 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
552 hash_pointer(xbridge->ofproto, 0));
553}
554
555static void
556xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
557{
558 list_init(&xbundle->xports);
559 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
560 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
561 hash_pointer(xbundle->ofbundle, 0));
562}
563
564static void
565xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
566{
567 hmap_init(&xport->skb_priorities);
568 hmap_insert(&xcfg->xports, &xport->hmap_node,
569 hash_pointer(xport->ofport, 0));
570 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
571 hash_ofp_port(xport->ofp_port));
572}
573
574static void
575xlate_xbridge_set(struct xbridge *xbridge,
576 struct dpif *dpif,
ec89fc6f 577 const struct mac_learning *ml, struct stp *stp,
9efd308e 578 struct rstp *rstp, const struct mcast_snooping *ms,
ec89fc6f 579 const struct mbridge *mbridge,
46c88433 580 const struct dpif_sflow *sflow,
ce3955be 581 const struct dpif_ipfix *ipfix,
2f47cdf4 582 const struct netflow *netflow,
4b97b70d 583 bool forward_bpdu, bool has_in_band,
b440dd8c 584 const struct dpif_backer_support *support)
46c88433 585{
46c88433
EJ
586 if (xbridge->ml != ml) {
587 mac_learning_unref(xbridge->ml);
588 xbridge->ml = mac_learning_ref(ml);
589 }
590
6d95c4e8
FL
591 if (xbridge->ms != ms) {
592 mcast_snooping_unref(xbridge->ms);
593 xbridge->ms = mcast_snooping_ref(ms);
594 }
595
46c88433
EJ
596 if (xbridge->mbridge != mbridge) {
597 mbridge_unref(xbridge->mbridge);
598 xbridge->mbridge = mbridge_ref(mbridge);
599 }
600
601 if (xbridge->sflow != sflow) {
602 dpif_sflow_unref(xbridge->sflow);
603 xbridge->sflow = dpif_sflow_ref(sflow);
604 }
605
606 if (xbridge->ipfix != ipfix) {
607 dpif_ipfix_unref(xbridge->ipfix);
608 xbridge->ipfix = dpif_ipfix_ref(ipfix);
609 }
610
9d189a50
EJ
611 if (xbridge->stp != stp) {
612 stp_unref(xbridge->stp);
613 xbridge->stp = stp_ref(stp);
614 }
615
9efd308e
DV
616 if (xbridge->rstp != rstp) {
617 rstp_unref(xbridge->rstp);
618 xbridge->rstp = rstp_ref(rstp);
619 }
620
ce3955be
EJ
621 if (xbridge->netflow != netflow) {
622 netflow_unref(xbridge->netflow);
623 xbridge->netflow = netflow_ref(netflow);
624 }
625
89a8a7f0 626 xbridge->dpif = dpif;
46c88433
EJ
627 xbridge->forward_bpdu = forward_bpdu;
628 xbridge->has_in_band = has_in_band;
b440dd8c 629 xbridge->support = *support;
46c88433
EJ
630}
631
84f0f298
RW
632static void
633xlate_xbundle_set(struct xbundle *xbundle,
634 enum port_vlan_mode vlan_mode, int vlan,
635 unsigned long *trunks, bool use_priority_tags,
636 const struct bond *bond, const struct lacp *lacp,
637 bool floodable)
638{
639 ovs_assert(xbundle->xbridge);
640
641 xbundle->vlan_mode = vlan_mode;
642 xbundle->vlan = vlan;
643 xbundle->trunks = trunks;
644 xbundle->use_priority_tags = use_priority_tags;
645 xbundle->floodable = floodable;
646
647 if (xbundle->bond != bond) {
648 bond_unref(xbundle->bond);
649 xbundle->bond = bond_ref(bond);
650 }
651
652 if (xbundle->lacp != lacp) {
653 lacp_unref(xbundle->lacp);
654 xbundle->lacp = lacp_ref(lacp);
655 }
656}
657
658static void
659xlate_xport_set(struct xport *xport, odp_port_t odp_port,
660 const struct netdev *netdev, const struct cfm *cfm,
0477baa9 661 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
f025bcb7 662 const struct rstp_port* rstp_port,
84f0f298
RW
663 enum ofputil_port_config config, enum ofputil_port_state state,
664 bool is_tunnel, bool may_enable)
665{
666 xport->config = config;
667 xport->state = state;
668 xport->stp_port_no = stp_port_no;
669 xport->is_tunnel = is_tunnel;
670 xport->may_enable = may_enable;
671 xport->odp_port = odp_port;
672
f025bcb7
JR
673 if (xport->rstp_port != rstp_port) {
674 rstp_port_unref(xport->rstp_port);
675 xport->rstp_port = rstp_port_ref(rstp_port);
676 }
677
84f0f298
RW
678 if (xport->cfm != cfm) {
679 cfm_unref(xport->cfm);
680 xport->cfm = cfm_ref(cfm);
681 }
682
683 if (xport->bfd != bfd) {
684 bfd_unref(xport->bfd);
685 xport->bfd = bfd_ref(bfd);
686 }
687
0477baa9
DF
688 if (xport->lldp != lldp) {
689 lldp_unref(xport->lldp);
690 xport->lldp = lldp_ref(lldp);
691 }
692
84f0f298
RW
693 if (xport->netdev != netdev) {
694 netdev_close(xport->netdev);
695 xport->netdev = netdev_ref(netdev);
696 }
697}
698
699static void
700xlate_xbridge_copy(struct xbridge *xbridge)
701{
702 struct xbundle *xbundle;
703 struct xport *xport;
704 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
705 new_xbridge->ofproto = xbridge->ofproto;
706 new_xbridge->name = xstrdup(xbridge->name);
707 xlate_xbridge_init(new_xcfg, new_xbridge);
708
709 xlate_xbridge_set(new_xbridge,
34dd0d78 710 xbridge->dpif, xbridge->ml, xbridge->stp,
9efd308e
DV
711 xbridge->rstp, xbridge->ms, xbridge->mbridge,
712 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
b440dd8c
JS
713 xbridge->forward_bpdu, xbridge->has_in_band,
714 &xbridge->support);
84f0f298
RW
715 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
716 xlate_xbundle_copy(new_xbridge, xbundle);
717 }
718
719 /* Copy xports which are not part of a xbundle */
720 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
721 if (!xport->xbundle) {
722 xlate_xport_copy(new_xbridge, NULL, xport);
723 }
724 }
725}
726
727static void
728xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
729{
730 struct xport *xport;
731 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
732 new_xbundle->ofbundle = xbundle->ofbundle;
733 new_xbundle->xbridge = xbridge;
734 new_xbundle->name = xstrdup(xbundle->name);
735 xlate_xbundle_init(new_xcfg, new_xbundle);
736
737 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
738 xbundle->vlan, xbundle->trunks,
739 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
740 xbundle->floodable);
741 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
742 xlate_xport_copy(xbridge, new_xbundle, xport);
743 }
744}
745
746static void
747xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
748 struct xport *xport)
749{
750 struct skb_priority_to_dscp *pdscp, *new_pdscp;
751 struct xport *new_xport = xzalloc(sizeof *xport);
752 new_xport->ofport = xport->ofport;
753 new_xport->ofp_port = xport->ofp_port;
754 new_xport->xbridge = xbridge;
755 xlate_xport_init(new_xcfg, new_xport);
756
757 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
0477baa9
DF
758 xport->bfd, xport->lldp, xport->stp_port_no,
759 xport->rstp_port, xport->config, xport->state,
760 xport->is_tunnel, xport->may_enable);
84f0f298
RW
761
762 if (xport->peer) {
763 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
764 if (peer) {
765 new_xport->peer = peer;
766 new_xport->peer->peer = new_xport;
767 }
768 }
769
770 if (xbundle) {
771 new_xport->xbundle = xbundle;
772 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
773 }
774
775 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
776 new_pdscp = xmalloc(sizeof *pdscp);
777 new_pdscp->skb_priority = pdscp->skb_priority;
778 new_pdscp->dscp = pdscp->dscp;
779 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
780 hash_int(new_pdscp->skb_priority, 0));
781 }
782}
783
784/* Sets the current xlate configuration to new_xcfg and frees the old xlate
785 * configuration in xcfgp.
786 *
787 * This needs to be called after editing the xlate configuration.
788 *
789 * Functions that edit the new xlate configuration are
790 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
791 *
792 * A sample workflow:
793 *
794 * xlate_txn_start();
795 * ...
796 * edit_xlate_configuration();
797 * ...
798 * xlate_txn_commit(); */
46c88433 799void
84f0f298
RW
800xlate_txn_commit(void)
801{
802 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
803
804 ovsrcu_set(&xcfgp, new_xcfg);
40a9c4c2
AW
805 ovsrcu_synchronize();
806 xlate_xcfg_free(xcfg);
84f0f298
RW
807 new_xcfg = NULL;
808}
809
810/* Copies the current xlate configuration in xcfgp to new_xcfg.
811 *
812 * This needs to be called prior to editing the xlate configuration. */
813void
814xlate_txn_start(void)
815{
816 struct xbridge *xbridge;
817 struct xlate_cfg *xcfg;
818
819 ovs_assert(!new_xcfg);
820
821 new_xcfg = xmalloc(sizeof *new_xcfg);
822 hmap_init(&new_xcfg->xbridges);
823 hmap_init(&new_xcfg->xbundles);
824 hmap_init(&new_xcfg->xports);
825
826 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
827 if (!xcfg) {
828 return;
829 }
830
831 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
832 xlate_xbridge_copy(xbridge);
833 }
834}
835
836
837static void
838xlate_xcfg_free(struct xlate_cfg *xcfg)
839{
840 struct xbridge *xbridge, *next_xbridge;
841
842 if (!xcfg) {
843 return;
844 }
845
846 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
847 xlate_xbridge_remove(xcfg, xbridge);
848 }
849
850 hmap_destroy(&xcfg->xbridges);
851 hmap_destroy(&xcfg->xbundles);
852 hmap_destroy(&xcfg->xports);
853 free(xcfg);
854}
855
856void
857xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
34dd0d78 858 struct dpif *dpif,
84f0f298 859 const struct mac_learning *ml, struct stp *stp,
9efd308e 860 struct rstp *rstp, const struct mcast_snooping *ms,
84f0f298
RW
861 const struct mbridge *mbridge,
862 const struct dpif_sflow *sflow,
863 const struct dpif_ipfix *ipfix,
2f47cdf4 864 const struct netflow *netflow,
b440dd8c
JS
865 bool forward_bpdu, bool has_in_band,
866 const struct dpif_backer_support *support)
84f0f298
RW
867{
868 struct xbridge *xbridge;
869
870 ovs_assert(new_xcfg);
871
872 xbridge = xbridge_lookup(new_xcfg, ofproto);
873 if (!xbridge) {
874 xbridge = xzalloc(sizeof *xbridge);
875 xbridge->ofproto = ofproto;
876
877 xlate_xbridge_init(new_xcfg, xbridge);
878 }
879
880 free(xbridge->name);
881 xbridge->name = xstrdup(name);
882
34dd0d78 883 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
b440dd8c 884 netflow, forward_bpdu, has_in_band, support);
84f0f298
RW
885}
886
887static void
888xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
46c88433 889{
46c88433
EJ
890 struct xbundle *xbundle, *next_xbundle;
891 struct xport *xport, *next_xport;
892
893 if (!xbridge) {
894 return;
895 }
896
897 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
84f0f298 898 xlate_xport_remove(xcfg, xport);
46c88433
EJ
899 }
900
901 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
84f0f298 902 xlate_xbundle_remove(xcfg, xbundle);
46c88433
EJ
903 }
904
84f0f298 905 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
795cc5c1 906 mac_learning_unref(xbridge->ml);
6d95c4e8 907 mcast_snooping_unref(xbridge->ms);
795cc5c1
EJ
908 mbridge_unref(xbridge->mbridge);
909 dpif_sflow_unref(xbridge->sflow);
910 dpif_ipfix_unref(xbridge->ipfix);
911 stp_unref(xbridge->stp);
9efd308e 912 rstp_unref(xbridge->rstp);
795cc5c1 913 hmap_destroy(&xbridge->xports);
46c88433
EJ
914 free(xbridge->name);
915 free(xbridge);
916}
917
84f0f298
RW
918void
919xlate_remove_ofproto(struct ofproto_dpif *ofproto)
920{
921 struct xbridge *xbridge;
922
923 ovs_assert(new_xcfg);
924
925 xbridge = xbridge_lookup(new_xcfg, ofproto);
926 xlate_xbridge_remove(new_xcfg, xbridge);
927}
928
46c88433
EJ
929void
930xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
931 const char *name, enum port_vlan_mode vlan_mode, int vlan,
932 unsigned long *trunks, bool use_priority_tags,
933 const struct bond *bond, const struct lacp *lacp,
934 bool floodable)
935{
84f0f298 936 struct xbundle *xbundle;
46c88433 937
84f0f298
RW
938 ovs_assert(new_xcfg);
939
940 xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
941 if (!xbundle) {
942 xbundle = xzalloc(sizeof *xbundle);
943 xbundle->ofbundle = ofbundle;
84f0f298 944 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433 945
84f0f298 946 xlate_xbundle_init(new_xcfg, xbundle);
46c88433
EJ
947 }
948
46c88433
EJ
949 free(xbundle->name);
950 xbundle->name = xstrdup(name);
951
84f0f298
RW
952 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
953 use_priority_tags, bond, lacp, floodable);
46c88433
EJ
954}
955
84f0f298
RW
956static void
957xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
46c88433 958{
5f03c983 959 struct xport *xport;
46c88433
EJ
960
961 if (!xbundle) {
962 return;
963 }
964
5f03c983 965 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
46c88433
EJ
966 xport->xbundle = NULL;
967 }
968
84f0f298 969 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
46c88433
EJ
970 list_remove(&xbundle->list_node);
971 bond_unref(xbundle->bond);
972 lacp_unref(xbundle->lacp);
973 free(xbundle->name);
974 free(xbundle);
975}
976
84f0f298
RW
977void
978xlate_bundle_remove(struct ofbundle *ofbundle)
979{
980 struct xbundle *xbundle;
981
982 ovs_assert(new_xcfg);
983
984 xbundle = xbundle_lookup(new_xcfg, ofbundle);
985 xlate_xbundle_remove(new_xcfg, xbundle);
986}
987
46c88433
EJ
988void
989xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
990 struct ofport_dpif *ofport, ofp_port_t ofp_port,
991 odp_port_t odp_port, const struct netdev *netdev,
992 const struct cfm *cfm, const struct bfd *bfd,
0477baa9
DF
993 const struct lldp *lldp, struct ofport_dpif *peer,
994 int stp_port_no, const struct rstp_port *rstp_port,
55954f6e 995 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
dd8cd4b4
SH
996 enum ofputil_port_config config,
997 enum ofputil_port_state state, bool is_tunnel,
9d189a50 998 bool may_enable)
46c88433 999{
55954f6e 1000 size_t i;
84f0f298
RW
1001 struct xport *xport;
1002
1003 ovs_assert(new_xcfg);
46c88433 1004
84f0f298 1005 xport = xport_lookup(new_xcfg, ofport);
46c88433
EJ
1006 if (!xport) {
1007 xport = xzalloc(sizeof *xport);
1008 xport->ofport = ofport;
84f0f298 1009 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433
EJ
1010 xport->ofp_port = ofp_port;
1011
84f0f298 1012 xlate_xport_init(new_xcfg, xport);
46c88433
EJ
1013 }
1014
1015 ovs_assert(xport->ofp_port == ofp_port);
1016
0477baa9
DF
1017 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1018 stp_port_no, rstp_port, config, state, is_tunnel,
1019 may_enable);
46c88433
EJ
1020
1021 if (xport->peer) {
1022 xport->peer->peer = NULL;
1023 }
84f0f298 1024 xport->peer = xport_lookup(new_xcfg, peer);
46c88433
EJ
1025 if (xport->peer) {
1026 xport->peer->peer = xport;
1027 }
1028
1029 if (xport->xbundle) {
1030 list_remove(&xport->bundle_node);
1031 }
84f0f298 1032 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
1033 if (xport->xbundle) {
1034 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1035 }
55954f6e
EJ
1036
1037 clear_skb_priorities(xport);
1038 for (i = 0; i < n_qdscp; i++) {
1039 struct skb_priority_to_dscp *pdscp;
1040 uint32_t skb_priority;
1041
89a8a7f0
EJ
1042 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1043 &skb_priority)) {
55954f6e
EJ
1044 continue;
1045 }
1046
1047 pdscp = xmalloc(sizeof *pdscp);
1048 pdscp->skb_priority = skb_priority;
1049 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1050 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1051 hash_int(pdscp->skb_priority, 0));
1052 }
46c88433
EJ
1053}
1054
84f0f298
RW
1055static void
1056xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
46c88433 1057{
46c88433
EJ
1058 if (!xport) {
1059 return;
1060 }
1061
1062 if (xport->peer) {
1063 xport->peer->peer = NULL;
1064 xport->peer = NULL;
1065 }
1066
e621a12d
EJ
1067 if (xport->xbundle) {
1068 list_remove(&xport->bundle_node);
1069 }
1070
55954f6e
EJ
1071 clear_skb_priorities(xport);
1072 hmap_destroy(&xport->skb_priorities);
1073
84f0f298 1074 hmap_remove(&xcfg->xports, &xport->hmap_node);
46c88433
EJ
1075 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1076
1077 netdev_close(xport->netdev);
f025bcb7 1078 rstp_port_unref(xport->rstp_port);
46c88433
EJ
1079 cfm_unref(xport->cfm);
1080 bfd_unref(xport->bfd);
0477baa9 1081 lldp_unref(xport->lldp);
46c88433
EJ
1082 free(xport);
1083}
1084
84f0f298
RW
1085void
1086xlate_ofport_remove(struct ofport_dpif *ofport)
1087{
1088 struct xport *xport;
1089
1090 ovs_assert(new_xcfg);
1091
1092 xport = xport_lookup(new_xcfg, ofport);
1093 xlate_xport_remove(new_xcfg, xport);
1094}
1095
ef377a58
JR
1096static struct ofproto_dpif *
1097xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1098 ofp_port_t *ofp_in_port, const struct xport **xportp)
1099{
e672ff9b 1100 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
ef377a58 1101 const struct xport *xport;
f9038ef6 1102
e672ff9b
JR
1103 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1104 ? tnl_port_receive(flow)
1105 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1106 if (OVS_UNLIKELY(!xport)) {
1107 return NULL;
ef377a58 1108 }
e672ff9b 1109 *xportp = xport;
f9038ef6 1110 if (ofp_in_port) {
e672ff9b 1111 *ofp_in_port = xport->ofp_port;
f9038ef6 1112 }
e672ff9b 1113 return xport->xbridge->ofproto;
ef377a58
JR
1114}
1115
1116/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1117 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1118struct ofproto_dpif *
1119xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1120 ofp_port_t *ofp_in_port)
1121{
1122 const struct xport *xport;
1123
1124 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1125}
1126
cc377352 1127/* Given a datapath and flow metadata ('backer', and 'flow' respectively),
ef377a58 1128 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
cc377352 1129 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
dcc2c6cd
JR
1130 * handles for those protocols if they're enabled. Caller may use the returned
1131 * pointers until quiescing, for longer term use additional references must
1132 * be taken.
8449c4d6 1133 *
f9038ef6 1134 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
ef377a58 1135 */
8449c4d6 1136int
5c476ea3
JR
1137xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1138 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1139 struct dpif_sflow **sflow, struct netflow **netflow,
1140 ofp_port_t *ofp_in_port)
8449c4d6 1141{
ef377a58 1142 struct ofproto_dpif *ofproto;
84f0f298 1143 const struct xport *xport;
8449c4d6 1144
ef377a58 1145 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
8449c4d6 1146
f9038ef6 1147 if (!ofproto) {
cc377352 1148 return ENODEV;
8449c4d6 1149 }
8449c4d6 1150
ef377a58
JR
1151 if (ofprotop) {
1152 *ofprotop = ofproto;
8449c4d6
EJ
1153 }
1154
1dfdb9b3 1155 if (ipfix) {
f9038ef6 1156 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1dfdb9b3
EJ
1157 }
1158
1159 if (sflow) {
f9038ef6 1160 *sflow = xport ? xport->xbridge->sflow : NULL;
1dfdb9b3
EJ
1161 }
1162
1163 if (netflow) {
f9038ef6 1164 *netflow = xport ? xport->xbridge->netflow : NULL;
1dfdb9b3 1165 }
f9038ef6 1166
cc377352 1167 return 0;
8449c4d6
EJ
1168}
1169
46c88433 1170static struct xbridge *
84f0f298 1171xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
46c88433 1172{
84f0f298 1173 struct hmap *xbridges;
46c88433
EJ
1174 struct xbridge *xbridge;
1175
84f0f298 1176 if (!ofproto || !xcfg) {
5e6af486
EJ
1177 return NULL;
1178 }
1179
84f0f298
RW
1180 xbridges = &xcfg->xbridges;
1181
46c88433 1182 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
84f0f298 1183 xbridges) {
46c88433
EJ
1184 if (xbridge->ofproto == ofproto) {
1185 return xbridge;
1186 }
1187 }
1188 return NULL;
1189}
1190
1191static struct xbundle *
84f0f298 1192xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
46c88433 1193{
84f0f298 1194 struct hmap *xbundles;
46c88433
EJ
1195 struct xbundle *xbundle;
1196
84f0f298 1197 if (!ofbundle || !xcfg) {
5e6af486
EJ
1198 return NULL;
1199 }
1200
84f0f298
RW
1201 xbundles = &xcfg->xbundles;
1202
46c88433 1203 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
84f0f298 1204 xbundles) {
46c88433
EJ
1205 if (xbundle->ofbundle == ofbundle) {
1206 return xbundle;
1207 }
1208 }
1209 return NULL;
1210}
1211
1212static struct xport *
84f0f298 1213xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
46c88433 1214{
84f0f298 1215 struct hmap *xports;
46c88433
EJ
1216 struct xport *xport;
1217
84f0f298 1218 if (!ofport || !xcfg) {
5e6af486
EJ
1219 return NULL;
1220 }
1221
84f0f298
RW
1222 xports = &xcfg->xports;
1223
46c88433 1224 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
84f0f298 1225 xports) {
46c88433
EJ
1226 if (xport->ofport == ofport) {
1227 return xport;
1228 }
1229 }
1230 return NULL;
1231}
1232
40085e56
EJ
1233static struct stp_port *
1234xport_get_stp_port(const struct xport *xport)
1235{
92cf817b 1236 return xport->xbridge->stp && xport->stp_port_no != -1
40085e56
EJ
1237 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1238 : NULL;
1239}
9d189a50 1240
0d1cee12 1241static bool
9d189a50
EJ
1242xport_stp_learn_state(const struct xport *xport)
1243{
40085e56 1244 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1245 return sp
1246 ? stp_learn_in_state(stp_port_get_state(sp))
1247 : true;
9d189a50
EJ
1248}
1249
1250static bool
1251xport_stp_forward_state(const struct xport *xport)
1252{
40085e56 1253 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1254 return sp
1255 ? stp_forward_in_state(stp_port_get_state(sp))
1256 : true;
9d189a50
EJ
1257}
1258
0d1cee12 1259static bool
bacdb85a 1260xport_stp_should_forward_bpdu(const struct xport *xport)
0d1cee12
K
1261{
1262 struct stp_port *sp = xport_get_stp_port(xport);
bacdb85a 1263 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
0d1cee12
K
1264}
1265
9d189a50
EJ
1266/* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1267 * were used to make the determination.*/
1268static bool
1269stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1270{
bbbca389 1271 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
9d189a50 1272 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
bbbca389 1273 return is_stp(flow);
9d189a50
EJ
1274}
1275
1276static void
cf62fa4c 1277stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9d189a50 1278{
40085e56 1279 struct stp_port *sp = xport_get_stp_port(xport);
cf62fa4c
PS
1280 struct dp_packet payload = *packet;
1281 struct eth_header *eth = dp_packet_data(&payload);
9d189a50
EJ
1282
1283 /* Sink packets on ports that have STP disabled when the bridge has
1284 * STP enabled. */
1285 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1286 return;
1287 }
1288
1289 /* Trim off padding on payload. */
cf62fa4c
PS
1290 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1291 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9d189a50
EJ
1292 }
1293
cf62fa4c
PS
1294 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1295 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
9d189a50
EJ
1296 }
1297}
1298
f025bcb7
JR
1299static enum rstp_state
1300xport_get_rstp_port_state(const struct xport *xport)
9efd308e 1301{
f025bcb7
JR
1302 return xport->rstp_port
1303 ? rstp_port_get_state(xport->rstp_port)
1304 : RSTP_DISABLED;
9efd308e
DV
1305}
1306
1307static bool
1308xport_rstp_learn_state(const struct xport *xport)
1309{
4b5f1996
DV
1310 return xport->xbridge->rstp && xport->rstp_port
1311 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1312 : true;
9efd308e
DV
1313}
1314
1315static bool
1316xport_rstp_forward_state(const struct xport *xport)
1317{
4b5f1996
DV
1318 return xport->xbridge->rstp && xport->rstp_port
1319 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1320 : true;
9efd308e
DV
1321}
1322
1323static bool
1324xport_rstp_should_manage_bpdu(const struct xport *xport)
1325{
f025bcb7 1326 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
9efd308e
DV
1327}
1328
1329static void
cf62fa4c 1330rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9efd308e 1331{
cf62fa4c
PS
1332 struct dp_packet payload = *packet;
1333 struct eth_header *eth = dp_packet_data(&payload);
9efd308e 1334
f025bcb7
JR
1335 /* Sink packets on ports that have no RSTP. */
1336 if (!xport->rstp_port) {
9efd308e
DV
1337 return;
1338 }
1339
1340 /* Trim off padding on payload. */
cf62fa4c
PS
1341 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1342 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9efd308e
DV
1343 }
1344
cf62fa4c
PS
1345 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1346 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1347 dp_packet_size(&payload));
9efd308e
DV
1348 }
1349}
1350
46c88433
EJ
1351static struct xport *
1352get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1353{
1354 struct xport *xport;
1355
1356 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1357 &xbridge->xports) {
1358 if (xport->ofp_port == ofp_port) {
1359 return xport;
1360 }
1361 }
1362 return NULL;
1363}
1364
1365static odp_port_t
1366ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1367{
1368 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1369 return xport ? xport->odp_port : ODPP_NONE;
1370}
1371
dd8cd4b4
SH
1372static bool
1373odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1374{
086fa873
BP
1375 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1376 return xport && xport->may_enable;
dd8cd4b4
SH
1377}
1378
1e684d7d 1379static struct ofputil_bucket *
dd8cd4b4
SH
1380group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1381 int depth);
1382
1383static bool
1384group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1385{
1386 struct group_dpif *group;
dd8cd4b4 1387
dc25893e
AZ
1388 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1389 struct ofputil_bucket *bucket;
dd8cd4b4 1390
dc25893e
AZ
1391 bucket = group_first_live_bucket(ctx, group, depth);
1392 group_dpif_unref(group);
1393 return bucket == NULL;
1394 }
dd8cd4b4 1395
dc25893e 1396 return false;
dd8cd4b4
SH
1397}
1398
1399#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1400
1401static bool
1402bucket_is_alive(const struct xlate_ctx *ctx,
1e684d7d 1403 struct ofputil_bucket *bucket, int depth)
dd8cd4b4
SH
1404{
1405 if (depth >= MAX_LIVENESS_RECURSION) {
1406 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1407
1408 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1409 MAX_LIVENESS_RECURSION);
1410 return false;
1411 }
1412
fdb1999b
AZ
1413 return (!ofputil_bucket_has_liveness(bucket)
1414 || (bucket->watch_port != OFPP_ANY
1415 && odp_port_is_alive(ctx, bucket->watch_port))
1416 || (bucket->watch_group != OFPG_ANY
1417 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
dd8cd4b4
SH
1418}
1419
1e684d7d 1420static struct ofputil_bucket *
dd8cd4b4
SH
1421group_first_live_bucket(const struct xlate_ctx *ctx,
1422 const struct group_dpif *group, int depth)
1423{
1424 struct ofputil_bucket *bucket;
ca6ba700 1425 const struct ovs_list *buckets;
dd8cd4b4
SH
1426
1427 group_dpif_get_buckets(group, &buckets);
1428 LIST_FOR_EACH (bucket, list_node, buckets) {
1429 if (bucket_is_alive(ctx, bucket, depth)) {
1430 return bucket;
1431 }
1432 }
1433
1434 return NULL;
1435}
1436
1e684d7d 1437static struct ofputil_bucket *
fe7e5749
SH
1438group_best_live_bucket(const struct xlate_ctx *ctx,
1439 const struct group_dpif *group,
1440 uint32_t basis)
1441{
1e684d7d 1442 struct ofputil_bucket *best_bucket = NULL;
fe7e5749
SH
1443 uint32_t best_score = 0;
1444 int i = 0;
1445
1e684d7d 1446 struct ofputil_bucket *bucket;
ca6ba700 1447 const struct ovs_list *buckets;
fe7e5749
SH
1448
1449 group_dpif_get_buckets(group, &buckets);
1450 LIST_FOR_EACH (bucket, list_node, buckets) {
1451 if (bucket_is_alive(ctx, bucket, 0)) {
7cb279c2 1452 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
fe7e5749
SH
1453 if (score >= best_score) {
1454 best_bucket = bucket;
1455 best_score = score;
1456 }
1457 }
1458 i++;
1459 }
1460
1461 return best_bucket;
1462}
1463
9583bc14 1464static bool
46c88433 1465xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
9583bc14
EJ
1466{
1467 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1468 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1469}
1470
1471static bool
46c88433
EJ
1472xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1473{
1474 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1475}
1476
1477static mirror_mask_t
1478xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1479{
1480 return xbundle != &ofpp_none_bundle
1481 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1482 : 0;
1483}
1484
1485static mirror_mask_t
1486xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1487{
46c88433
EJ
1488 return xbundle != &ofpp_none_bundle
1489 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1490 : 0;
9583bc14
EJ
1491}
1492
46c88433
EJ
1493static mirror_mask_t
1494xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1495{
46c88433
EJ
1496 return xbundle != &ofpp_none_bundle
1497 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1498 : 0;
1499}
1500
1501static struct xbundle *
1502lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1503 bool warn, struct xport **in_xportp)
1504{
1505 struct xport *xport;
9583bc14
EJ
1506
1507 /* Find the port and bundle for the received packet. */
46c88433
EJ
1508 xport = get_ofp_port(xbridge, in_port);
1509 if (in_xportp) {
1510 *in_xportp = xport;
9583bc14 1511 }
46c88433
EJ
1512 if (xport && xport->xbundle) {
1513 return xport->xbundle;
9583bc14
EJ
1514 }
1515
6362203b
YT
1516 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1517 * which a controller may use as the ingress port for traffic that
1518 * it is sourcing. */
1519 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
9583bc14
EJ
1520 return &ofpp_none_bundle;
1521 }
1522
1523 /* Odd. A few possible reasons here:
1524 *
1525 * - We deleted a port but there are still a few packets queued up
1526 * from it.
1527 *
1528 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1529 * we don't know about.
1530 *
1531 * - The ofproto client didn't configure the port as part of a bundle.
1532 * This is particularly likely to happen if a packet was received on the
1533 * port after it was created, but before the client had a chance to
1534 * configure its bundle.
1535 */
1536 if (warn) {
1537 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1538
1539 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
46c88433 1540 "port %"PRIu16, xbridge->name, in_port);
9583bc14
EJ
1541 }
1542 return NULL;
1543}
1544
1545static void
7efbc3b7
BP
1546mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1547 mirror_mask_t mirrors)
9583bc14 1548{
7efbc3b7
BP
1549 bool warn = ctx->xin->packet != NULL;
1550 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1551 if (!input_vid_is_valid(vid, xbundle, warn)) {
9583bc14
EJ
1552 return;
1553 }
7efbc3b7 1554 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
9583bc14 1555
7efbc3b7 1556 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 1557
7efbc3b7
BP
1558 /* Don't mirror to destinations that we've already mirrored to. */
1559 mirrors &= ~ctx->mirrors;
9583bc14
EJ
1560 if (!mirrors) {
1561 return;
1562 }
1563
7efbc3b7
BP
1564 /* Record these mirrors so that we don't mirror to them again. */
1565 ctx->mirrors |= mirrors;
1566
1567 if (ctx->xin->resubmit_stats) {
1568 mirror_update_stats(xbridge->mbridge, mirrors,
1569 ctx->xin->resubmit_stats->n_packets,
1570 ctx->xin->resubmit_stats->n_bytes);
1571 }
1572 if (ctx->xin->xcache) {
1573 struct xc_entry *entry;
1574
1575 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1576 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1577 entry->u.mirror.mirrors = mirrors;
1578 }
9583bc14
EJ
1579
1580 while (mirrors) {
7efbc3b7 1581 const unsigned long *vlans;
ec7ceaed
EJ
1582 mirror_mask_t dup_mirrors;
1583 struct ofbundle *out;
ec7ceaed
EJ
1584 int out_vlan;
1585
7efbc3b7
BP
1586 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1587 &vlans, &dup_mirrors, &out, &out_vlan);
ec7ceaed
EJ
1588 ovs_assert(has_mirror);
1589
1590 if (vlans) {
49a73e0c 1591 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
9583bc14
EJ
1592 }
1593
7efbc3b7 1594 if (vlans && !bitmap_is_set(vlans, vlan)) {
9583bc14
EJ
1595 mirrors = zero_rightmost_1bit(mirrors);
1596 continue;
1597 }
1598
ec7ceaed 1599 mirrors &= ~dup_mirrors;
3d6151f3 1600 ctx->mirrors |= dup_mirrors;
ec7ceaed 1601 if (out) {
84f0f298
RW
1602 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1603 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
46c88433
EJ
1604 if (out_xbundle) {
1605 output_normal(ctx, out_xbundle, vlan);
1606 }
ec7ceaed 1607 } else if (vlan != out_vlan
7efbc3b7 1608 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
46c88433 1609 struct xbundle *xbundle;
9583bc14 1610
46c88433
EJ
1611 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1612 if (xbundle_includes_vlan(xbundle, out_vlan)
1613 && !xbundle_mirror_out(xbridge, xbundle)) {
1614 output_normal(ctx, xbundle, out_vlan);
9583bc14
EJ
1615 }
1616 }
1617 }
1618 }
1619}
1620
7efbc3b7
BP
1621static void
1622mirror_ingress_packet(struct xlate_ctx *ctx)
1623{
1624 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1625 bool warn = ctx->xin->packet != NULL;
1626 struct xbundle *xbundle = lookup_input_bundle(
1627 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1628 if (xbundle) {
1629 mirror_packet(ctx, xbundle,
1630 xbundle_mirror_src(ctx->xbridge, xbundle));
1631 }
1632 }
1633}
1634
9583bc14 1635/* Given 'vid', the VID obtained from the 802.1Q header that was received as
46c88433 1636 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
9583bc14
EJ
1637 * the bundle on which the packet was received, returns the VLAN to which the
1638 * packet belongs.
1639 *
1640 * Both 'vid' and the return value are in the range 0...4095. */
1641static uint16_t
46c88433 1642input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
9583bc14 1643{
46c88433 1644 switch (in_xbundle->vlan_mode) {
9583bc14 1645 case PORT_VLAN_ACCESS:
46c88433 1646 return in_xbundle->vlan;
9583bc14
EJ
1647 break;
1648
1649 case PORT_VLAN_TRUNK:
1650 return vid;
1651
1652 case PORT_VLAN_NATIVE_UNTAGGED:
1653 case PORT_VLAN_NATIVE_TAGGED:
46c88433 1654 return vid ? vid : in_xbundle->vlan;
9583bc14
EJ
1655
1656 default:
428b2edd 1657 OVS_NOT_REACHED();
9583bc14
EJ
1658 }
1659}
1660
46c88433 1661/* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
9583bc14
EJ
1662 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1663 * a warning.
1664 *
1665 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1666 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1667 * 0...4095. */
1668static bool
46c88433 1669input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
9583bc14
EJ
1670{
1671 /* Allow any VID on the OFPP_NONE port. */
46c88433 1672 if (in_xbundle == &ofpp_none_bundle) {
9583bc14
EJ
1673 return true;
1674 }
1675
46c88433 1676 switch (in_xbundle->vlan_mode) {
9583bc14
EJ
1677 case PORT_VLAN_ACCESS:
1678 if (vid) {
1679 if (warn) {
1680 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1681 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
9583bc14 1682 "packet received on port %s configured as VLAN "
46c88433
EJ
1683 "%"PRIu16" access port", vid, in_xbundle->name,
1684 in_xbundle->vlan);
9583bc14
EJ
1685 }
1686 return false;
1687 }
1688 return true;
1689
1690 case PORT_VLAN_NATIVE_UNTAGGED:
1691 case PORT_VLAN_NATIVE_TAGGED:
1692 if (!vid) {
1693 /* Port must always carry its native VLAN. */
1694 return true;
1695 }
1696 /* Fall through. */
1697 case PORT_VLAN_TRUNK:
46c88433 1698 if (!xbundle_includes_vlan(in_xbundle, vid)) {
9583bc14
EJ
1699 if (warn) {
1700 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1701 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
9583bc14 1702 "received on port %s not configured for trunking "
46c88433 1703 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
9583bc14
EJ
1704 }
1705 return false;
1706 }
1707 return true;
1708
1709 default:
428b2edd 1710 OVS_NOT_REACHED();
9583bc14
EJ
1711 }
1712
1713}
1714
1715/* Given 'vlan', the VLAN that a packet belongs to, and
46c88433 1716 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
9583bc14
EJ
1717 * that should be included in the 802.1Q header. (If the return value is 0,
1718 * then the 802.1Q header should only be included in the packet if there is a
1719 * nonzero PCP.)
1720 *
1721 * Both 'vlan' and the return value are in the range 0...4095. */
1722static uint16_t
46c88433 1723output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
9583bc14 1724{
46c88433 1725 switch (out_xbundle->vlan_mode) {
9583bc14
EJ
1726 case PORT_VLAN_ACCESS:
1727 return 0;
1728
1729 case PORT_VLAN_TRUNK:
1730 case PORT_VLAN_NATIVE_TAGGED:
1731 return vlan;
1732
1733 case PORT_VLAN_NATIVE_UNTAGGED:
46c88433 1734 return vlan == out_xbundle->vlan ? 0 : vlan;
9583bc14
EJ
1735
1736 default:
428b2edd 1737 OVS_NOT_REACHED();
9583bc14
EJ
1738 }
1739}
1740
1741static void
46c88433 1742output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
9583bc14
EJ
1743 uint16_t vlan)
1744{
33bf9176 1745 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
9583bc14
EJ
1746 uint16_t vid;
1747 ovs_be16 tci, old_tci;
46c88433 1748 struct xport *xport;
e93ef1c7
JR
1749 struct xlate_bond_recirc xr;
1750 bool use_recirc = false;
9583bc14 1751
46c88433
EJ
1752 vid = output_vlan_to_vid(out_xbundle, vlan);
1753 if (list_is_empty(&out_xbundle->xports)) {
1754 /* Partially configured bundle with no slaves. Drop the packet. */
1755 return;
1756 } else if (!out_xbundle->bond) {
1757 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1758 bundle_node);
9583bc14 1759 } else {
84f0f298 1760 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
49a73e0c 1761 struct flow_wildcards *wc = ctx->wc;
84f0f298 1762 struct ofport_dpif *ofport;
adcf00ba 1763
2494ccd7 1764 if (ctx->xbridge->support.odp.recirc) {
e93ef1c7
JR
1765 use_recirc = bond_may_recirc(
1766 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
adcf00ba 1767
e93ef1c7 1768 if (use_recirc) {
adcf00ba 1769 /* Only TCP mode uses recirculation. */
e93ef1c7 1770 xr.hash_alg = OVS_HASH_ALG_L4;
adcf00ba 1771 bond_update_post_recirc_rules(out_xbundle->bond, false);
54ecb5a2
AZ
1772
1773 /* Recirculation does not require unmasking hash fields. */
1774 wc = NULL;
adcf00ba
AZ
1775 }
1776 }
46c88433 1777
54ecb5a2
AZ
1778 ofport = bond_choose_output_slave(out_xbundle->bond,
1779 &ctx->xin->flow, wc, vid);
84f0f298 1780 xport = xport_lookup(xcfg, ofport);
46c88433
EJ
1781
1782 if (!xport) {
9583bc14
EJ
1783 /* No slaves enabled, so drop packet. */
1784 return;
1785 }
d6fc5f57 1786
e93ef1c7 1787 /* If use_recirc is set, the main thread will handle stats
b256dc52 1788 * accounting for this bond. */
e93ef1c7 1789 if (!use_recirc) {
b256dc52
JS
1790 if (ctx->xin->resubmit_stats) {
1791 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1792 ctx->xin->resubmit_stats->n_bytes);
1793 }
1794 if (ctx->xin->xcache) {
1795 struct xc_entry *entry;
1796 struct flow *flow;
1797
1798 flow = &ctx->xin->flow;
1799 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1800 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1801 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1802 entry->u.bond.vid = vid;
1803 }
d6fc5f57 1804 }
9583bc14
EJ
1805 }
1806
33bf9176 1807 old_tci = *flow_tci;
9583bc14 1808 tci = htons(vid);
46c88433 1809 if (tci || out_xbundle->use_priority_tags) {
33bf9176 1810 tci |= *flow_tci & htons(VLAN_PCP_MASK);
9583bc14
EJ
1811 if (tci) {
1812 tci |= htons(VLAN_CFI);
1813 }
1814 }
33bf9176 1815 *flow_tci = tci;
9583bc14 1816
e93ef1c7 1817 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
33bf9176 1818 *flow_tci = old_tci;
9583bc14
EJ
1819}
1820
1821/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1822 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1823 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1824static bool
1825is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1826{
1827 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1828 return false;
1829 }
1830
1831 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1832 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1833 return false;
1834 }
1835
1836 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1837 if (flow->nw_proto == ARP_OP_REPLY) {
1838 return true;
1839 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1840 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1841 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1842
1843 return flow->nw_src == flow->nw_dst;
1844 } else {
1845 return false;
1846 }
1847}
1848
ff69c24a
FL
1849/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1850 * dropped. Returns true if they may be forwarded, false if they should be
1851 * dropped.
1852 *
1853 * 'in_port' must be the xport that corresponds to flow->in_port.
1854 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1855 *
1856 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1857 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1858 * checked by input_vid_is_valid().
1859 *
1860 * May also add tags to '*tags', although the current implementation only does
1861 * so in one special case.
1862 */
1863static bool
1864is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1865 uint16_t vlan)
1866{
1867 struct xbundle *in_xbundle = in_port->xbundle;
1868 const struct xbridge *xbridge = ctx->xbridge;
1869 struct flow *flow = &ctx->xin->flow;
1870
1871 /* Drop frames for reserved multicast addresses
1872 * only if forward_bpdu option is absent. */
1873 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1874 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1875 return false;
1876 }
1877
1878 if (in_xbundle->bond) {
1879 struct mac_entry *mac;
1880
1881 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1882 flow->dl_dst)) {
1883 case BV_ACCEPT:
1884 break;
1885
1886 case BV_DROP:
1887 xlate_report(ctx, "bonding refused admissibility, dropping");
1888 return false;
1889
1890 case BV_DROP_IF_MOVED:
1891 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1892 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
9d078ec2
BP
1893 if (mac
1894 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
49a73e0c 1895 && (!is_gratuitous_arp(flow, ctx->wc)
9d078ec2 1896 || mac_entry_is_grat_arp_locked(mac))) {
ff69c24a
FL
1897 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1898 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1899 "dropping");
1900 return false;
1901 }
1902 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1903 break;
1904 }
1905 }
1906
1907 return true;
1908}
1909
ee047520
BP
1910/* Checks whether a MAC learning update is necessary for MAC learning table
1911 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1912 * 'vlan'.
1913 *
1914 * Most packets processed through the MAC learning table do not actually
1915 * change it in any way. This function requires only a read lock on the MAC
1916 * learning table, so it is much cheaper in this common case.
1917 *
1918 * Keep the code here synchronized with that in update_learning_table__()
1919 * below. */
1920static bool
1921is_mac_learning_update_needed(const struct mac_learning *ml,
1922 const struct flow *flow,
1923 struct flow_wildcards *wc,
1924 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1925OVS_REQ_RDLOCK(ml->rwlock)
9583bc14
EJ
1926{
1927 struct mac_entry *mac;
1928
ee047520
BP
1929 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1930 return false;
1931 }
1932
1933 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1934 if (!mac || mac_entry_age(ml, mac)) {
1935 return true;
9583bc14
EJ
1936 }
1937
ee047520
BP
1938 if (is_gratuitous_arp(flow, wc)) {
1939 /* We don't want to learn from gratuitous ARP packets that are
1940 * reflected back over bond slaves so we lock the learning table. */
1941 if (!in_xbundle->bond) {
1942 return true;
1943 } else if (mac_entry_is_grat_arp_locked(mac)) {
1944 return false;
1945 }
1946 }
1947
9d078ec2 1948 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
ee047520
BP
1949}
1950
1951
1952/* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1953 * received on 'in_xbundle' in 'vlan'.
1954 *
1955 * This code repeats all the checks in is_mac_learning_update_needed() because
1956 * the lock was released between there and here and thus the MAC learning state
1957 * could have changed.
1958 *
1959 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1960 * above. */
1961static void
1962update_learning_table__(const struct xbridge *xbridge,
1963 const struct flow *flow, struct flow_wildcards *wc,
1964 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1965OVS_REQ_WRLOCK(xbridge->ml->rwlock)
ee047520
BP
1966{
1967 struct mac_entry *mac;
1968
46c88433 1969 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
ee047520 1970 return;
9583bc14
EJ
1971 }
1972
46c88433 1973 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
9583bc14
EJ
1974 if (is_gratuitous_arp(flow, wc)) {
1975 /* We don't want to learn from gratuitous ARP packets that are
1976 * reflected back over bond slaves so we lock the learning table. */
46c88433 1977 if (!in_xbundle->bond) {
9583bc14
EJ
1978 mac_entry_set_grat_arp_lock(mac);
1979 } else if (mac_entry_is_grat_arp_locked(mac)) {
ee047520 1980 return;
9583bc14
EJ
1981 }
1982 }
1983
9d078ec2 1984 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
9583bc14
EJ
1985 /* The log messages here could actually be useful in debugging,
1986 * so keep the rate limit relatively high. */
1987 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
ee047520 1988
9583bc14
EJ
1989 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1990 "on port %s in VLAN %d",
46c88433
EJ
1991 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1992 in_xbundle->name, vlan);
9583bc14 1993
9d078ec2 1994 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
9583bc14 1995 }
ee047520
BP
1996}
1997
1998static void
1999update_learning_table(const struct xbridge *xbridge,
2000 const struct flow *flow, struct flow_wildcards *wc,
2001 int vlan, struct xbundle *in_xbundle)
2002{
2003 bool need_update;
2004
2005 /* Don't learn the OFPP_NONE port. */
2006 if (in_xbundle == &ofpp_none_bundle) {
2007 return;
2008 }
2009
2010 /* First try the common case: no change to MAC learning table. */
2011 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2012 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2013 in_xbundle);
509c0149 2014 ovs_rwlock_unlock(&xbridge->ml->rwlock);
ee047520
BP
2015
2016 if (need_update) {
2017 /* Slow path: MAC learning table might need an update. */
2018 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2019 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2020 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2021 }
9583bc14
EJ
2022}
2023
86e2dcdd
FL
2024/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2025 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2026static void
06994f87
TLSC
2027update_mcast_snooping_table4__(const struct xbridge *xbridge,
2028 const struct flow *flow,
2029 struct mcast_snooping *ms, int vlan,
2030 struct xbundle *in_xbundle,
2031 const struct dp_packet *packet)
86e2dcdd
FL
2032 OVS_REQ_WRLOCK(ms->rwlock)
2033{
2034 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
e3102e42 2035 int count;
06994f87 2036 ovs_be32 ip4 = flow->igmp_group_ip4;
86e2dcdd
FL
2037
2038 switch (ntohs(flow->tp_src)) {
2039 case IGMP_HOST_MEMBERSHIP_REPORT:
2040 case IGMPV2_HOST_MEMBERSHIP_REPORT:
964a4d5f 2041 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
86e2dcdd
FL
2042 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2043 IP_FMT" is on port %s in VLAN %d",
2044 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2045 }
2046 break;
2047 case IGMP_HOST_LEAVE_MESSAGE:
964a4d5f 2048 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
86e2dcdd
FL
2049 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2050 IP_FMT" is on port %s in VLAN %d",
2051 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2052 }
2053 break;
2054 case IGMP_HOST_MEMBERSHIP_QUERY:
2055 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2056 in_xbundle->ofbundle)) {
2057 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2058 IP_FMT" is on port %s in VLAN %d",
2059 xbridge->name, IP_ARGS(flow->nw_src),
2060 in_xbundle->name, vlan);
2061 }
2062 break;
e3102e42
TLSC
2063 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2064 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2065 in_xbundle->ofbundle))) {
2066 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2067 "addresses on port %s in VLAN %d",
2068 xbridge->name, count, in_xbundle->name, vlan);
2069 }
2070 break;
86e2dcdd
FL
2071 }
2072}
2073
06994f87
TLSC
2074static void
2075update_mcast_snooping_table6__(const struct xbridge *xbridge,
2076 const struct flow *flow,
2077 struct mcast_snooping *ms, int vlan,
2078 struct xbundle *in_xbundle,
2079 const struct dp_packet *packet)
2080 OVS_REQ_WRLOCK(ms->rwlock)
2081{
2082 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2083 int count;
2084
2085 switch (ntohs(flow->tp_src)) {
2086 case MLD_QUERY:
2087 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2088 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2089 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2090 "in VLAN %d",
2091 xbridge->name, in_xbundle->name, vlan);
2092 }
2093 break;
2094 case MLD_REPORT:
2095 case MLD_DONE:
2096 case MLD2_REPORT:
2097 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2098 if (count) {
2099 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2100 "addresses on port %s in VLAN %d",
2101 xbridge->name, count, in_xbundle->name, vlan);
2102 }
2103 break;
2104 }
2105}
2106
86e2dcdd
FL
2107/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2108 * was received on 'in_xbundle' in 'vlan'. */
2109static void
2110update_mcast_snooping_table(const struct xbridge *xbridge,
2111 const struct flow *flow, int vlan,
e3102e42
TLSC
2112 struct xbundle *in_xbundle,
2113 const struct dp_packet *packet)
86e2dcdd
FL
2114{
2115 struct mcast_snooping *ms = xbridge->ms;
2116 struct xlate_cfg *xcfg;
2117 struct xbundle *mcast_xbundle;
f4ae6e23 2118 struct mcast_port_bundle *fport;
86e2dcdd
FL
2119
2120 /* Don't learn the OFPP_NONE port. */
2121 if (in_xbundle == &ofpp_none_bundle) {
2122 return;
2123 }
2124
2125 /* Don't learn from flood ports */
2126 mcast_xbundle = NULL;
2127 ovs_rwlock_wrlock(&ms->rwlock);
2128 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2129 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2130 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2131 if (mcast_xbundle == in_xbundle) {
2132 break;
2133 }
2134 }
2135
2136 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
06994f87
TLSC
2137 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2138 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2139 in_xbundle, packet);
2140 } else {
2141 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2142 in_xbundle, packet);
2143 }
86e2dcdd
FL
2144 }
2145 ovs_rwlock_unlock(&ms->rwlock);
2146}
2147
2148/* send the packet to ports having the multicast group learned */
2149static void
2150xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2151 struct mcast_snooping *ms OVS_UNUSED,
2152 struct mcast_group *grp,
2153 struct xbundle *in_xbundle, uint16_t vlan)
2154 OVS_REQ_RDLOCK(ms->rwlock)
2155{
2156 struct xlate_cfg *xcfg;
2157 struct mcast_group_bundle *b;
2158 struct xbundle *mcast_xbundle;
2159
2160 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2161 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2162 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2163 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2164 xlate_report(ctx, "forwarding to mcast group port");
2165 output_normal(ctx, mcast_xbundle, vlan);
2166 } else if (!mcast_xbundle) {
2167 xlate_report(ctx, "mcast group port is unknown, dropping");
2168 } else {
2169 xlate_report(ctx, "mcast group port is input port, dropping");
2170 }
2171 }
2172}
2173
2174/* send the packet to ports connected to multicast routers */
2175static void
2176xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2177 struct mcast_snooping *ms,
2178 struct xbundle *in_xbundle, uint16_t vlan)
2179 OVS_REQ_RDLOCK(ms->rwlock)
2180{
2181 struct xlate_cfg *xcfg;
2182 struct mcast_mrouter_bundle *mrouter;
2183 struct xbundle *mcast_xbundle;
2184
2185 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2186 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2187 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2188 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2189 xlate_report(ctx, "forwarding to mcast router port");
2190 output_normal(ctx, mcast_xbundle, vlan);
2191 } else if (!mcast_xbundle) {
2192 xlate_report(ctx, "mcast router port is unknown, dropping");
2193 } else {
2194 xlate_report(ctx, "mcast router port is input port, dropping");
2195 }
2196 }
2197}
2198
2199/* send the packet to ports flagged to be flooded */
2200static void
2201xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2202 struct mcast_snooping *ms,
2203 struct xbundle *in_xbundle, uint16_t vlan)
2204 OVS_REQ_RDLOCK(ms->rwlock)
2205{
2206 struct xlate_cfg *xcfg;
f4ae6e23 2207 struct mcast_port_bundle *fport;
86e2dcdd
FL
2208 struct xbundle *mcast_xbundle;
2209
2210 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2211 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2212 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2213 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2214 xlate_report(ctx, "forwarding to mcast flood port");
2215 output_normal(ctx, mcast_xbundle, vlan);
2216 } else if (!mcast_xbundle) {
2217 xlate_report(ctx, "mcast flood port is unknown, dropping");
2218 } else {
2219 xlate_report(ctx, "mcast flood port is input port, dropping");
2220 }
2221 }
2222}
2223
8e04a33f
FL
2224/* forward the Reports to configured ports */
2225static void
2226xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2227 struct mcast_snooping *ms,
2228 struct xbundle *in_xbundle, uint16_t vlan)
2229 OVS_REQ_RDLOCK(ms->rwlock)
2230{
2231 struct xlate_cfg *xcfg;
2232 struct mcast_port_bundle *rport;
2233 struct xbundle *mcast_xbundle;
2234
2235 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2236 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2237 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2238 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2239 xlate_report(ctx, "forwarding Report to mcast flagged port");
2240 output_normal(ctx, mcast_xbundle, vlan);
2241 } else if (!mcast_xbundle) {
2242 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2243 } else {
2244 xlate_report(ctx, "mcast port is input port, dropping the Report");
2245 }
2246 }
2247}
2248
682800a4
FL
2249static void
2250xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2251 uint16_t vlan)
2252{
2253 struct xbundle *xbundle;
2254
2255 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2256 if (xbundle != in_xbundle
2257 && xbundle_includes_vlan(xbundle, vlan)
2258 && xbundle->floodable
2259 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2260 output_normal(ctx, xbundle, vlan);
2261 }
2262 }
2031ef97 2263 ctx->nf_output_iface = NF_OUT_FLOOD;
682800a4
FL
2264}
2265
9583bc14
EJ
2266static void
2267xlate_normal(struct xlate_ctx *ctx)
2268{
49a73e0c 2269 struct flow_wildcards *wc = ctx->wc;
33bf9176 2270 struct flow *flow = &ctx->xin->flow;
46c88433
EJ
2271 struct xbundle *in_xbundle;
2272 struct xport *in_port;
9583bc14 2273 struct mac_entry *mac;
d6d5bbc9 2274 void *mac_port;
9583bc14
EJ
2275 uint16_t vlan;
2276 uint16_t vid;
2277
33bf9176
BP
2278 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2279 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1dd35f8a 2280 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
9583bc14 2281
46c88433
EJ
2282 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2283 ctx->xin->packet != NULL, &in_port);
2284 if (!in_xbundle) {
9583bc14
EJ
2285 xlate_report(ctx, "no input bundle, dropping");
2286 return;
2287 }
2288
2289 /* Drop malformed frames. */
33bf9176
BP
2290 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2291 !(flow->vlan_tci & htons(VLAN_CFI))) {
9583bc14
EJ
2292 if (ctx->xin->packet != NULL) {
2293 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2294 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2295 "VLAN tag received on port %s",
46c88433 2296 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2297 }
2298 xlate_report(ctx, "partial VLAN tag, dropping");
2299 return;
2300 }
2301
2302 /* Drop frames on bundles reserved for mirroring. */
46c88433 2303 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
9583bc14
EJ
2304 if (ctx->xin->packet != NULL) {
2305 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2306 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2307 "%s, which is reserved exclusively for mirroring",
46c88433 2308 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2309 }
2310 xlate_report(ctx, "input port is mirror output port, dropping");
2311 return;
2312 }
2313
2314 /* Check VLAN. */
33bf9176 2315 vid = vlan_tci_to_vid(flow->vlan_tci);
46c88433 2316 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
2317 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2318 return;
2319 }
46c88433 2320 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14
EJ
2321
2322 /* Check other admissibility requirements. */
2323 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2324 return;
2325 }
2326
2327 /* Learn source MAC. */
2328 if (ctx->xin->may_learn) {
46c88433 2329 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
9583bc14 2330 }
b256dc52
JS
2331 if (ctx->xin->xcache) {
2332 struct xc_entry *entry;
2333
2334 /* Save enough info to update mac learning table later. */
2335 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
9edf6b48 2336 entry->u.normal.ofproto = ctx->xbridge->ofproto;
b256dc52
JS
2337 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2338 entry->u.normal.vlan = vlan;
2339 }
9583bc14
EJ
2340
2341 /* Determine output bundle. */
86e2dcdd
FL
2342 if (mcast_snooping_enabled(ctx->xbridge->ms)
2343 && !eth_addr_is_broadcast(flow->dl_dst)
2344 && eth_addr_is_multicast(flow->dl_dst)
06994f87 2345 && is_ip_any(flow)) {
86e2dcdd 2346 struct mcast_snooping *ms = ctx->xbridge->ms;
06994f87 2347 struct mcast_group *grp = NULL;
86e2dcdd 2348
06994f87 2349 if (is_igmp(flow)) {
d29f137b
TLSC
2350 if (mcast_snooping_is_membership(flow->tp_src) ||
2351 mcast_snooping_is_query(flow->tp_src)) {
2352 if (ctx->xin->may_learn) {
86e2dcdd 2353 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
e3102e42 2354 in_xbundle, ctx->xin->packet);
d29f137b
TLSC
2355 }
2356 /*
2357 * IGMP packets need to take the slow path, in order to be
2358 * processed for mdb updates. That will prevent expires
2359 * firing off even after hosts have sent reports.
2360 */
2361 ctx->xout->slow |= SLOW_ACTION;
86e2dcdd 2362 }
d6d5bbc9 2363
86e2dcdd
FL
2364 if (mcast_snooping_is_membership(flow->tp_src)) {
2365 ovs_rwlock_rdlock(&ms->rwlock);
2366 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
8e04a33f
FL
2367 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2368 * forward IGMP Membership Reports only to those ports where
2369 * multicast routers are attached. Alternatively stated: a
2370 * snooping switch should not forward IGMP Membership Reports
2371 * to ports on which only hosts are attached.
2372 * An administrative control may be provided to override this
2373 * restriction, allowing the report messages to be flooded to
2374 * other ports. */
2375 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
86e2dcdd
FL
2376 ovs_rwlock_unlock(&ms->rwlock);
2377 } else {
2378 xlate_report(ctx, "multicast traffic, flooding");
2379 xlate_normal_flood(ctx, in_xbundle, vlan);
2380 }
2381 return;
06994f87
TLSC
2382 } else if (is_mld(flow)) {
2383 ctx->xout->slow |= SLOW_ACTION;
2384 if (ctx->xin->may_learn) {
2385 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2386 in_xbundle, ctx->xin->packet);
2387 }
2388 if (is_mld_report(flow)) {
2389 ovs_rwlock_rdlock(&ms->rwlock);
2390 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2391 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2392 ovs_rwlock_unlock(&ms->rwlock);
2393 } else {
2394 xlate_report(ctx, "MLD query, flooding");
2395 xlate_normal_flood(ctx, in_xbundle, vlan);
2396 }
86e2dcdd 2397 } else {
06994f87
TLSC
2398 if ((flow->dl_type == htons(ETH_TYPE_IP)
2399 && ip_is_local_multicast(flow->nw_dst))
2400 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2401 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
86e2dcdd
FL
2402 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2403 * address in the 224.0.0.x range which are not IGMP must
2404 * be forwarded on all ports */
2405 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2406 xlate_normal_flood(ctx, in_xbundle, vlan);
2407 return;
2408 }
2409 }
2410
2411 /* forwarding to group base ports */
2412 ovs_rwlock_rdlock(&ms->rwlock);
06994f87
TLSC
2413 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2414 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2415 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2416 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2417 }
86e2dcdd
FL
2418 if (grp) {
2419 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2420 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2421 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
9583bc14 2422 } else {
86e2dcdd
FL
2423 if (mcast_snooping_flood_unreg(ms)) {
2424 xlate_report(ctx, "unregistered multicast, flooding");
2425 xlate_normal_flood(ctx, in_xbundle, vlan);
2426 } else {
2427 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2428 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2429 }
9583bc14 2430 }
86e2dcdd 2431 ovs_rwlock_unlock(&ms->rwlock);
9583bc14 2432 } else {
86e2dcdd
FL
2433 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2434 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
9d078ec2 2435 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
86e2dcdd
FL
2436 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2437
2438 if (mac_port) {
2439 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2440 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2441 if (mac_xbundle && mac_xbundle != in_xbundle) {
2442 xlate_report(ctx, "forwarding to learned port");
2443 output_normal(ctx, mac_xbundle, vlan);
2444 } else if (!mac_xbundle) {
2445 xlate_report(ctx, "learned port is unknown, dropping");
2446 } else {
2447 xlate_report(ctx, "learned port is input port, dropping");
2448 }
2449 } else {
2450 xlate_report(ctx, "no learned MAC for destination, flooding");
2451 xlate_normal_flood(ctx, in_xbundle, vlan);
2452 }
9583bc14
EJ
2453 }
2454}
2455
a6092018
BP
2456/* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2457 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2458 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2459 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2460 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2461 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
9583bc14
EJ
2462 */
2463static size_t
a6092018 2464compose_sample_action(struct xlate_ctx *ctx,
9583bc14
EJ
2465 const uint32_t probability,
2466 const union user_action_cookie *cookie,
8b7ea2d4 2467 const size_t cookie_size,
7321bda3
NM
2468 const odp_port_t tunnel_out_port,
2469 bool include_actions)
9583bc14 2470{
a6092018
BP
2471 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2472 OVS_ACTION_ATTR_SAMPLE);
9583bc14 2473
a6092018 2474 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
9583bc14 2475
a6092018
BP
2476 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2477 OVS_SAMPLE_ATTR_ACTIONS);
9583bc14 2478
a6092018
BP
2479 odp_port_t odp_port = ofp_port_to_odp_port(
2480 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2481 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2482 flow_hash_5tuple(&ctx->xin->flow, 0));
2483 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2484 tunnel_out_port,
2485 include_actions,
2486 ctx->odp_actions);
89a8a7f0 2487
a6092018
BP
2488 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2489 nl_msg_end_nested(ctx->odp_actions, sample_offset);
9583bc14 2490
9583bc14
EJ
2491 return cookie_offset;
2492}
2493
a6092018
BP
2494/* If sFLow is not enabled, returns 0 without doing anything.
2495 *
2496 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2497 * in 'ctx'. This action is a template because some of the information needed
2498 * to fill it out is not available until flow translation is complete. In this
2499 * case, this functions returns an offset, which is always nonzero, to pass
2500 * later to fix_sflow_action() to fill in the rest of the template. */
9583bc14 2501static size_t
a6092018 2502compose_sflow_action(struct xlate_ctx *ctx)
9583bc14 2503{
a6092018
BP
2504 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2505 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2506 return 0;
2507 }
2508
a6092018
BP
2509 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2510 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
7321bda3
NM
2511 &cookie, sizeof cookie.sflow, ODPP_NONE,
2512 true);
9583bc14
EJ
2513}
2514
a6092018
BP
2515/* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2516 * 'ctx->odp_actions'. */
9583bc14 2517static void
a6092018 2518compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
9583bc14 2519{
a6092018 2520 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
8b7ea2d4 2521 odp_port_t tunnel_out_port = ODPP_NONE;
9583bc14 2522
a6092018 2523 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2524 return;
2525 }
2526
8b7ea2d4
WZ
2527 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2528 * number. */
2529 if (output_odp_port == ODPP_NONE &&
a6092018 2530 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
8b7ea2d4
WZ
2531 return;
2532 }
2533
2534 /* For output case, output_odp_port is valid*/
2535 if (output_odp_port != ODPP_NONE) {
a6092018 2536 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
8b7ea2d4
WZ
2537 return;
2538 }
2539 /* If tunnel sampling is enabled, put an additional option attribute:
2540 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2541 */
a6092018
BP
2542 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2543 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
8b7ea2d4
WZ
2544 tunnel_out_port = output_odp_port;
2545 }
2546 }
2547
a6092018
BP
2548 union user_action_cookie cookie = {
2549 .ipfix = {
2550 .type = USER_ACTION_COOKIE_IPFIX,
2551 .output_odp_port = output_odp_port,
2552 }
2553 };
2554 compose_sample_action(ctx,
2555 dpif_ipfix_get_bridge_exporter_probability(ipfix),
7321bda3
NM
2556 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2557 false);
9583bc14
EJ
2558}
2559
a6092018
BP
2560/* Fix "sample" action according to data collected while composing ODP actions,
2561 * as described in compose_sflow_action().
2562 *
2563 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
9583bc14 2564static void
a6092018 2565fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
9583bc14
EJ
2566{
2567 const struct flow *base = &ctx->base_flow;
2568 union user_action_cookie *cookie;
2569
a6092018 2570 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
9583bc14
EJ
2571 sizeof cookie->sflow);
2572 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2573
a6092018
BP
2574 cookie->type = USER_ACTION_COOKIE_SFLOW;
2575 cookie->sflow.vlan_tci = base->vlan_tci;
2576
2577 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2578 * port information") for the interpretation of cookie->output. */
2579 switch (ctx->sflow_n_outputs) {
2580 case 0:
2581 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2582 cookie->sflow.output = 0x40000000 | 256;
2583 break;
2584
2585 case 1:
2586 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2587 ctx->xbridge->sflow, ctx->sflow_odp_port);
2588 if (cookie->sflow.output) {
2589 break;
2590 }
2591 /* Fall through. */
2592 default:
2593 /* 0x80000000 means "multiple output ports. */
2594 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2595 break;
2596 }
9583bc14
EJ
2597}
2598
515793d5
BP
2599static bool
2600process_special(struct xlate_ctx *ctx, const struct xport *xport)
db7d4e46 2601{
515793d5 2602 const struct flow *flow = &ctx->xin->flow;
49a73e0c 2603 struct flow_wildcards *wc = ctx->wc;
46c88433 2604 const struct xbridge *xbridge = ctx->xbridge;
515793d5
BP
2605 const struct dp_packet *packet = ctx->xin->packet;
2606 enum slow_path_reason slow;
642dc74d 2607
46c88433 2608 if (!xport) {
515793d5 2609 slow = 0;
46c88433 2610 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
db7d4e46 2611 if (packet) {
46c88433 2612 cfm_process_heartbeat(xport->cfm, packet);
db7d4e46 2613 }
515793d5 2614 slow = SLOW_CFM;
fab52e16 2615 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
db7d4e46 2616 if (packet) {
46c88433 2617 bfd_process_packet(xport->bfd, flow, packet);
60d02c72
AW
2618 /* If POLL received, immediately sends FINAL back. */
2619 if (bfd_should_send_packet(xport->bfd)) {
6d308b28 2620 ofproto_dpif_monitor_port_send_soon(xport->ofport);
60d02c72 2621 }
db7d4e46 2622 }
515793d5 2623 slow = SLOW_BFD;
46c88433 2624 } else if (xport->xbundle && xport->xbundle->lacp
db7d4e46
JP
2625 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2626 if (packet) {
46c88433 2627 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
db7d4e46 2628 }
515793d5 2629 slow = SLOW_LACP;
9efd308e
DV
2630 } else if ((xbridge->stp || xbridge->rstp) &&
2631 stp_should_process_flow(flow, wc)) {
db7d4e46 2632 if (packet) {
f025bcb7
JR
2633 xbridge->stp
2634 ? stp_process_packet(xport, packet)
2635 : rstp_process_packet(xport, packet);
db7d4e46 2636 }
515793d5 2637 slow = SLOW_STP;
19aef6ef 2638 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
0477baa9
DF
2639 if (packet) {
2640 lldp_process_packet(xport->lldp, packet);
2641 }
515793d5 2642 slow = SLOW_LLDP;
db7d4e46 2643 } else {
515793d5
BP
2644 slow = 0;
2645 }
2646
2647 if (slow) {
2648 ctx->xout->slow |= slow;
2649 return true;
2650 } else {
2651 return false;
db7d4e46
JP
2652 }
2653}
2654
a36de779
PS
2655static int
2656tnl_route_lookup_flow(const struct flow *oflow,
2657 ovs_be32 *ip, struct xport **out_port)
2658{
2659 char out_dev[IFNAMSIZ];
2660 struct xbridge *xbridge;
2661 struct xlate_cfg *xcfg;
2662 ovs_be32 gw;
2663
2664 if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
2665 return -ENOENT;
2666 }
2667
2668 if (gw) {
2669 *ip = gw;
2670 } else {
2671 *ip = oflow->tunnel.ip_dst;
2672 }
2673
2674 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2675 ovs_assert(xcfg);
2676
2677 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2678 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2679 struct xport *port;
2680
2681 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2682 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2683 *out_port = port;
2684 return 0;
2685 }
2686 }
2687 }
2688 }
2689 return -ENOENT;
2690}
2691
2692static int
cdd42eda
JG
2693compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
2694 struct dp_packet *packet)
a36de779 2695{
cdd42eda 2696 struct xbridge *xbridge = out_dev->xbridge;
a36de779
PS
2697 struct ofpact_output output;
2698 struct flow flow;
2699
2700 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
cf62fa4c 2701 flow_extract(packet, &flow);
cdd42eda
JG
2702 flow.in_port.ofp_port = out_dev->ofp_port;
2703 output.port = OFPP_TABLE;
a36de779
PS
2704 output.max_len = 0;
2705
cdd42eda
JG
2706 return ofproto_dpif_execute_actions__(xbridge->ofproto, &flow, NULL,
2707 &output.ofpact, sizeof output,
2708 ctx->recurse, ctx->resubmits, packet);
a36de779
PS
2709}
2710
2711static void
cdd42eda 2712tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
74ff3298 2713 const struct eth_addr eth_src,
a36de779
PS
2714 ovs_be32 ip_src, ovs_be32 ip_dst)
2715{
cf62fa4c 2716 struct dp_packet packet;
a36de779 2717
cf62fa4c 2718 dp_packet_init(&packet, 0);
eb0b295e
BP
2719 compose_arp(&packet, ARP_OP_REQUEST,
2720 eth_src, eth_addr_zero, true, ip_src, ip_dst);
a36de779 2721
cdd42eda 2722 compose_table_xlate(ctx, out_dev, &packet);
cf62fa4c 2723 dp_packet_uninit(&packet);
a36de779
PS
2724}
2725
2726static int
81de18ec 2727build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
a36de779
PS
2728 const struct flow *flow, odp_port_t tunnel_odp_port)
2729{
2730 struct ovs_action_push_tnl tnl_push_data;
2731 struct xport *out_dev = NULL;
2732 ovs_be32 s_ip, d_ip = 0;
74ff3298
JR
2733 struct eth_addr smac;
2734 struct eth_addr dmac;
a36de779
PS
2735 int err;
2736
2737 err = tnl_route_lookup_flow(flow, &d_ip, &out_dev);
2738 if (err) {
81de18ec 2739 xlate_report(ctx, "native tunnel routing failed");
a36de779
PS
2740 return err;
2741 }
81de18ec
BP
2742 xlate_report(ctx, "tunneling to "IP_FMT" via %s",
2743 IP_ARGS(d_ip), netdev_get_name(out_dev->netdev));
a36de779
PS
2744
2745 /* Use mac addr of bridge port of the peer. */
74ff3298 2746 err = netdev_get_etheraddr(out_dev->netdev, &smac);
a36de779 2747 if (err) {
81de18ec 2748 xlate_report(ctx, "tunnel output device lacks Ethernet address");
a36de779
PS
2749 return err;
2750 }
2751
2752 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2753 if (err) {
81de18ec 2754 xlate_report(ctx, "tunnel output device lacks IPv4 address");
a36de779
PS
2755 return err;
2756 }
2757
74ff3298 2758 err = tnl_arp_lookup(out_dev->xbridge->name, d_ip, &dmac);
a36de779 2759 if (err) {
81de18ec
BP
2760 xlate_report(ctx, "ARP cache miss for "IP_FMT" on bridge %s, "
2761 "sending ARP request",
2762 IP_ARGS(d_ip), out_dev->xbridge->name);
cdd42eda 2763 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
a36de779
PS
2764 return err;
2765 }
2766 if (ctx->xin->xcache) {
2767 struct xc_entry *entry;
2768
2769 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
8742957c
BP
2770 ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name,
2771 sizeof entry->u.tnl_arp_cache.br_name);
a36de779
PS
2772 entry->u.tnl_arp_cache.d_ip = d_ip;
2773 }
81de18ec
BP
2774
2775 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" "IP_FMT
2776 " to "ETH_ADDR_FMT" "IP_FMT,
2777 ETH_ADDR_ARGS(smac), IP_ARGS(s_ip),
2778 ETH_ADDR_ARGS(dmac), IP_ARGS(d_ip));
a36de779
PS
2779 err = tnl_port_build_header(xport->ofport, flow,
2780 dmac, smac, s_ip, &tnl_push_data);
2781 if (err) {
2782 return err;
2783 }
2784 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2785 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
1520ef4f 2786 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
a36de779
PS
2787 return 0;
2788}
2789
704bb0bf
JS
2790static void
2791xlate_commit_actions(struct xlate_ctx *ctx)
2792{
2793 bool use_masked = ctx->xbridge->support.masked_set_action;
2794
2795 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2796 ctx->odp_actions, ctx->wc,
2797 use_masked);
2798}
2799
9583bc14 2800static void
4e022ec0 2801compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
e93ef1c7 2802 const struct xlate_bond_recirc *xr, bool check_stp)
9583bc14 2803{
46c88433 2804 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
49a73e0c 2805 struct flow_wildcards *wc = ctx->wc;
33bf9176 2806 struct flow *flow = &ctx->xin->flow;
a36de779 2807 struct flow_tnl flow_tnl;
9583bc14 2808 ovs_be16 flow_vlan_tci;
1362e248 2809 uint32_t flow_pkt_mark;
9583bc14 2810 uint8_t flow_nw_tos;
4e022ec0 2811 odp_port_t out_port, odp_port;
a36de779 2812 bool tnl_push_pop_send = false;
ca077186 2813 uint8_t dscp;
9583bc14
EJ
2814
2815 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2816 * before traversing a patch port. */
b666962b 2817 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
a36de779 2818 memset(&flow_tnl, 0, sizeof flow_tnl);
9583bc14 2819
46c88433 2820 if (!xport) {
9583bc14
EJ
2821 xlate_report(ctx, "Nonexistent output port");
2822 return;
46c88433 2823 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
9583bc14
EJ
2824 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2825 return;
0d1cee12 2826 } else if (check_stp) {
bbbca389 2827 if (is_stp(&ctx->base_flow)) {
9efd308e
DV
2828 if (!xport_stp_should_forward_bpdu(xport) &&
2829 !xport_rstp_should_manage_bpdu(xport)) {
2830 if (ctx->xbridge->stp != NULL) {
2831 xlate_report(ctx, "STP not in listening state, "
2832 "skipping bpdu output");
2833 } else if (ctx->xbridge->rstp != NULL) {
2834 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2835 "skipping bpdu output");
2836 }
0d1cee12
K
2837 return;
2838 }
9efd308e
DV
2839 } else if (!xport_stp_forward_state(xport) ||
2840 !xport_rstp_forward_state(xport)) {
2841 if (ctx->xbridge->stp != NULL) {
2842 xlate_report(ctx, "STP not in forwarding state, "
2843 "skipping output");
2844 } else if (ctx->xbridge->rstp != NULL) {
2845 xlate_report(ctx, "RSTP not in forwarding state, "
2846 "skipping output");
2847 }
0d1cee12
K
2848 return;
2849 }
9583bc14
EJ
2850 }
2851
46c88433
EJ
2852 if (xport->peer) {
2853 const struct xport *peer = xport->peer;
9583bc14 2854 struct flow old_flow = ctx->xin->flow;
7f5fe537 2855 bool old_was_mpls = ctx->was_mpls;
18721c4a 2856 cls_version_t old_version = ctx->tables_version;
1774d762
JR
2857 struct ofpbuf old_stack = ctx->stack;
2858 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
ed9c9e3e
JR
2859 struct ofpbuf old_action_set = ctx->action_set;
2860 uint64_t actset_stub[1024 / 8];
9583bc14 2861
1774d762 2862 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
ed9c9e3e 2863 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
46c88433
EJ
2864 ctx->xbridge = peer->xbridge;
2865 flow->in_port.ofp_port = peer->ofp_port;
33bf9176
BP
2866 flow->metadata = htonll(0);
2867 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2868 memset(flow->regs, 0, sizeof flow->regs);
c61f3870 2869 flow->actset_output = OFPP_UNSET;
9583bc14 2870
621b8064
JR
2871 /* The bridge is now known so obtain its table version. */
2872 ctx->tables_version
2873 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2874
515793d5 2875 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
9efd308e 2876 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
e672ff9b 2877 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
ed9c9e3e 2878 if (ctx->action_set.size) {
e672ff9b
JR
2879 /* Translate action set only if not dropping the packet and
2880 * not recirculating. */
2881 if (!exit_recirculates(ctx)) {
2882 xlate_action_set(ctx);
2883 }
2884 }
2885 /* Check if need to recirculate. */
2886 if (exit_recirculates(ctx)) {
2887 compose_recirculate_action(ctx);
ed9c9e3e 2888 }
9583bc14 2889 } else {
9efd308e
DV
2890 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2891 * the learning action look at the packet, then drop it. */
9583bc14 2892 struct flow old_base_flow = ctx->base_flow;
1520ef4f 2893 size_t old_size = ctx->odp_actions->size;
3d6151f3 2894 mirror_mask_t old_mirrors = ctx->mirrors;
f3d5b473 2895
e672ff9b 2896 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3d6151f3 2897 ctx->mirrors = old_mirrors;
9583bc14 2898 ctx->base_flow = old_base_flow;
1520ef4f 2899 ctx->odp_actions->size = old_size;
e672ff9b
JR
2900
2901 /* Undo changes that may have been done for recirculation. */
2902 if (exit_recirculates(ctx)) {
2903 ctx->action_set.size = ctx->recirc_action_offset;
2904 ctx->recirc_action_offset = -1;
2905 ctx->last_unroll_offset = -1;
2906 }
9583bc14
EJ
2907 }
2908 }
2909
2910 ctx->xin->flow = old_flow;
832554e3 2911 ctx->xbridge = xport->xbridge;
ed9c9e3e
JR
2912 ofpbuf_uninit(&ctx->action_set);
2913 ctx->action_set = old_action_set;
1774d762
JR
2914 ofpbuf_uninit(&ctx->stack);
2915 ctx->stack = old_stack;
9583bc14 2916
621b8064
JR
2917 /* Restore calling bridge's lookup version. */
2918 ctx->tables_version = old_version;
2919
7f5fe537
JR
2920 /* The peer bridge popping MPLS should have no effect on the original
2921 * bridge. */
2922 ctx->was_mpls = old_was_mpls;
2923
f3d5b473
JR
2924 /* The fact that the peer bridge exits (for any reason) does not mean
2925 * that the original bridge should exit. Specifically, if the peer
2926 * bridge recirculates (which typically modifies the packet), the
2927 * original bridge must continue processing with the original, not the
2928 * recirculated packet! */
2929 ctx->exit = false;
2930
9583bc14 2931 if (ctx->xin->resubmit_stats) {
46c88433
EJ
2932 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2933 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
a1aeea86
AW
2934 if (peer->bfd) {
2935 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2936 }
9583bc14 2937 }
b256dc52
JS
2938 if (ctx->xin->xcache) {
2939 struct xc_entry *entry;
2940
2941 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2942 entry->u.dev.tx = netdev_ref(xport->netdev);
2943 entry->u.dev.rx = netdev_ref(peer->netdev);
2944 entry->u.dev.bfd = bfd_ref(peer->bfd);
2945 }
9583bc14
EJ
2946 return;
2947 }
2948
33bf9176 2949 flow_vlan_tci = flow->vlan_tci;
1362e248 2950 flow_pkt_mark = flow->pkt_mark;
33bf9176 2951 flow_nw_tos = flow->nw_tos;
9583bc14 2952
16194afd
DDP
2953 if (count_skb_priorities(xport)) {
2954 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
2955 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
2956 wc->masks.nw_tos |= IP_DSCP_MASK;
2957 flow->nw_tos &= ~IP_DSCP_MASK;
2958 flow->nw_tos |= dscp;
2959 }
9583bc14
EJ
2960 }
2961
46c88433 2962 if (xport->is_tunnel) {
9583bc14
EJ
2963 /* Save tunnel metadata so that changes made due to
2964 * the Logical (tunnel) Port are not visible for any further
2965 * matches, while explicit set actions on tunnel metadata are.
2966 */
a36de779 2967 flow_tnl = flow->tunnel;
49a73e0c 2968 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
4e022ec0 2969 if (odp_port == ODPP_NONE) {
9583bc14
EJ
2970 xlate_report(ctx, "Tunneling decided against output");
2971 goto out; /* restore flow_nw_tos */
2972 }
33bf9176 2973 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
9583bc14
EJ
2974 xlate_report(ctx, "Not tunneling to our own address");
2975 goto out; /* restore flow_nw_tos */
2976 }
2977 if (ctx->xin->resubmit_stats) {
46c88433 2978 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
9583bc14 2979 }
b256dc52
JS
2980 if (ctx->xin->xcache) {
2981 struct xc_entry *entry;
2982
2983 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2984 entry->u.dev.tx = netdev_ref(xport->netdev);
2985 }
9583bc14 2986 out_port = odp_port;
a36de779 2987 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
81de18ec 2988 xlate_report(ctx, "output to native tunnel");
a36de779
PS
2989 tnl_push_pop_send = true;
2990 } else {
81de18ec 2991 xlate_report(ctx, "output to kernel tunnel");
1520ef4f 2992 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
a36de779
PS
2993 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
2994 }
9583bc14 2995 } else {
46c88433 2996 odp_port = xport->odp_port;
7614e5d0 2997 out_port = odp_port;
46c88433 2998 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
7614e5d0
JR
2999 ofp_port_t vlandev_port;
3000
1dd35f8a 3001 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
7614e5d0
JR
3002 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3003 ofp_port, flow->vlan_tci);
3004 if (vlandev_port != ofp_port) {
3005 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3006 flow->vlan_tci = htons(0);
3007 }
9583bc14 3008 }
9583bc14 3009 }
9583bc14 3010
4e022ec0 3011 if (out_port != ODPP_NONE) {
704bb0bf 3012 xlate_commit_actions(ctx);
adcf00ba 3013
e93ef1c7 3014 if (xr) {
347bf289 3015 struct ovs_action_hash *act_hash;
adcf00ba 3016
347bf289 3017 /* Hash action. */
1520ef4f 3018 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
347bf289
AZ
3019 OVS_ACTION_ATTR_HASH,
3020 sizeof *act_hash);
3021 act_hash->hash_alg = xr->hash_alg;
62ac1f20 3022 act_hash->hash_basis = xr->hash_basis;
347bf289
AZ
3023
3024 /* Recirc action. */
1520ef4f 3025 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
347bf289 3026 xr->recirc_id);
adcf00ba 3027 } else {
a36de779
PS
3028
3029 if (tnl_push_pop_send) {
3030 build_tunnel_send(ctx, xport, flow, odp_port);
3031 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3032 } else {
3033 odp_port_t odp_tnl_port = ODPP_NONE;
3034
3035 /* XXX: Write better Filter for tunnel port. We can use inport
3036 * int tunnel-port flow to avoid these checks completely. */
3037 if (ofp_port == OFPP_LOCAL &&
3038 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3039
3040 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3041 }
3042
3043 if (odp_tnl_port != ODPP_NONE) {
1520ef4f 3044 nl_msg_put_odp_port(ctx->odp_actions,
a36de779
PS
3045 OVS_ACTION_ATTR_TUNNEL_POP,
3046 odp_tnl_port);
3047 } else {
3048 /* Tunnel push-pop action is not compatible with
3049 * IPFIX action. */
a6092018 3050 compose_ipfix_action(ctx, out_port);
1520ef4f 3051 nl_msg_put_odp_port(ctx->odp_actions,
a36de779
PS
3052 OVS_ACTION_ATTR_OUTPUT,
3053 out_port);
3054 }
3055 }
adcf00ba 3056 }
9583bc14 3057
6cbbf4fa
EJ
3058 ctx->sflow_odp_port = odp_port;
3059 ctx->sflow_n_outputs++;
2031ef97 3060 ctx->nf_output_iface = ofp_port;
6cbbf4fa
EJ
3061 }
3062
7efbc3b7
BP
3063 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3064 mirror_packet(ctx, xport->xbundle,
3065 xbundle_mirror_dst(xport->xbundle->xbridge,
3066 xport->xbundle));
3067 }
3068
6cbbf4fa 3069 out:
9583bc14 3070 /* Restore flow */
33bf9176 3071 flow->vlan_tci = flow_vlan_tci;
1362e248 3072 flow->pkt_mark = flow_pkt_mark;
33bf9176 3073 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
3074}
3075
3076static void
e93ef1c7
JR
3077compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3078 const struct xlate_bond_recirc *xr)
9583bc14 3079{
e93ef1c7 3080 compose_output_action__(ctx, ofp_port, xr, true);
9583bc14
EJ
3081}
3082
bb61b33d
BP
3083static void
3084xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
bb61b33d
BP
3085{
3086 struct rule_dpif *old_rule = ctx->rule;
8b1e5560 3087 ovs_be64 old_cookie = ctx->rule_cookie;
dc723c44 3088 const struct rule_actions *actions;
bb61b33d
BP
3089
3090 if (ctx->xin->resubmit_stats) {
70742c7f 3091 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
3092 }
3093
98b07853 3094 ctx->resubmits++;
bb61b33d
BP
3095 ctx->recurse++;
3096 ctx->rule = rule;
8b1e5560 3097 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
6f00e29b
BP
3098 actions = rule_dpif_get_actions(rule);
3099 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
8b1e5560 3100 ctx->rule_cookie = old_cookie;
bb61b33d
BP
3101 ctx->rule = old_rule;
3102 ctx->recurse--;
bb61b33d
BP
3103}
3104
bd3240ba
SH
3105static bool
3106xlate_resubmit_resource_check(struct xlate_ctx *ctx)
9583bc14 3107{
98b07853
BP
3108 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3109
adcf00ba 3110 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
98b07853
BP
3111 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
3112 MAX_RESUBMIT_RECURSION);
adcf00ba 3113 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
98b07853 3114 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1520ef4f 3115 } else if (ctx->odp_actions->size > UINT16_MAX) {
98b07853 3116 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
6fd6ed71 3117 } else if (ctx->stack.size >= 65536) {
98b07853
BP
3118 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
3119 } else {
bd3240ba
SH
3120 return true;
3121 }
3122
3123 return false;
3124}
3125
3126static void
6d328fa2
SH
3127xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3128 bool may_packet_in, bool honor_table_miss)
bd3240ba 3129{
1d741d6d
JR
3130 /* Check if we need to recirculate before matching in a table. */
3131 if (ctx->was_mpls) {
3132 ctx_trigger_recirculation(ctx);
3133 return;
3134 }
bd3240ba 3135 if (xlate_resubmit_resource_check(ctx)) {
9583bc14 3136 uint8_t old_table_id = ctx->table_id;
3f207910 3137 struct rule_dpif *rule;
9583bc14
EJ
3138
3139 ctx->table_id = table_id;
3140
34dd0d78 3141 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
621b8064 3142 ctx->tables_version,
49a73e0c 3143 &ctx->xin->flow, ctx->xin->wc,
34dd0d78
JR
3144 ctx->xin->resubmit_stats,
3145 &ctx->table_id, in_port,
3146 may_packet_in, honor_table_miss);
9583bc14 3147
a8c31348
BP
3148 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3149 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
ad3efdcb
EJ
3150 }
3151
a2143702 3152 if (rule) {
83709dfa
JR
3153 /* Fill in the cache entry here instead of xlate_recursively
3154 * to make the reference counting more explicit. We take a
3155 * reference in the lookups above if we are going to cache the
3156 * rule. */
3157 if (ctx->xin->xcache) {
3158 struct xc_entry *entry;
3159
3160 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3161 entry->u.rule = rule;
1e1e1d19 3162 rule_dpif_ref(rule);
83709dfa 3163 }
bb61b33d 3164 xlate_recursively(ctx, rule);
ad3efdcb
EJ
3165 }
3166
9583bc14 3167 ctx->table_id = old_table_id;
98b07853 3168 return;
9583bc14 3169 }
98b07853
BP
3170
3171 ctx->exit = true;
9583bc14
EJ
3172}
3173
f4fb341b 3174static void
1e684d7d
RW
3175xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3176 struct ofputil_bucket *bucket)
3177{
3178 if (ctx->xin->resubmit_stats) {
3179 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3180 }
3181 if (ctx->xin->xcache) {
3182 struct xc_entry *entry;
3183
3184 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3185 entry->u.group.group = group_dpif_ref(group);
3186 entry->u.group.bucket = bucket;
3187 }
3188}
3189
3190static void
3191xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
f4fb341b
SH
3192{
3193 uint64_t action_list_stub[1024 / 8];
3194 struct ofpbuf action_list, action_set;
5b09e569
JR
3195 struct flow old_flow = ctx->xin->flow;
3196 bool old_was_mpls = ctx->was_mpls;
f4fb341b
SH
3197
3198 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3199 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3200
3201 ofpacts_execute_action_set(&action_list, &action_set);
3202 ctx->recurse++;
6fd6ed71 3203 do_xlate_actions(action_list.data, action_list.size, ctx);
f4fb341b
SH
3204 ctx->recurse--;
3205
3206 ofpbuf_uninit(&action_set);
3207 ofpbuf_uninit(&action_list);
5b09e569 3208
e672ff9b
JR
3209 /* Check if need to recirculate. */
3210 if (exit_recirculates(ctx)) {
3211 compose_recirculate_action(ctx);
3212 }
3213
5b09e569
JR
3214 /* Roll back flow to previous state.
3215 * This is equivalent to cloning the packet for each bucket.
3216 *
3217 * As a side effect any subsequently applied actions will
3218 * also effectively be applied to a clone of the packet taken
3219 * just before applying the all or indirect group.
3220 *
3221 * Note that group buckets are action sets, hence they cannot modify the
3222 * main action set. Also any stack actions are ignored when executing an
3223 * action set, so group buckets cannot change the stack either.
3224 * However, we do allow resubmit actions in group buckets, which could
3225 * break the above assumptions. It is up to the controller to not mess up
3226 * with the action_set and stack in the tables resubmitted to from
3227 * group buckets. */
3228 ctx->xin->flow = old_flow;
3229
3230 /* The group bucket popping MPLS should have no effect after bucket
3231 * execution. */
3232 ctx->was_mpls = old_was_mpls;
3233
3234 /* The fact that the group bucket exits (for any reason) does not mean that
3235 * the translation after the group action should exit. Specifically, if
3236 * the group bucket recirculates (which typically modifies the packet), the
3237 * actions after the group action must continue processing with the
3238 * original, not the recirculated packet! */
3239 ctx->exit = false;
f4fb341b
SH
3240}
3241
3242static void
3243xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3244{
1e684d7d 3245 struct ofputil_bucket *bucket;
ca6ba700 3246 const struct ovs_list *buckets;
f4fb341b
SH
3247
3248 group_dpif_get_buckets(group, &buckets);
3249
3250 LIST_FOR_EACH (bucket, list_node, buckets) {
3251 xlate_group_bucket(ctx, bucket);
f4fb341b 3252 }
1e684d7d 3253 xlate_group_stats(ctx, group, NULL);
f4fb341b
SH
3254}
3255
dd8cd4b4
SH
3256static void
3257xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3258{
1e684d7d 3259 struct ofputil_bucket *bucket;
dd8cd4b4
SH
3260
3261 bucket = group_first_live_bucket(ctx, group, 0);
3262 if (bucket) {
3263 xlate_group_bucket(ctx, bucket);
1e684d7d 3264 xlate_group_stats(ctx, group, bucket);
dd8cd4b4
SH
3265 }
3266}
3267
fe7e5749 3268static void
7565c3e4 3269xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
fe7e5749 3270{
49a73e0c 3271 struct flow_wildcards *wc = ctx->wc;
1e684d7d 3272 struct ofputil_bucket *bucket;
fe7e5749
SH
3273 uint32_t basis;
3274
1d1aae0b 3275 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
80e3509d 3276 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
fe7e5749
SH
3277 bucket = group_best_live_bucket(ctx, group, basis);
3278 if (bucket) {
fe7e5749 3279 xlate_group_bucket(ctx, bucket);
1e684d7d 3280 xlate_group_stats(ctx, group, bucket);
fe7e5749
SH
3281 }
3282}
3283
0c4b9393
SH
3284static void
3285xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3286{
3287 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
0c4b9393
SH
3288 const struct field_array *fields;
3289 struct ofputil_bucket *bucket;
3290 uint32_t basis;
3291 int i;
3292
3293 fields = group_dpif_get_fields(group);
3294 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3295
3296 /* Determine which fields to hash */
3297 for (i = 0; i < MFF_N_IDS; i++) {
3298 if (bitmap_is_set(fields->used.bm, i)) {
3299 const struct mf_field *mf;
3300
3301 /* If the field is already present in 'hash_fields' then
3302 * this loop has already checked that it and its pre-requisites
3303 * are present in the flow and its pre-requisites have
3304 * already been added to 'hash_fields'. There is nothing more
3305 * to do here and as an optimisation the loop can continue. */
3306 if (bitmap_is_set(hash_fields.bm, i)) {
3307 continue;
3308 }
3309
3310 mf = mf_from_id(i);
3311
3312 /* Only hash a field if it and its pre-requisites are present
3313 * in the flow. */
3314 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3315 continue;
3316 }
3317
3318 /* Hash both the field and its pre-requisites */
3319 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3320 }
3321 }
3322
3323 /* Hash the fields */
3324 for (i = 0; i < MFF_N_IDS; i++) {
3325 if (bitmap_is_set(hash_fields.bm, i)) {
3326 const struct mf_field *mf = mf_from_id(i);
3327 union mf_value value;
3328 int j;
3329
3330 mf_get_value(mf, &ctx->xin->flow, &value);
3331 /* This seems inefficient but so does apply_mask() */
3332 for (j = 0; j < mf->n_bytes; j++) {
3333 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3334 }
3335 basis = hash_bytes(&value, mf->n_bytes, basis);
3336
1cb20095
JG
3337 /* For tunnels, hash in whether the field is present. */
3338 if (mf_is_tun_metadata(mf)) {
3339 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3340 }
3341
49a73e0c 3342 mf_mask_field(mf, &ctx->wc->masks);
0c4b9393
SH
3343 }
3344 }
3345
3346 bucket = group_best_live_bucket(ctx, group, basis);
3347 if (bucket) {
3348 xlate_group_bucket(ctx, bucket);
3349 xlate_group_stats(ctx, group, bucket);
3350 }
3351}
3352
7565c3e4
SH
3353static void
3354xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3355{
3356 const char *selection_method = group_dpif_get_selection_method(group);
3357
3358 if (selection_method[0] == '\0') {
3359 xlate_default_select_group(ctx, group);
0c4b9393
SH
3360 } else if (!strcasecmp("hash", selection_method)) {
3361 xlate_hash_fields_select_group(ctx, group);
7565c3e4
SH
3362 } else {
3363 /* Parsing of groups should ensure this never happens */
3364 OVS_NOT_REACHED();
3365 }
3366}
3367
f4fb341b
SH
3368static void
3369xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3370{
0eb48fe1 3371 bool was_in_group = ctx->in_group;
5a070238
BP
3372 ctx->in_group = true;
3373
f4fb341b
SH
3374 switch (group_dpif_get_type(group)) {
3375 case OFPGT11_ALL:
3376 case OFPGT11_INDIRECT:
3377 xlate_all_group(ctx, group);
3378 break;
3379 case OFPGT11_SELECT:
fe7e5749 3380 xlate_select_group(ctx, group);
f4fb341b 3381 break;
dd8cd4b4
SH
3382 case OFPGT11_FF:
3383 xlate_ff_group(ctx, group);
3384 break;
f4fb341b 3385 default:
428b2edd 3386 OVS_NOT_REACHED();
f4fb341b 3387 }
809c7548 3388 group_dpif_unref(group);
5a070238 3389
0eb48fe1 3390 ctx->in_group = was_in_group;
f4fb341b
SH
3391}
3392
3393static bool
3394xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3395{
0eb48fe1 3396 if (xlate_resubmit_resource_check(ctx)) {
f4fb341b
SH
3397 struct group_dpif *group;
3398 bool got_group;
3399
3400 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3401 if (got_group) {
3402 xlate_group_action__(ctx, group);
3403 } else {
3404 return true;
3405 }
3406 }
3407
3408 return false;
3409}
3410
9583bc14
EJ
3411static void
3412xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3413 const struct ofpact_resubmit *resubmit)
3414{
4e022ec0 3415 ofp_port_t in_port;
9583bc14 3416 uint8_t table_id;
adcf00ba
AZ
3417 bool may_packet_in = false;
3418 bool honor_table_miss = false;
3419
3420 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3421 /* Still allow missed packets to be sent to the controller
3422 * if resubmitting from an internal table. */
3423 may_packet_in = true;
3424 honor_table_miss = true;
3425 }
9583bc14
EJ
3426
3427 in_port = resubmit->in_port;
3428 if (in_port == OFPP_IN_PORT) {
4e022ec0 3429 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
3430 }
3431
3432 table_id = resubmit->table_id;
3433 if (table_id == 255) {
3434 table_id = ctx->table_id;
3435 }
3436
adcf00ba
AZ
3437 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3438 honor_table_miss);
9583bc14
EJ
3439}
3440
3441static void
3442flood_packets(struct xlate_ctx *ctx, bool all)
3443{
46c88433 3444 const struct xport *xport;
9583bc14 3445
46c88433
EJ
3446 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3447 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3448 continue;
3449 }
3450
3451 if (all) {
e93ef1c7 3452 compose_output_action__(ctx, xport->ofp_port, NULL, false);
46c88433 3453 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
e93ef1c7 3454 compose_output_action(ctx, xport->ofp_port, NULL);
9583bc14
EJ
3455 }
3456 }
3457
2031ef97 3458 ctx->nf_output_iface = NF_OUT_FLOOD;
9583bc14
EJ
3459}
3460
3461static void
3462execute_controller_action(struct xlate_ctx *ctx, int len,
3463 enum ofp_packet_in_reason reason,
3464 uint16_t controller_id)
3465{
0fb7792a 3466 struct ofproto_packet_in *pin;
e14deea0 3467 struct dp_packet *packet;
9583bc14 3468
04594cd5 3469 ctx->xout->slow |= SLOW_CONTROLLER;
9583bc14
EJ
3470 if (!ctx->xin->packet) {
3471 return;
3472 }
3473
cf62fa4c 3474 packet = dp_packet_clone(ctx->xin->packet);
9583bc14 3475
704bb0bf 3476 xlate_commit_actions(ctx);
9583bc14 3477
41ccaa24 3478 odp_execute_actions(NULL, &packet, 1, false,
1520ef4f 3479 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
9583bc14 3480
ada3a58d 3481 pin = xmalloc(sizeof *pin);
cf62fa4c
PS
3482 pin->up.packet_len = dp_packet_size(packet);
3483 pin->up.packet = dp_packet_steal_data(packet);
0fb7792a 3484 pin->up.reason = reason;
0fb7792a 3485 pin->up.table_id = ctx->table_id;
8b1e5560 3486 pin->up.cookie = ctx->rule_cookie;
0fb7792a 3487
50dcbd8e 3488 flow_get_metadata(&ctx->xin->flow, &pin->up.flow_metadata);
9583bc14 3489
f11c7538 3490 pin->controller_id = controller_id;
d38a3c7b 3491 pin->send_len = len;
32260212
SH
3492 /* If a rule is a table-miss rule then this is
3493 * a table-miss handled by a table-miss rule.
3494 *
3495 * Else, if rule is internal and has a controller action,
3496 * the later being implied by the rule being processed here,
3497 * then this is a table-miss handled without a table-miss rule.
3498 *
3499 * Otherwise this is not a table-miss. */
3500 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3501 if (ctx->rule) {
3502 if (rule_dpif_is_table_miss(ctx->rule)) {
3503 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3504 } else if (rule_dpif_is_internal(ctx->rule)) {
3505 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3506 }
3507 }
ada3a58d 3508 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
e14deea0 3509 dp_packet_delete(packet);
9583bc14
EJ
3510}
3511
e672ff9b 3512/* Called only when ctx->recirc_action_offset is set. */
7bbdd84f 3513static void
e672ff9b 3514compose_recirculate_action(struct xlate_ctx *ctx)
7bbdd84f 3515{
e672ff9b 3516 struct recirc_metadata md;
7bbdd84f 3517 uint32_t id;
7bbdd84f 3518
704bb0bf 3519 xlate_commit_actions(ctx);
e672ff9b 3520 recirc_metadata_from_flow(&md, &ctx->xin->flow);
7bbdd84f 3521
e672ff9b 3522 ovs_assert(ctx->recirc_action_offset >= 0);
7bbdd84f 3523
2082425c
BP
3524 struct recirc_state state = {
3525 .table_id = 0,
3526 .ofproto = ctx->xbridge->ofproto,
3527 .metadata = md,
3528 .stack = &ctx->stack,
29bae541 3529 .mirrors = ctx->mirrors,
2082425c
BP
3530 .action_set_len = ctx->recirc_action_offset,
3531 .ofpacts_len = ctx->action_set.size,
3532 .ofpacts = ctx->action_set.data,
3533 };
3534
e672ff9b
JR
3535 /* Only allocate recirculation ID if we have a packet. */
3536 if (ctx->xin->packet) {
3537 /* Allocate a unique recirc id for the given metadata state in the
3538 * flow. The life-cycle of this recirc id is managed by associating it
3539 * with the udpif key ('ukey') created for each new datapath flow. */
2082425c 3540 id = recirc_alloc_id_ctx(&state);
e672ff9b
JR
3541 if (!id) {
3542 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3543 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3544 return;
e2676b44 3545 }
e672ff9b
JR
3546 xlate_out_add_recirc(ctx->xout, id);
3547 } else {
3548 /* Look up an existing recirc id for the given metadata state in the
3549 * flow. No new reference is taken, as the ID is RCU protected and is
2082425c
BP
3550 * only required temporarily for verification.
3551 *
3552 * This might fail and return 0. We let zero 'id' to be used in the
3553 * RECIRC action below, which will fail all revalidations as zero is
3554 * not a valid recirculation ID. */
3555 id = recirc_find_id(&state);
7bbdd84f
SH
3556 }
3557
1520ef4f 3558 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
e672ff9b
JR
3559
3560 /* Undo changes done by recirculation. */
3561 ctx->action_set.size = ctx->recirc_action_offset;
3562 ctx->recirc_action_offset = -1;
3563 ctx->last_unroll_offset = -1;
7bbdd84f
SH
3564}
3565
8bfd0fda
BP
3566static void
3567compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
9583bc14 3568{
33bf9176 3569 struct flow *flow = &ctx->xin->flow;
8bfd0fda 3570 int n;
33bf9176 3571
8bfd0fda 3572 ovs_assert(eth_type_mpls(mpls->ethertype));
b0a17866 3573
49a73e0c 3574 n = flow_count_mpls_labels(flow, ctx->wc);
8bfd0fda 3575 if (!n) {
704bb0bf 3576 xlate_commit_actions(ctx);
8bfd0fda
BP
3577 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3578 if (ctx->xin->packet != NULL) {
3579 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3580 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3581 "MPLS push action can't be performed as it would "
3582 "have more MPLS LSEs than the %d supported.",
3583 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
9583bc14 3584 }
8bfd0fda
BP
3585 ctx->exit = true;
3586 return;
9583bc14 3587 }
b0a17866 3588
49a73e0c 3589 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
9583bc14
EJ
3590}
3591
8bfd0fda 3592static void
9cfef3d0 3593compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 3594{
8bfd0fda 3595 struct flow *flow = &ctx->xin->flow;
49a73e0c 3596 int n = flow_count_mpls_labels(flow, ctx->wc);
33bf9176 3597
49a73e0c 3598 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
2494ccd7 3599 if (ctx->xbridge->support.odp.recirc) {
7bbdd84f
SH
3600 ctx->was_mpls = true;
3601 }
3602 } else if (n >= FLOW_MAX_MPLS_LABELS) {
8bfd0fda
BP
3603 if (ctx->xin->packet != NULL) {
3604 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3605 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3606 "MPLS pop action can't be performed as it has "
3607 "more MPLS LSEs than the %d supported.",
3608 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3609 }
3610 ctx->exit = true;
1520ef4f 3611 ofpbuf_clear(ctx->odp_actions);
9583bc14
EJ
3612 }
3613}
3614
3615static bool
3616compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3617{
33bf9176
BP
3618 struct flow *flow = &ctx->xin->flow;
3619
3620 if (!is_ip_any(flow)) {
9583bc14
EJ
3621 return false;
3622 }
3623
49a73e0c 3624 ctx->wc->masks.nw_ttl = 0xff;
33bf9176
BP
3625 if (flow->nw_ttl > 1) {
3626 flow->nw_ttl--;
9583bc14
EJ
3627 return false;
3628 } else {
3629 size_t i;
3630
3631 for (i = 0; i < ids->n_controllers; i++) {
3632 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3633 ids->cnt_ids[i]);
3634 }
3635
3636 /* Stop processing for current table. */
3637 return true;
3638 }
3639}
3640
8bfd0fda 3641static void
097d4939
JR
3642compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3643{
8bfd0fda 3644 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 3645 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
8bfd0fda 3646 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
097d4939 3647 }
097d4939
JR
3648}
3649
8bfd0fda 3650static void
097d4939
JR
3651compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3652{
8bfd0fda 3653 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 3654 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
8bfd0fda 3655 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
097d4939 3656 }
097d4939
JR
3657}
3658
8bfd0fda 3659static void
9cfef3d0 3660compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14 3661{
8bfd0fda 3662 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 3663 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda 3664 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
b0a17866 3665 }
9583bc14
EJ
3666}
3667
3668static bool
9cfef3d0 3669compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 3670{
33bf9176 3671 struct flow *flow = &ctx->xin->flow;
1dd35f8a 3672
8bfd0fda 3673 if (eth_type_mpls(flow->dl_type)) {
22d38fca
JR
3674 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3675
49a73e0c 3676 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda
BP
3677 if (ttl > 1) {
3678 ttl--;
3679 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3680 return false;
3681 } else {
3682 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
8bfd0fda 3683 }
9583bc14 3684 }
22d38fca
JR
3685
3686 /* Stop processing for current table. */
3687 return true;
9583bc14
EJ
3688}
3689
3690static void
3691xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 3692 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 3693{
2031ef97 3694 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
9583bc14 3695
2031ef97 3696 ctx->nf_output_iface = NF_OUT_DROP;
9583bc14
EJ
3697
3698 switch (port) {
3699 case OFPP_IN_PORT:
e93ef1c7 3700 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
9583bc14
EJ
3701 break;
3702 case OFPP_TABLE:
4e022ec0 3703 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 3704 0, may_packet_in, true);
9583bc14
EJ
3705 break;
3706 case OFPP_NORMAL:
3707 xlate_normal(ctx);
3708 break;
3709 case OFPP_FLOOD:
3710 flood_packets(ctx, false);
3711 break;
3712 case OFPP_ALL:
3713 flood_packets(ctx, true);
3714 break;
3715 case OFPP_CONTROLLER:
3a11fd5b 3716 execute_controller_action(ctx, max_len,
029ca940
SS
3717 (ctx->in_group ? OFPR_GROUP
3718 : ctx->in_action_set ? OFPR_ACTION_SET
3719 : OFPR_ACTION),
3720 0);
9583bc14
EJ
3721 break;
3722 case OFPP_NONE:
3723 break;
3724 case OFPP_LOCAL:
3725 default:
4e022ec0 3726 if (port != ctx->xin->flow.in_port.ofp_port) {
e93ef1c7 3727 compose_output_action(ctx, port, NULL);
9583bc14
EJ
3728 } else {
3729 xlate_report(ctx, "skipping output to input port");
3730 }
3731 break;
3732 }
3733
3734 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2031ef97
BP
3735 ctx->nf_output_iface = NF_OUT_FLOOD;
3736 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3737 ctx->nf_output_iface = prev_nf_output_iface;
9583bc14 3738 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2031ef97
BP
3739 ctx->nf_output_iface != NF_OUT_FLOOD) {
3740 ctx->nf_output_iface = NF_OUT_MULTI;
9583bc14
EJ
3741 }
3742}
3743
3744static void
3745xlate_output_reg_action(struct xlate_ctx *ctx,
3746 const struct ofpact_output_reg *or)
3747{
3748 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3749 if (port <= UINT16_MAX) {
3750 union mf_subvalue value;
3751
3752 memset(&value, 0xff, sizeof value);
49a73e0c 3753 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
4e022ec0
AW
3754 xlate_output_action(ctx, u16_to_ofp(port),
3755 or->max_len, false);
9583bc14
EJ
3756 }
3757}
3758
3759static void
3760xlate_enqueue_action(struct xlate_ctx *ctx,
3761 const struct ofpact_enqueue *enqueue)
3762{
4e022ec0 3763 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
3764 uint32_t queue_id = enqueue->queue;
3765 uint32_t flow_priority, priority;
3766 int error;
3767
3768 /* Translate queue to priority. */
89a8a7f0 3769 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
3770 if (error) {
3771 /* Fall back to ordinary output action. */
3772 xlate_output_action(ctx, enqueue->port, 0, false);
3773 return;
3774 }
3775
3776 /* Check output port. */
3777 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
3778 ofp_port = ctx->xin->flow.in_port.ofp_port;
3779 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3780 return;
3781 }
3782
3783 /* Add datapath actions. */
3784 flow_priority = ctx->xin->flow.skb_priority;
3785 ctx->xin->flow.skb_priority = priority;
e93ef1c7 3786 compose_output_action(ctx, ofp_port, NULL);
9583bc14
EJ
3787 ctx->xin->flow.skb_priority = flow_priority;
3788
3789 /* Update NetFlow output port. */
2031ef97
BP
3790 if (ctx->nf_output_iface == NF_OUT_DROP) {
3791 ctx->nf_output_iface = ofp_port;
3792 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3793 ctx->nf_output_iface = NF_OUT_MULTI;
9583bc14
EJ
3794 }
3795}
3796
3797static void
3798xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3799{
3800 uint32_t skb_priority;
3801
89a8a7f0 3802 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
3803 ctx->xin->flow.skb_priority = skb_priority;
3804 } else {
3805 /* Couldn't translate queue to a priority. Nothing to do. A warning
3806 * has already been logged. */
3807 }
3808}
3809
3810static bool
46c88433 3811slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 3812{
46c88433
EJ
3813 const struct xbridge *xbridge = xbridge_;
3814 struct xport *port;
9583bc14
EJ
3815
3816 switch (ofp_port) {
3817 case OFPP_IN_PORT:
3818 case OFPP_TABLE:
3819 case OFPP_NORMAL:
3820 case OFPP_FLOOD:
3821 case OFPP_ALL:
3822 case OFPP_NONE:
3823 return true;
3824 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3825 return false;
3826 default:
46c88433 3827 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
3828 return port ? port->may_enable : false;
3829 }
3830}
3831
3832static void
3833xlate_bundle_action(struct xlate_ctx *ctx,
3834 const struct ofpact_bundle *bundle)
3835{
4e022ec0 3836 ofp_port_t port;
9583bc14 3837
49a73e0c 3838 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
46c88433 3839 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 3840 if (bundle->dst.field) {
49a73e0c 3841 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
9583bc14
EJ
3842 } else {
3843 xlate_output_action(ctx, port, 0, false);
3844 }
3845}
3846
3847static void
4165b5e0
JS
3848xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3849 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
9583bc14 3850{
4165b5e0
JS
3851 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3852 if (ctx->xin->may_learn) {
3853 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3854 }
3855}
9583bc14 3856
4165b5e0
JS
3857static void
3858xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3859{
49a73e0c 3860 learn_mask(learn, ctx->wc);
9583bc14 3861
b256dc52
JS
3862 if (ctx->xin->xcache) {
3863 struct xc_entry *entry;
3864
3865 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4165b5e0
JS
3866 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3867 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3868 entry->u.learn.ofpacts = ofpbuf_new(64);
3869 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3870 entry->u.learn.ofpacts);
3871 } else if (ctx->xin->may_learn) {
3872 uint64_t ofpacts_stub[1024 / 8];
3873 struct ofputil_flow_mod fm;
3874 struct ofpbuf ofpacts;
3875
3876 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3877 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3878 ofpbuf_uninit(&ofpacts);
b256dc52
JS
3879 }
3880}
3881
3882static void
3883xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3884 uint16_t idle_timeout, uint16_t hard_timeout)
3885{
3886 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3887 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3888 }
9583bc14
EJ
3889}
3890
9583bc14
EJ
3891static void
3892xlate_fin_timeout(struct xlate_ctx *ctx,
3893 const struct ofpact_fin_timeout *oft)
3894{
b256dc52
JS
3895 if (ctx->rule) {
3896 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3897 oft->fin_idle_timeout, oft->fin_hard_timeout);
3898 if (ctx->xin->xcache) {
3899 struct xc_entry *entry;
3900
3901 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
83709dfa
JR
3902 /* XC_RULE already holds a reference on the rule, none is taken
3903 * here. */
b256dc52
JS
3904 entry->u.fin.rule = ctx->rule;
3905 entry->u.fin.idle = oft->fin_idle_timeout;
3906 entry->u.fin.hard = oft->fin_hard_timeout;
b256dc52 3907 }
9583bc14
EJ
3908 }
3909}
3910
3911static void
3912xlate_sample_action(struct xlate_ctx *ctx,
3913 const struct ofpact_sample *os)
3914{
e824d78d
JR
3915 /* Scale the probability from 16-bit to 32-bit while representing
3916 * the same percentage. */
3917 uint32_t probability = (os->probability << 16) | os->probability;
3918
b440dd8c 3919 if (!ctx->xbridge->support.variable_length_userdata) {
e824d78d
JR
3920 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3921
3922 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3923 "lacks support (needs Linux 3.10+ or kernel module from "
3924 "OVS 1.11+)");
3925 return;
3926 }
3927
704bb0bf 3928 xlate_commit_actions(ctx);
e824d78d 3929
a6092018
BP
3930 union user_action_cookie cookie = {
3931 .flow_sample = {
3932 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
3933 .probability = os->probability,
3934 .collector_set_id = os->collector_set_id,
3935 .obs_domain_id = os->obs_domain_id,
3936 .obs_point_id = os->obs_point_id,
3937 }
3938 };
3939 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
3940 ODPP_NONE, false);
9583bc14
EJ
3941}
3942
3943static bool
46c88433 3944may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 3945{
bbbca389 3946 if (xport->config & (is_stp(&ctx->xin->flow)
46c88433
EJ
3947 ? OFPUTIL_PC_NO_RECV_STP
3948 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
3949 return false;
3950 }
3951
3952 /* Only drop packets here if both forwarding and learning are
3953 * disabled. If just learning is enabled, we need to have
3954 * OFPP_NORMAL and the learning action have a look at the packet
3955 * before we can drop it. */
9efd308e
DV
3956 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3957 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
9583bc14
EJ
3958 return false;
3959 }
3960
3961 return true;
3962}
3963
7fdb60a7
SH
3964static void
3965xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3966{
c61f3870
BP
3967 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3968 size_t on_len = ofpact_nest_get_action_len(on);
3969 const struct ofpact *inner;
3970
3971 /* Maintain actset_output depending on the contents of the action set:
3972 *
3973 * - OFPP_UNSET, if there is no "output" action.
3974 *
3975 * - The output port, if there is an "output" action and no "group"
3976 * action.
3977 *
3978 * - OFPP_UNSET, if there is a "group" action.
3979 */
3980 if (!ctx->action_set_has_group) {
3981 OFPACT_FOR_EACH (inner, on->actions, on_len) {
3982 if (inner->type == OFPACT_OUTPUT) {
3983 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
3984 } else if (inner->type == OFPACT_GROUP) {
3985 ctx->xin->flow.actset_output = OFPP_UNSET;
3986 ctx->action_set_has_group = true;
9055ca9a 3987 break;
c61f3870
BP
3988 }
3989 }
3990 }
3991
3992 ofpbuf_put(&ctx->action_set, on->actions, on_len);
7fdb60a7
SH
3993 ofpact_pad(&ctx->action_set);
3994}
3995
3996static void
3997xlate_action_set(struct xlate_ctx *ctx)
3998{
3999 uint64_t action_list_stub[1024 / 64];
4000 struct ofpbuf action_list;
4001
029ca940 4002 ctx->in_action_set = true;
7fdb60a7
SH
4003 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4004 ofpacts_execute_action_set(&action_list, &ctx->action_set);
ed9c9e3e
JR
4005 /* Clear the action set, as it is not needed any more. */
4006 ofpbuf_clear(&ctx->action_set);
6fd6ed71 4007 do_xlate_actions(action_list.data, action_list.size, ctx);
029ca940 4008 ctx->in_action_set = false;
7fdb60a7
SH
4009 ofpbuf_uninit(&action_list);
4010}
4011
e672ff9b
JR
4012static void
4013recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4014{
4015 struct ofpact_unroll_xlate *unroll;
4016
4017 unroll = ctx->last_unroll_offset < 0
4018 ? NULL
4019 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4020 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4021
4022 /* Restore the table_id and rule cookie for a potential PACKET
4023 * IN if needed. */
4024 if (!unroll ||
4025 (ctx->table_id != unroll->rule_table_id
4026 || ctx->rule_cookie != unroll->rule_cookie)) {
4027
4028 ctx->last_unroll_offset = ctx->action_set.size;
4029 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4030 unroll->rule_table_id = ctx->table_id;
4031 unroll->rule_cookie = ctx->rule_cookie;
4032 }
4033}
4034
4035
4036/* Copy remaining actions to the action_set to be executed after recirculation.
4037 * UNROLL_XLATE action is inserted, if not already done so, before actions that
4038 * may generate PACKET_INs from the current table and without matching another
4039 * rule. */
4040static void
4041recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4042 struct xlate_ctx *ctx)
4043{
4044 const struct ofpact *a;
4045
4046 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4047 switch (a->type) {
4048 /* May generate PACKET INs. */
4049 case OFPACT_OUTPUT_REG:
4050 case OFPACT_GROUP:
4051 case OFPACT_OUTPUT:
4052 case OFPACT_CONTROLLER:
4053 case OFPACT_DEC_MPLS_TTL:
4054 case OFPACT_DEC_TTL:
4055 recirc_put_unroll_xlate(ctx);
4056 break;
4057
4058 /* These may not generate PACKET INs. */
4059 case OFPACT_SET_TUNNEL:
4060 case OFPACT_REG_MOVE:
4061 case OFPACT_SET_FIELD:
4062 case OFPACT_STACK_PUSH:
4063 case OFPACT_STACK_POP:
4064 case OFPACT_LEARN:
4065 case OFPACT_WRITE_METADATA:
4066 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4067 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4068 case OFPACT_ENQUEUE:
4069 case OFPACT_SET_VLAN_VID:
4070 case OFPACT_SET_VLAN_PCP:
4071 case OFPACT_STRIP_VLAN:
4072 case OFPACT_PUSH_VLAN:
4073 case OFPACT_SET_ETH_SRC:
4074 case OFPACT_SET_ETH_DST:
4075 case OFPACT_SET_IPV4_SRC:
4076 case OFPACT_SET_IPV4_DST:
4077 case OFPACT_SET_IP_DSCP:
4078 case OFPACT_SET_IP_ECN:
4079 case OFPACT_SET_IP_TTL:
4080 case OFPACT_SET_L4_SRC_PORT:
4081 case OFPACT_SET_L4_DST_PORT:
4082 case OFPACT_SET_QUEUE:
4083 case OFPACT_POP_QUEUE:
4084 case OFPACT_PUSH_MPLS:
4085 case OFPACT_POP_MPLS:
4086 case OFPACT_SET_MPLS_LABEL:
4087 case OFPACT_SET_MPLS_TC:
4088 case OFPACT_SET_MPLS_TTL:
4089 case OFPACT_MULTIPATH:
4090 case OFPACT_BUNDLE:
4091 case OFPACT_EXIT:
4092 case OFPACT_UNROLL_XLATE:
4093 case OFPACT_FIN_TIMEOUT:
4094 case OFPACT_CLEAR_ACTIONS:
4095 case OFPACT_WRITE_ACTIONS:
4096 case OFPACT_METER:
4097 case OFPACT_SAMPLE:
d4abaff5 4098 case OFPACT_DEBUG_RECIRC:
e672ff9b
JR
4099 break;
4100
4101 /* These need not be copied for restoration. */
4102 case OFPACT_NOTE:
4103 case OFPACT_CONJUNCTION:
4104 continue;
4105 }
4106 /* Copy the action over. */
4107 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4108 }
4109}
4110
1d741d6d
JR
4111#define CHECK_MPLS_RECIRCULATION() \
4112 if (ctx->was_mpls) { \
4113 ctx_trigger_recirculation(ctx); \
4114 break; \
4115 }
4116#define CHECK_MPLS_RECIRCULATION_IF(COND) \
4117 if (COND) { \
4118 CHECK_MPLS_RECIRCULATION(); \
4119 }
4120
9583bc14
EJ
4121static void
4122do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4123 struct xlate_ctx *ctx)
4124{
49a73e0c 4125 struct flow_wildcards *wc = ctx->wc;
33bf9176 4126 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
4127 const struct ofpact *a;
4128
a36de779
PS
4129 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4130 tnl_arp_snoop(flow, wc, ctx->xbridge->name);
4131 }
f47ea021
JR
4132 /* dl_type already in the mask, not set below. */
4133
9583bc14
EJ
4134 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4135 struct ofpact_controller *controller;
4136 const struct ofpact_metadata *metadata;
b2dd70be
JR
4137 const struct ofpact_set_field *set_field;
4138 const struct mf_field *mf;
9583bc14 4139
e672ff9b
JR
4140 if (ctx->exit) {
4141 /* Check if need to store the remaining actions for later
4142 * execution. */
4143 if (exit_recirculates(ctx)) {
4144 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4145 ((uint8_t *)a -
4146 (uint8_t *)ofpacts)),
4147 ctx);
4148 }
4149 break;
7bbdd84f
SH
4150 }
4151
9583bc14
EJ
4152 switch (a->type) {
4153 case OFPACT_OUTPUT:
4154 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4155 ofpact_get_OUTPUT(a)->max_len, true);
4156 break;
4157
7395c052 4158 case OFPACT_GROUP:
f4fb341b 4159 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
1d741d6d 4160 /* Group could not be found. */
f4fb341b
SH
4161 return;
4162 }
7395c052
NZ
4163 break;
4164
9583bc14
EJ
4165 case OFPACT_CONTROLLER:
4166 controller = ofpact_get_CONTROLLER(a);
4167 execute_controller_action(ctx, controller->max_len,
4168 controller->reason,
4169 controller->controller_id);
4170 break;
4171
4172 case OFPACT_ENQUEUE:
16194afd
DDP
4173 memset(&wc->masks.skb_priority, 0xff,
4174 sizeof wc->masks.skb_priority);
9583bc14
EJ
4175 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4176 break;
4177
4178 case OFPACT_SET_VLAN_VID:
f74e7df7 4179 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
ca287d20
JR
4180 if (flow->vlan_tci & htons(VLAN_CFI) ||
4181 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4182 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4183 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4184 | htons(VLAN_CFI));
4185 }
9583bc14
EJ
4186 break;
4187
4188 case OFPACT_SET_VLAN_PCP:
f74e7df7 4189 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
ca287d20
JR
4190 if (flow->vlan_tci & htons(VLAN_CFI) ||
4191 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4192 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4193 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4194 << VLAN_PCP_SHIFT) | VLAN_CFI);
4195 }
9583bc14
EJ
4196 break;
4197
4198 case OFPACT_STRIP_VLAN:
f74e7df7 4199 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 4200 flow->vlan_tci = htons(0);
9583bc14
EJ
4201 break;
4202
4203 case OFPACT_PUSH_VLAN:
4204 /* XXX 802.1AD(QinQ) */
f74e7df7 4205 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 4206 flow->vlan_tci = htons(VLAN_CFI);
9583bc14
EJ
4207 break;
4208
4209 case OFPACT_SET_ETH_SRC:
74ff3298
JR
4210 WC_MASK_FIELD(wc, dl_src);
4211 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
9583bc14
EJ
4212 break;
4213
4214 case OFPACT_SET_ETH_DST:
74ff3298
JR
4215 WC_MASK_FIELD(wc, dl_dst);
4216 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
9583bc14
EJ
4217 break;
4218
4219 case OFPACT_SET_IPV4_SRC:
1d741d6d 4220 CHECK_MPLS_RECIRCULATION();
33bf9176 4221 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 4222 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176 4223 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
4224 }
4225 break;
4226
4227 case OFPACT_SET_IPV4_DST:
1d741d6d 4228 CHECK_MPLS_RECIRCULATION();
33bf9176 4229 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 4230 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176 4231 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
4232 }
4233 break;
4234
04f01c24 4235 case OFPACT_SET_IP_DSCP:
1d741d6d 4236 CHECK_MPLS_RECIRCULATION();
04f01c24 4237 if (is_ip_any(flow)) {
f47ea021 4238 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 4239 flow->nw_tos &= ~IP_DSCP_MASK;
04f01c24 4240 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
9583bc14
EJ
4241 }
4242 break;
4243
ff14eb7a 4244 case OFPACT_SET_IP_ECN:
1d741d6d 4245 CHECK_MPLS_RECIRCULATION();
ff14eb7a
JR
4246 if (is_ip_any(flow)) {
4247 wc->masks.nw_tos |= IP_ECN_MASK;
4248 flow->nw_tos &= ~IP_ECN_MASK;
4249 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4250 }
4251 break;
4252
0c20dbe4 4253 case OFPACT_SET_IP_TTL:
1d741d6d 4254 CHECK_MPLS_RECIRCULATION();
0c20dbe4
JR
4255 if (is_ip_any(flow)) {
4256 wc->masks.nw_ttl = 0xff;
4257 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4258 }
4259 break;
4260
9583bc14 4261 case OFPACT_SET_L4_SRC_PORT:
1d741d6d 4262 CHECK_MPLS_RECIRCULATION();
b8778a0d 4263 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
4264 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4265 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176 4266 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
4267 }
4268 break;
4269
4270 case OFPACT_SET_L4_DST_PORT:
1d741d6d 4271 CHECK_MPLS_RECIRCULATION();
b8778a0d 4272 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
4273 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4274 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176 4275 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
4276 }
4277 break;
4278
4279 case OFPACT_RESUBMIT:
4280 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4281 break;
4282
4283 case OFPACT_SET_TUNNEL:
33bf9176 4284 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
4285 break;
4286
4287 case OFPACT_SET_QUEUE:
16194afd
DDP
4288 memset(&wc->masks.skb_priority, 0xff,
4289 sizeof wc->masks.skb_priority);
9583bc14
EJ
4290 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4291 break;
4292
4293 case OFPACT_POP_QUEUE:
16194afd
DDP
4294 memset(&wc->masks.skb_priority, 0xff,
4295 sizeof wc->masks.skb_priority);
33bf9176 4296 flow->skb_priority = ctx->orig_skb_priority;
9583bc14
EJ
4297 break;
4298
4299 case OFPACT_REG_MOVE:
1d741d6d
JR
4300 CHECK_MPLS_RECIRCULATION_IF(
4301 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4302 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
33bf9176 4303 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
9583bc14
EJ
4304 break;
4305
b2dd70be 4306 case OFPACT_SET_FIELD:
1d741d6d
JR
4307 CHECK_MPLS_RECIRCULATION_IF(
4308 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
b2dd70be
JR
4309 set_field = ofpact_get_SET_FIELD(a);
4310 mf = set_field->field;
b2dd70be
JR
4311
4312 /* Set field action only ever overwrites packet's outermost
4313 * applicable header fields. Do nothing if no header exists. */
4479846e
JR
4314 if (mf->id == MFF_VLAN_VID) {
4315 wc->masks.vlan_tci |= htons(VLAN_CFI);
4316 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4317 break;
4318 }
4319 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4320 /* 'dl_type' is already unwildcarded. */
4321 && !eth_type_mpls(flow->dl_type)) {
4322 break;
b2dd70be 4323 }
b8778a0d
JR
4324 /* A flow may wildcard nw_frag. Do nothing if setting a trasport
4325 * header field on a packet that does not have them. */
5e2e998a 4326 mf_mask_field_and_prereqs(mf, wc);
b8778a0d
JR
4327 if (mf_are_prereqs_ok(mf, flow)) {
4328 mf_set_flow_value_masked(mf, &set_field->value,
4329 &set_field->mask, flow);
4330 }
b2dd70be
JR
4331 break;
4332
9583bc14 4333 case OFPACT_STACK_PUSH:
1d741d6d
JR
4334 CHECK_MPLS_RECIRCULATION_IF(
4335 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
33bf9176
BP
4336 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4337 &ctx->stack);
9583bc14
EJ
4338 break;
4339
4340 case OFPACT_STACK_POP:
1d741d6d
JR
4341 CHECK_MPLS_RECIRCULATION_IF(
4342 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
f74e7df7
JP
4343 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4344 &ctx->stack);
9583bc14
EJ
4345 break;
4346
4347 case OFPACT_PUSH_MPLS:
1d741d6d
JR
4348 /* Recirculate if it is an IP packet with a zero ttl. This may
4349 * indicate that the packet was previously MPLS and an MPLS pop
4350 * action converted it to IP. In this case recirculating should
4351 * reveal the IP TTL which is used as the basis for a new MPLS
4352 * LSE. */
4353 CHECK_MPLS_RECIRCULATION_IF(
4354 !flow_count_mpls_labels(flow, wc)
4355 && flow->nw_ttl == 0
4356 && is_ip_any(flow));
8bfd0fda 4357 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
9583bc14
EJ
4358 break;
4359
4360 case OFPACT_POP_MPLS:
1d741d6d 4361 CHECK_MPLS_RECIRCULATION();
8bfd0fda 4362 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
9583bc14
EJ
4363 break;
4364
097d4939 4365 case OFPACT_SET_MPLS_LABEL:
1d741d6d 4366 CHECK_MPLS_RECIRCULATION();
8bfd0fda
BP
4367 compose_set_mpls_label_action(
4368 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
1d741d6d 4369 break;
097d4939
JR
4370
4371 case OFPACT_SET_MPLS_TC:
1d741d6d 4372 CHECK_MPLS_RECIRCULATION();
8bfd0fda 4373 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
097d4939
JR
4374 break;
4375
9583bc14 4376 case OFPACT_SET_MPLS_TTL:
1d741d6d 4377 CHECK_MPLS_RECIRCULATION();
8bfd0fda 4378 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
9583bc14
EJ
4379 break;
4380
4381 case OFPACT_DEC_MPLS_TTL:
1d741d6d 4382 CHECK_MPLS_RECIRCULATION();
9cfef3d0 4383 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 4384 return;
9583bc14
EJ
4385 }
4386 break;
4387
4388 case OFPACT_DEC_TTL:
1d741d6d 4389 CHECK_MPLS_RECIRCULATION();
f74e7df7 4390 wc->masks.nw_ttl = 0xff;
9583bc14 4391 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 4392 return;
9583bc14
EJ
4393 }
4394 break;
4395
4396 case OFPACT_NOTE:
4397 /* Nothing to do. */
4398 break;
4399
4400 case OFPACT_MULTIPATH:
1d741d6d 4401 CHECK_MPLS_RECIRCULATION();
33bf9176 4402 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
9583bc14
EJ
4403 break;
4404
4405 case OFPACT_BUNDLE:
1d741d6d 4406 CHECK_MPLS_RECIRCULATION();
9583bc14
EJ
4407 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4408 break;
4409
4410 case OFPACT_OUTPUT_REG:
4411 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4412 break;
4413
4414 case OFPACT_LEARN:
1d741d6d 4415 CHECK_MPLS_RECIRCULATION();
9583bc14
EJ
4416 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4417 break;
4418
afc3987b
BP
4419 case OFPACT_CONJUNCTION: {
4420 /* A flow with a "conjunction" action represents part of a special
4421 * kind of "set membership match". Such a flow should not actually
4422 * get executed, but it could via, say, a "packet-out", even though
4423 * that wouldn't be useful. Log it to help debugging. */
4424 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4425 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
18080541 4426 break;
afc3987b 4427 }
18080541 4428
9583bc14
EJ
4429 case OFPACT_EXIT:
4430 ctx->exit = true;
4431 break;
4432
e672ff9b
JR
4433 case OFPACT_UNROLL_XLATE: {
4434 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4435
4436 /* Restore translation context data that was stored earlier. */
4437 ctx->table_id = unroll->rule_table_id;
4438 ctx->rule_cookie = unroll->rule_cookie;
4439 break;
4440 }
9583bc14 4441 case OFPACT_FIN_TIMEOUT:
1d741d6d 4442 CHECK_MPLS_RECIRCULATION();
33bf9176 4443 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
4444 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4445 break;
4446
4447 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 4448 ofpbuf_clear(&ctx->action_set);
c61f3870
BP
4449 ctx->xin->flow.actset_output = OFPP_UNSET;
4450 ctx->action_set_has_group = false;
7fdb60a7
SH
4451 break;
4452
4453 case OFPACT_WRITE_ACTIONS:
4454 xlate_write_actions(ctx, a);
9583bc14
EJ
4455 break;
4456
4457 case OFPACT_WRITE_METADATA:
4458 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
4459 flow->metadata &= ~metadata->mask;
4460 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
4461 break;
4462
638a19b0
JR
4463 case OFPACT_METER:
4464 /* Not implemented yet. */
4465 break;
4466
9583bc14 4467 case OFPACT_GOTO_TABLE: {
9583bc14 4468 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14 4469
7bbdd84f
SH
4470 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4471 * than ogt->table_id. This is to allow goto_table actions that
4472 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4473 * after recirculation. */
4474 ovs_assert(ctx->table_id == TBL_INTERNAL
4475 || ctx->table_id < ogt->table_id);
4468099e 4476 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 4477 ogt->table_id, true, true);
9583bc14
EJ
4478 break;
4479 }
4480
4481 case OFPACT_SAMPLE:
4482 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4483 break;
d4abaff5
BP
4484
4485 case OFPACT_DEBUG_RECIRC:
4486 ctx_trigger_recirculation(ctx);
4487 a = ofpact_next(a);
4488 break;
9583bc14 4489 }
1d741d6d
JR
4490
4491 /* Check if need to store this and the remaining actions for later
4492 * execution. */
4493 if (ctx->exit && ctx_first_recirculation_action(ctx)) {
4494 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4495 ((uint8_t *)a -
4496 (uint8_t *)ofpacts)),
4497 ctx);
4498 break;
4499 }
9583bc14 4500 }
9583bc14
EJ
4501}
4502
4503void
4504xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
cc377352
EJ
4505 const struct flow *flow, ofp_port_t in_port,
4506 struct rule_dpif *rule, uint16_t tcp_flags,
1520ef4f
BP
4507 const struct dp_packet *packet, struct flow_wildcards *wc,
4508 struct ofpbuf *odp_actions)
9583bc14
EJ
4509{
4510 xin->ofproto = ofproto;
4511 xin->flow = *flow;
cc377352 4512 xin->flow.in_port.ofp_port = in_port;
c61f3870 4513 xin->flow.actset_output = OFPP_UNSET;
9583bc14
EJ
4514 xin->packet = packet;
4515 xin->may_learn = packet != NULL;
4516 xin->rule = rule;
b256dc52 4517 xin->xcache = NULL;
9583bc14
EJ
4518 xin->ofpacts = NULL;
4519 xin->ofpacts_len = 0;
4520 xin->tcp_flags = tcp_flags;
4521 xin->resubmit_hook = NULL;
4522 xin->report_hook = NULL;
4523 xin->resubmit_stats = NULL;
cdd42eda
JG
4524 xin->recurse = 0;
4525 xin->resubmits = 0;
49a73e0c 4526 xin->wc = wc;
1520ef4f 4527 xin->odp_actions = odp_actions;
e672ff9b
JR
4528
4529 /* Do recirc lookup. */
4530 xin->recirc = flow->recirc_id
4531 ? recirc_id_node_find(flow->recirc_id)
4532 : NULL;
9583bc14
EJ
4533}
4534
4535void
4536xlate_out_uninit(struct xlate_out *xout)
4537{
e672ff9b 4538 if (xout) {
e672ff9b 4539 xlate_out_free_recircs(xout);
9583bc14
EJ
4540 }
4541}
4542
4543/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4544 * into datapath actions, using 'ctx', and discards the datapath actions. */
4545void
4546xlate_actions_for_side_effects(struct xlate_in *xin)
4547{
4548 struct xlate_out xout;
4549
4550 xlate_actions(xin, &xout);
4551 xlate_out_uninit(&xout);
4552}
9583bc14 4553\f
55954f6e
EJ
4554static struct skb_priority_to_dscp *
4555get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4556{
4557 struct skb_priority_to_dscp *pdscp;
4558 uint32_t hash;
4559
4560 hash = hash_int(skb_priority, 0);
4561 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4562 if (pdscp->skb_priority == skb_priority) {
4563 return pdscp;
4564 }
4565 }
4566 return NULL;
4567}
4568
4569static bool
4570dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4571 uint8_t *dscp)
4572{
4573 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4574 *dscp = pdscp ? pdscp->dscp : 0;
4575 return pdscp != NULL;
4576}
4577
16194afd
DDP
4578static size_t
4579count_skb_priorities(const struct xport *xport)
4580{
4581 return hmap_count(&xport->skb_priorities);
4582}
4583
55954f6e
EJ
4584static void
4585clear_skb_priorities(struct xport *xport)
4586{
4587 struct skb_priority_to_dscp *pdscp, *next;
4588
4589 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4590 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4591 free(pdscp);
4592 }
4593}
4594
ce4a6b76
BP
4595static bool
4596actions_output_to_local_port(const struct xlate_ctx *ctx)
4597{
46c88433 4598 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
4599 const struct nlattr *a;
4600 unsigned int left;
4601
1520ef4f
BP
4602 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4603 ctx->odp_actions->size) {
ce4a6b76
BP
4604 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4605 && nl_attr_get_odp_port(a) == local_odp_port) {
4606 return true;
4607 }
4608 }
4609 return false;
4610}
9583bc14 4611
5e2a6702 4612#if defined(__linux__)
7d031d7e
BP
4613/* Returns the maximum number of packets that the Linux kernel is willing to
4614 * queue up internally to certain kinds of software-implemented ports, or the
4615 * default (and rarely modified) value if it cannot be determined. */
4616static int
4617netdev_max_backlog(void)
4618{
4619 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4620 static int max_backlog = 1000; /* The normal default value. */
4621
4622 if (ovsthread_once_start(&once)) {
4623 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4624 FILE *stream;
4625 int n;
4626
4627 stream = fopen(filename, "r");
4628 if (!stream) {
120c348f 4629 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
7d031d7e
BP
4630 } else {
4631 if (fscanf(stream, "%d", &n) != 1) {
4632 VLOG_WARN("%s: read error", filename);
4633 } else if (n <= 100) {
4634 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4635 } else {
4636 max_backlog = n;
4637 }
4638 fclose(stream);
4639 }
4640 ovsthread_once_done(&once);
4641
4642 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4643 }
4644
4645 return max_backlog;
4646}
4647
4648/* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4649 * 'odp_actions'. */
4650static int
4651count_output_actions(const struct ofpbuf *odp_actions)
4652{
4653 const struct nlattr *a;
4654 size_t left;
4655 int n = 0;
4656
6fd6ed71 4657 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
7d031d7e
BP
4658 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4659 n++;
4660 }
4661 }
4662 return n;
4663}
5e2a6702 4664#endif /* defined(__linux__) */
7d031d7e
BP
4665
4666/* Returns true if 'odp_actions' contains more output actions than the datapath
4667 * can reliably handle in one go. On Linux, this is the value of the
4668 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4669 * packets that the kernel is willing to queue up for processing while the
4670 * datapath is processing a set of actions. */
4671static bool
5e2a6702 4672too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
7d031d7e
BP
4673{
4674#ifdef __linux__
6fd6ed71 4675 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
7d031d7e
BP
4676 && count_output_actions(odp_actions) > netdev_max_backlog());
4677#else
4678 /* OSes other than Linux might have similar limits, but we don't know how
4679 * to determine them.*/
4680 return false;
4681#endif
4682}
4683
234c3da9
BP
4684static void
4685xlate_wc_init(struct xlate_ctx *ctx)
4686{
4687 flow_wildcards_init_catchall(ctx->wc);
4688
4689 /* Some fields we consider to always be examined. */
5e2e998a
JR
4690 WC_MASK_FIELD(ctx->wc, in_port);
4691 WC_MASK_FIELD(ctx->wc, dl_type);
234c3da9 4692 if (is_ip_any(&ctx->xin->flow)) {
5e2e998a 4693 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
234c3da9
BP
4694 }
4695
4696 if (ctx->xbridge->support.odp.recirc) {
4697 /* Always exactly match recirc_id when datapath supports
4698 * recirculation. */
5e2e998a 4699 WC_MASK_FIELD(ctx->wc, recirc_id);
234c3da9
BP
4700 }
4701
4702 if (ctx->xbridge->netflow) {
4703 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
4704 }
4705
4706 tnl_wc_init(&ctx->xin->flow, ctx->wc);
4707}
4708
4709static void
4710xlate_wc_finish(struct xlate_ctx *ctx)
4711{
4712 /* Clear the metadata and register wildcard masks, because we won't
4713 * use non-header fields as part of the cache. */
4714 flow_wildcards_clear_non_packet_fields(ctx->wc);
4715
4716 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
4717 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
4718 * represent these fields. The datapath interface, on the other hand,
4719 * represents them with just 8 bits each. This means that if the high
4720 * 8 bits of the masks for these fields somehow become set, then they
4721 * will get chopped off by a round trip through the datapath, and
4722 * revalidation will spot that as an inconsistency and delete the flow.
4723 * Avoid the problem here by making sure that only the low 8 bits of
4724 * either field can be unwildcarded for ICMP.
4725 */
4726 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
4727 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
4728 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
4729 }
4730 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
4731 if (ctx->wc->masks.vlan_tci) {
4732 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
4733 }
4734}
4735
e672ff9b
JR
4736/* Translates the flow, actions, or rule in 'xin' into datapath actions in
4737 * 'xout'.
56450a41
BP
4738 * The caller must take responsibility for eventually freeing 'xout', with
4739 * xlate_out_uninit(). */
84f0f298
RW
4740void
4741xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
9583bc14 4742{
e467ea42
BP
4743 *xout = (struct xlate_out) {
4744 .slow = 0,
4745 .fail_open = false,
e467ea42
BP
4746 .n_recircs = 0,
4747 };
4748
84f0f298 4749 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
bb00fdef
BP
4750 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
4751 if (!xbridge) {
4752 return;
4753 }
4754
33bf9176
BP
4755 struct flow *flow = &xin->flow;
4756
bb00fdef
BP
4757 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
4758 uint64_t action_set_stub[1024 / 8];
49a73e0c 4759 struct flow_wildcards scratch_wc;
1520ef4f
BP
4760 uint64_t actions_stub[256 / 8];
4761 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
bb00fdef
BP
4762 struct xlate_ctx ctx = {
4763 .xin = xin,
4764 .xout = xout,
4765 .base_flow = *flow,
4766 .orig_tunnel_ip_dst = flow->tunnel.ip_dst,
4767 .xbridge = xbridge,
4768 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
4769 .rule = xin->rule,
49a73e0c 4770 .wc = xin->wc ? xin->wc : &scratch_wc,
1520ef4f 4771 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
bb00fdef 4772
cdd42eda
JG
4773 .recurse = xin->recurse,
4774 .resubmits = xin->resubmits,
bb00fdef
BP
4775 .in_group = false,
4776 .in_action_set = false,
4777
4778 .table_id = 0,
4779 .rule_cookie = OVS_BE64_MAX,
4780 .orig_skb_priority = flow->skb_priority,
4781 .sflow_n_outputs = 0,
4782 .sflow_odp_port = 0,
2031ef97 4783 .nf_output_iface = NF_OUT_DROP,
bb00fdef 4784 .exit = false,
3d6151f3 4785 .mirrors = 0,
bb00fdef
BP
4786
4787 .recirc_action_offset = -1,
4788 .last_unroll_offset = -1,
4789
4790 .was_mpls = false,
4791
4792 .action_set_has_group = false,
4793 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
4794 };
865ca6cf
BP
4795
4796 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
4797 * the packet as the datapath will treat it for output actions:
4798 *
4799 * - Our datapath doesn't retain tunneling information without us
4800 * re-setting it, so clear the tunnel data.
4801 *
4802 * - For VLAN splinters, a higher layer may pretend that the packet
4803 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
4804 * attached, because that's how we want to treat it from an OpenFlow
4805 * perspective. But from the datapath's perspective it actually came
4806 * in on a VLAN device without any VLAN attached. So here we put the
4807 * datapath's view of the VLAN information in 'base_flow' to ensure
4808 * correct treatment.
4809 */
bb00fdef 4810 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
865ca6cf
BP
4811 if (flow->in_port.ofp_port
4812 != vsp_realdev_to_vlandev(xbridge->ofproto,
4813 flow->in_port.ofp_port,
4814 flow->vlan_tci)) {
4815 ctx.base_flow.vlan_tci = 0;
4816 }
4817
1520ef4f 4818 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
234c3da9
BP
4819 if (xin->wc) {
4820 xlate_wc_init(&ctx);
4821 }
bb00fdef 4822
46c88433 4823 COVERAGE_INC(xlate_actions);
9583bc14 4824
e672ff9b 4825 if (xin->recirc) {
2082425c 4826 const struct recirc_state *state = &xin->recirc->state;
e672ff9b 4827
d6bef3cc
BP
4828 xlate_report(&ctx, "Restoring state post-recirculation:");
4829
e672ff9b
JR
4830 if (xin->ofpacts_len > 0 || ctx.rule) {
4831 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
d6bef3cc 4832 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
e672ff9b 4833
d6bef3cc
BP
4834 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
4835 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
1520ef4f 4836 goto exit;
e672ff9b
JR
4837 }
4838
4839 /* Set the bridge for post-recirculation processing if needed. */
2082425c 4840 if (ctx.xbridge->ofproto != state->ofproto) {
e672ff9b 4841 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2082425c
BP
4842 const struct xbridge *new_bridge
4843 = xbridge_lookup(xcfg, state->ofproto);
e672ff9b
JR
4844
4845 if (OVS_UNLIKELY(!new_bridge)) {
4846 /* Drop the packet if the bridge cannot be found. */
4847 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4848 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
d6bef3cc 4849 xlate_report(&ctx, "- Recirculation bridge no longer exists.");
1520ef4f 4850 goto exit;
e672ff9b
JR
4851 }
4852 ctx.xbridge = new_bridge;
4853 }
4854
4855 /* Set the post-recirculation table id. Note: A table lookup is done
4856 * only if there are no post-recirculation actions. */
2082425c 4857 ctx.table_id = state->table_id;
d6bef3cc 4858 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
e672ff9b
JR
4859
4860 /* Restore pipeline metadata. May change flow's in_port and other
4861 * metadata to the values that existed when recirculation was
4862 * triggered. */
2082425c 4863 recirc_metadata_to_flow(&state->metadata, flow);
e672ff9b
JR
4864
4865 /* Restore stack, if any. */
2082425c
BP
4866 if (state->stack) {
4867 ofpbuf_put(&ctx.stack, state->stack->data, state->stack->size);
e672ff9b
JR
4868 }
4869
29bae541
BP
4870 /* Restore mirror state. */
4871 ctx.mirrors = state->mirrors;
4872
e672ff9b 4873 /* Restore action set, if any. */
2082425c 4874 if (state->action_set_len) {
e672ff9b
JR
4875 const struct ofpact *a;
4876
d6bef3cc
BP
4877 xlate_report_actions(&ctx, "- Restoring action set",
4878 state->ofpacts, state->action_set_len);
4879
2082425c 4880 ofpbuf_put(&ctx.action_set, state->ofpacts, state->action_set_len);
e672ff9b 4881
2082425c 4882 OFPACT_FOR_EACH(a, state->ofpacts, state->action_set_len) {
e672ff9b
JR
4883 if (a->type == OFPACT_GROUP) {
4884 ctx.action_set_has_group = true;
4885 break;
4886 }
4887 }
4888 }
4889
4890 /* Restore recirculation actions. If there are no actions, processing
4891 * will start with a lookup in the table set above. */
2082425c
BP
4892 if (state->ofpacts_len > state->action_set_len) {
4893 xin->ofpacts_len = state->ofpacts_len - state->action_set_len;
4894 xin->ofpacts = state->ofpacts +
4895 state->action_set_len / sizeof *state->ofpacts;
d6bef3cc
BP
4896
4897 xlate_report_actions(&ctx, "- Restoring actions",
4898 xin->ofpacts, xin->ofpacts_len);
e672ff9b
JR
4899 }
4900 } else if (OVS_UNLIKELY(flow->recirc_id)) {
4901 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4902
4903 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
4904 flow->recirc_id);
1520ef4f 4905 goto exit;
e672ff9b 4906 }
621b8064
JR
4907 /* The bridge is now known so obtain its table version. */
4908 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
9583bc14 4909
10c44245 4910 if (!xin->ofpacts && !ctx.rule) {
b2e89cc9
BP
4911 ctx.rule = rule_dpif_lookup_from_table(
4912 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
1e1e1d19 4913 ctx.xin->resubmit_stats, &ctx.table_id,
b2e89cc9 4914 flow->in_port.ofp_port, true, true);
10c44245 4915 if (ctx.xin->resubmit_stats) {
b2e89cc9 4916 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
10c44245 4917 }
b256dc52
JS
4918 if (ctx.xin->xcache) {
4919 struct xc_entry *entry;
4920
4921 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
b2e89cc9 4922 entry->u.rule = ctx.rule;
1e1e1d19 4923 rule_dpif_ref(ctx.rule);
b256dc52 4924 }
a8c31348
BP
4925
4926 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
b2e89cc9 4927 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
a8c31348 4928 }
10c44245 4929 }
ee1afdd5 4930 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
10c44245 4931
14d2b8b2
BP
4932 /* Get the proximate input port of the packet. (If xin->recirc,
4933 * flow->in_port is the ultimate input port of the packet.) */
4934 struct xport *in_port = get_ofp_port(xbridge,
4935 ctx.base_flow.in_port.ofp_port);
4936
e672ff9b
JR
4937 /* Tunnel stats only for non-recirculated packets. */
4938 if (!xin->recirc && in_port && in_port->is_tunnel) {
b256dc52
JS
4939 if (ctx.xin->resubmit_stats) {
4940 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4941 if (in_port->bfd) {
4942 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4943 }
4944 }
4945 if (ctx.xin->xcache) {
4946 struct xc_entry *entry;
4947
4948 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4949 entry->u.dev.rx = netdev_ref(in_port->netdev);
4950 entry->u.dev.bfd = bfd_ref(in_port->bfd);
d6fc5f57
EJ
4951 }
4952 }
4953
bef1403e
BP
4954 if (!xin->recirc && process_special(&ctx, in_port)) {
4955 /* process_special() did all the processing for this packet.
4956 *
4957 * We do not perform special processing on recirculated packets, as
4958 * recirculated packets are not really received by the bridge.*/
4959 } else if (in_port && in_port->xbundle
4960 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
4961 if (ctx.xin->packet != NULL) {
4962 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4963 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4964 "%s, which is reserved exclusively for mirroring",
4965 ctx.xbridge->name, in_port->xbundle->name);
4966 }
4967 } else {
e672ff9b 4968 /* Sampling is done only for packets really received by the bridge. */
a6092018 4969 unsigned int user_cookie_offset = 0;
e672ff9b 4970 if (!xin->recirc) {
a6092018
BP
4971 user_cookie_offset = compose_sflow_action(&ctx);
4972 compose_ipfix_action(&ctx, ODPP_NONE);
e672ff9b 4973 }
0731abc5 4974 size_t sample_actions_len = ctx.odp_actions->size;
9583bc14 4975
234c3da9
BP
4976 if (tnl_process_ecn(flow)
4977 && (!in_port || may_receive(in_port, &ctx))) {
1806291d
BP
4978 const struct ofpact *ofpacts;
4979 size_t ofpacts_len;
4980
4981 if (xin->ofpacts) {
4982 ofpacts = xin->ofpacts;
4983 ofpacts_len = xin->ofpacts_len;
4984 } else if (ctx.rule) {
4985 const struct rule_actions *actions
4986 = rule_dpif_get_actions(ctx.rule);
4987 ofpacts = actions->ofpacts;
4988 ofpacts_len = actions->ofpacts_len;
4989 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
4990 } else {
4991 OVS_NOT_REACHED();
4992 }
4993
7efbc3b7 4994 mirror_ingress_packet(&ctx);
9583bc14
EJ
4995 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4996
4997 /* We've let OFPP_NORMAL and the learning action look at the
4998 * packet, so drop it now if forwarding is disabled. */
9efd308e
DV
4999 if (in_port && (!xport_stp_forward_state(in_port) ||
5000 !xport_rstp_forward_state(in_port))) {
8ec90813 5001 /* Drop all actions added by do_xlate_actions() above. */
1520ef4f 5002 ctx.odp_actions->size = sample_actions_len;
e672ff9b
JR
5003
5004 /* Undo changes that may have been done for recirculation. */
5005 if (exit_recirculates(&ctx)) {
5006 ctx.action_set.size = ctx.recirc_action_offset;
5007 ctx.recirc_action_offset = -1;
5008 ctx.last_unroll_offset = -1;
5009 }
8ec90813 5010 } else if (ctx.action_set.size) {
e672ff9b
JR
5011 /* Translate action set only if not dropping the packet and
5012 * not recirculating. */
5013 if (!exit_recirculates(&ctx)) {
5014 xlate_action_set(&ctx);
5015 }
5016 }
5017 /* Check if need to recirculate. */
5018 if (exit_recirculates(&ctx)) {
5019 compose_recirculate_action(&ctx);
9583bc14
EJ
5020 }
5021 }
5022
e672ff9b
JR
5023 /* Output only fully processed packets. */
5024 if (!exit_recirculates(&ctx)
5025 && xbridge->has_in_band
ce4a6b76
BP
5026 && in_band_must_output_to_local_port(flow)
5027 && !actions_output_to_local_port(&ctx)) {
e93ef1c7 5028 compose_output_action(&ctx, OFPP_LOCAL, NULL);
9583bc14 5029 }
aaa0fbae 5030
a6092018
BP
5031 if (user_cookie_offset) {
5032 fix_sflow_action(&ctx, user_cookie_offset);
e672ff9b 5033 }
9583bc14
EJ
5034 }
5035
1520ef4f 5036 if (nl_attr_oversized(ctx.odp_actions->size)) {
542024c4 5037 /* These datapath actions are too big for a Netlink attribute, so we
0f032e95
BP
5038 * can't hand them to the kernel directly. dpif_execute() can execute
5039 * them one by one with help, so just mark the result as SLOW_ACTION to
5040 * prevent the flow from being installed. */
5041 COVERAGE_INC(xlate_actions_oversize);
5042 ctx.xout->slow |= SLOW_ACTION;
1520ef4f 5043 } else if (too_many_output_actions(ctx.odp_actions)) {
7d031d7e
BP
5044 COVERAGE_INC(xlate_actions_too_many_output);
5045 ctx.xout->slow |= SLOW_ACTION;
542024c4
BP
5046 }
5047
1806291d
BP
5048 /* Do netflow only for packets really received by the bridge and not sent
5049 * to the controller. We consider packets sent to the controller to be
5050 * part of the control plane rather than the data plane. */
5051 if (!xin->recirc && xbridge->netflow && !(xout->slow & SLOW_CONTROLLER)) {
5052 if (ctx.xin->resubmit_stats) {
5053 netflow_flow_update(xbridge->netflow, flow,
2031ef97 5054 ctx.nf_output_iface,
1806291d
BP
5055 ctx.xin->resubmit_stats);
5056 }
5057 if (ctx.xin->xcache) {
5058 struct xc_entry *entry;
b256dc52 5059
1806291d
BP
5060 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5061 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5062 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
2031ef97 5063 entry->u.nf.iface = ctx.nf_output_iface;
d6fc5f57
EJ
5064 }
5065 }
5066
49a73e0c 5067 if (xin->wc) {
234c3da9 5068 xlate_wc_finish(&ctx);
c56fac1b 5069 }
1520ef4f
BP
5070
5071exit:
5072 ofpbuf_uninit(&ctx.stack);
5073 ofpbuf_uninit(&ctx.action_set);
5074 ofpbuf_uninit(&scratch_actions);
91d6cd12
AW
5075}
5076
5077/* Sends 'packet' out 'ofport'.
5078 * May modify 'packet'.
5079 * Returns 0 if successful, otherwise a positive errno value. */
5080int
cf62fa4c 5081xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
91d6cd12 5082{
84f0f298 5083 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
91d6cd12 5084 struct xport *xport;
91d6cd12 5085 struct ofpact_output output;
91d6cd12 5086 struct flow flow;
91d6cd12 5087
91d6cd12
AW
5088 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5089 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
cf62fa4c 5090 flow_extract(packet, &flow);
b5e7e61a 5091 flow.in_port.ofp_port = OFPP_NONE;
91d6cd12 5092
84f0f298 5093 xport = xport_lookup(xcfg, ofport);
91d6cd12 5094 if (!xport) {
02ea2703 5095 return EINVAL;
91d6cd12 5096 }
91d6cd12
AW
5097 output.port = xport->ofp_port;
5098 output.max_len = 0;
e491a67a
YT
5099
5100 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5101 &output.ofpact, sizeof output,
5102 packet);
9583bc14 5103}
b256dc52
JS
5104
5105struct xlate_cache *
5106xlate_cache_new(void)
5107{
5108 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5109
5110 ofpbuf_init(&xcache->entries, 512);
5111 return xcache;
5112}
5113
5114static struct xc_entry *
5115xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5116{
5117 struct xc_entry *entry;
5118
5119 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5120 entry->type = type;
5121
5122 return entry;
5123}
5124
5125static void
5126xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5127{
5128 if (entry->u.dev.tx) {
5129 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5130 }
5131 if (entry->u.dev.rx) {
5132 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5133 }
5134 if (entry->u.dev.bfd) {
5135 bfd_account_rx(entry->u.dev.bfd, stats);
5136 }
5137}
5138
5139static void
5140xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5141{
84f0f298 5142 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
b256dc52
JS
5143 struct xbridge *xbridge;
5144 struct xbundle *xbundle;
5145 struct flow_wildcards wc;
5146
84f0f298 5147 xbridge = xbridge_lookup(xcfg, ofproto);
b256dc52
JS
5148 if (!xbridge) {
5149 return;
5150 }
5151
5152 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5153 NULL);
5154 if (!xbundle) {
5155 return;
5156 }
5157
5158 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5159}
5160
5161/* Push stats and perform side effects of flow translation. */
5162void
0725e747 5163xlate_push_stats(struct xlate_cache *xcache,
b256dc52
JS
5164 const struct dpif_flow_stats *stats)
5165{
5166 struct xc_entry *entry;
5167 struct ofpbuf entries = xcache->entries;
74ff3298 5168 struct eth_addr dmac;
b256dc52 5169
bc388583
BP
5170 if (!stats->n_packets) {
5171 return;
5172 }
5173
b256dc52
JS
5174 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5175 switch (entry->type) {
5176 case XC_RULE:
5177 rule_dpif_credit_stats(entry->u.rule, stats);
5178 break;
5179 case XC_BOND:
5180 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5181 entry->u.bond.vid, stats->n_bytes);
5182 break;
5183 case XC_NETDEV:
5184 xlate_cache_netdev(entry, stats);
5185 break;
5186 case XC_NETFLOW:
5187 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5188 entry->u.nf.iface, stats);
5189 break;
5190 case XC_MIRROR:
5191 mirror_update_stats(entry->u.mirror.mbridge,
5192 entry->u.mirror.mirrors,
5193 stats->n_packets, stats->n_bytes);
5194 break;
5195 case XC_LEARN:
0725e747 5196 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
b256dc52
JS
5197 break;
5198 case XC_NORMAL:
0725e747
BP
5199 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5200 entry->u.normal.vlan);
b256dc52
JS
5201 break;
5202 case XC_FIN_TIMEOUT:
5203 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5204 entry->u.fin.idle, entry->u.fin.hard);
5205 break;
1e684d7d
RW
5206 case XC_GROUP:
5207 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5208 stats);
5209 break;
a36de779
PS
5210 case XC_TNL_ARP:
5211 /* Lookup arp to avoid arp timeout. */
74ff3298
JR
5212 tnl_arp_lookup(entry->u.tnl_arp_cache.br_name,
5213 entry->u.tnl_arp_cache.d_ip, &dmac);
a36de779 5214 break;
b256dc52
JS
5215 default:
5216 OVS_NOT_REACHED();
5217 }
5218 }
5219}
5220
5221static void
5222xlate_dev_unref(struct xc_entry *entry)
5223{
5224 if (entry->u.dev.tx) {
5225 netdev_close(entry->u.dev.tx);
5226 }
5227 if (entry->u.dev.rx) {
5228 netdev_close(entry->u.dev.rx);
5229 }
5230 if (entry->u.dev.bfd) {
5231 bfd_unref(entry->u.dev.bfd);
5232 }
5233}
5234
5235static void
5236xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5237{
b256dc52
JS
5238 netflow_flow_clear(netflow, flow);
5239 netflow_unref(netflow);
5240 free(flow);
5241}
5242
5243void
5244xlate_cache_clear(struct xlate_cache *xcache)
5245{
5246 struct xc_entry *entry;
5247 struct ofpbuf entries;
5248
5249 if (!xcache) {
5250 return;
5251 }
5252
5253 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5254 switch (entry->type) {
5255 case XC_RULE:
5256 rule_dpif_unref(entry->u.rule);
5257 break;
5258 case XC_BOND:
5259 free(entry->u.bond.flow);
5260 bond_unref(entry->u.bond.bond);
5261 break;
5262 case XC_NETDEV:
5263 xlate_dev_unref(entry);
5264 break;
5265 case XC_NETFLOW:
5266 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5267 break;
5268 case XC_MIRROR:
5269 mbridge_unref(entry->u.mirror.mbridge);
5270 break;
5271 case XC_LEARN:
4165b5e0
JS
5272 free(entry->u.learn.fm);
5273 ofpbuf_delete(entry->u.learn.ofpacts);
b256dc52
JS
5274 break;
5275 case XC_NORMAL:
5276 free(entry->u.normal.flow);
5277 break;
5278 case XC_FIN_TIMEOUT:
83709dfa
JR
5279 /* 'u.fin.rule' is always already held as a XC_RULE, which
5280 * has already released it's reference above. */
b256dc52 5281 break;
1e684d7d
RW
5282 case XC_GROUP:
5283 group_dpif_unref(entry->u.group.group);
5284 break;
a36de779
PS
5285 case XC_TNL_ARP:
5286 break;
b256dc52
JS
5287 default:
5288 OVS_NOT_REACHED();
5289 }
5290 }
5291
5292 ofpbuf_clear(&xcache->entries);
5293}
5294
5295void
5296xlate_cache_delete(struct xlate_cache *xcache)
5297{
5298 xlate_cache_clear(xcache);
5299 ofpbuf_uninit(&xcache->entries);
5300 free(xcache);
5301}