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