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