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