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