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