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