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