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