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