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