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