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