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