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