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