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