1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
17 #include "ofproto/ofproto-dpif-xlate.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
52 COVERAGE_DEFINE(xlate_actions);
53 COVERAGE_DEFINE(xlate_actions_oversize);
54 COVERAGE_DEFINE(xlate_actions_mpls_overflow);
56 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
58 /* Maximum depth of flow table recursion (due to resubmit actions) in a
59 * flow translation. */
60 #define MAX_RESUBMIT_RECURSION 64
61 #define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
64 /* Maximum number of resubmit actions in a flow translation, whether they are
65 * recursive or not. */
66 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
68 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
71 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
72 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
74 struct list xbundles; /* Owned xbundles. */
75 struct hmap xports; /* Indexed by ofp_port. */
77 char *name; /* Name used in log messages. */
78 struct dpif *dpif; /* Datapath interface. */
79 struct mac_learning *ml; /* Mac learning handle. */
80 struct mbridge *mbridge; /* Mirroring. */
81 struct dpif_sflow *sflow; /* SFlow handle, or null. */
82 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
83 struct netflow *netflow; /* Netflow handle, or null. */
84 struct stp *stp; /* STP or null if disabled. */
86 /* Special rules installed by ofproto-dpif. */
87 struct rule_dpif *miss_rule;
88 struct rule_dpif *no_packet_in_rule;
90 enum ofp_config_flags frag; /* Fragmentation handling. */
91 bool has_in_band; /* Bridge has in band control? */
92 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
94 /* True if the datapath supports recirculation. */
97 /* True if the datapath supports variable-length
98 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
99 * False if the datapath supports only 8-byte (or shorter) userdata. */
100 bool variable_length_userdata;
102 /* Number of MPLS label stack entries that the datapath supports
104 size_t max_mpls_depth;
108 struct hmap_node hmap_node; /* In global 'xbundles' map. */
109 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
111 struct list list_node; /* In parent 'xbridges' list. */
112 struct xbridge *xbridge; /* Parent xbridge. */
114 struct list xports; /* Contains "struct xport"s. */
116 char *name; /* Name used in log messages. */
117 struct bond *bond; /* Nonnull iff more than one port. */
118 struct lacp *lacp; /* LACP handle or null. */
120 enum port_vlan_mode vlan_mode; /* VLAN mode. */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
125 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
129 struct hmap_node hmap_node; /* Node in global 'xports' map. */
130 struct ofport_dpif *ofport; /* Key in global 'xports map. */
132 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
133 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
135 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
137 struct list bundle_node; /* In parent xbundle (if it exists). */
138 struct xbundle *xbundle; /* Parent xbundle or null. */
140 struct netdev *netdev; /* 'ofport''s netdev. */
142 struct xbridge *xbridge; /* Parent bridge. */
143 struct xport *peer; /* Patch port peer or null. */
145 enum ofputil_port_config config; /* OpenFlow port configuration. */
146 enum ofputil_port_state state; /* OpenFlow port state. */
147 int stp_port_no; /* STP port number or -1 if not in use. */
149 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
151 bool may_enable; /* May be enabled in bonds. */
152 bool is_tunnel; /* Is a tunnel port. */
154 struct cfm *cfm; /* CFM handle or null. */
155 struct bfd *bfd; /* BFD handle or null. */
159 struct xlate_in *xin;
160 struct xlate_out *xout;
162 const struct xbridge *xbridge;
164 /* Flow at the last commit. */
165 struct flow base_flow;
167 /* Tunnel IP destination address as received. This is stored separately
168 * as the base_flow.tunnel is cleared on init to reflect the datapath
169 * behavior. Used to make sure not to send tunneled output to ourselves,
170 * which might lead to an infinite loop. This could happen easily
171 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
172 * actually set the tun_dst field. */
173 ovs_be32 orig_tunnel_ip_dst;
175 /* Stack for the push and pop actions. Each stack element is of type
176 * "union mf_subvalue". */
177 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
180 /* The rule that we are currently translating, or NULL. */
181 struct rule_dpif *rule;
183 /* Resubmit statistics, via xlate_table_action(). */
184 int recurse; /* Current resubmit nesting depth. */
185 int resubmits; /* Total number of resubmits. */
186 bool in_group; /* Currently translating ofgroup, if true. */
188 uint32_t orig_skb_priority; /* Priority when packet arrived. */
189 uint8_t table_id; /* OpenFlow table ID where flow was found. */
190 uint32_t sflow_n_outputs; /* Number of output ports. */
191 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
192 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
193 bool exit; /* No further actions should be processed. */
195 bool use_recirc; /* Should generate recirc? */
196 struct xlate_recirc recirc; /* Information used for generating
197 * recirculation actions */
199 /* OpenFlow 1.1+ action set.
201 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
202 * When translation is otherwise complete, ofpacts_execute_action_set()
203 * converts it to a set of "struct ofpact"s that can be translated into
204 * datapath actions. */
205 struct ofpbuf action_set; /* Action set. */
206 uint64_t action_set_stub[1024 / 8];
209 /* A controller may use OFPP_NONE as the ingress port to indicate that
210 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
211 * when an input bundle is needed for validation (e.g., mirroring or
212 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
213 * any 'port' structs, so care must be taken when dealing with it. */
214 static struct xbundle ofpp_none_bundle = {
216 .vlan_mode = PORT_VLAN_TRUNK
219 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
220 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
221 * traffic egressing the 'ofport' with that priority should be marked with. */
222 struct skb_priority_to_dscp {
223 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
224 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
226 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
240 /* xlate_cache entries hold enough information to perform the side effects of
241 * xlate_actions() for a rule, without needing to perform rule translation
242 * from scratch. The primary usage of these is to submit statistics to objects
243 * that a flow relates to, although they may be used for other effects as well
244 * (for instance, refreshing hard timeouts for learned flows). */
248 struct rule_dpif *rule;
255 struct netflow *netflow;
260 struct mbridge *mbridge;
261 mirror_mask_t mirrors;
269 struct ofproto_dpif *ofproto;
270 struct ofputil_flow_mod *fm;
271 struct ofpbuf *ofpacts;
274 struct ofproto_dpif *ofproto;
279 struct rule_dpif *rule;
286 #define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
287 entries = xcache->entries; \
288 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
290 entry = ofpbuf_try_pull(&entries, sizeof *entry))
293 struct ofpbuf entries;
296 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
297 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
298 static struct hmap xports = HMAP_INITIALIZER(&xports);
300 static bool may_receive(const struct xport *, struct xlate_ctx *);
301 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
303 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
304 OVS_REQ_RDLOCK(xlate_rwlock);
305 static void xlate_normal(struct xlate_ctx *);
306 static void xlate_report(struct xlate_ctx *, const char *);
307 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
308 uint8_t table_id, bool may_packet_in,
309 bool honor_table_miss);
310 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
311 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
312 static void output_normal(struct xlate_ctx *, const struct xbundle *,
314 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
316 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
317 static struct xbundle *xbundle_lookup(const struct ofbundle *);
318 static struct xport *xport_lookup(const struct ofport_dpif *);
319 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
320 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
321 uint32_t skb_priority);
322 static void clear_skb_priorities(struct xport *);
323 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
326 static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
330 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
331 struct dpif *dpif, struct rule_dpif *miss_rule,
332 struct rule_dpif *no_packet_in_rule,
333 const struct mac_learning *ml, struct stp *stp,
334 const struct mbridge *mbridge,
335 const struct dpif_sflow *sflow,
336 const struct dpif_ipfix *ipfix,
337 const struct netflow *netflow, enum ofp_config_flags frag,
338 bool forward_bpdu, bool has_in_band,
340 bool variable_length_userdata,
341 size_t max_mpls_depth)
343 struct xbridge *xbridge = xbridge_lookup(ofproto);
346 xbridge = xzalloc(sizeof *xbridge);
347 xbridge->ofproto = ofproto;
349 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
350 hmap_init(&xbridge->xports);
351 list_init(&xbridge->xbundles);
354 if (xbridge->ml != ml) {
355 mac_learning_unref(xbridge->ml);
356 xbridge->ml = mac_learning_ref(ml);
359 if (xbridge->mbridge != mbridge) {
360 mbridge_unref(xbridge->mbridge);
361 xbridge->mbridge = mbridge_ref(mbridge);
364 if (xbridge->sflow != sflow) {
365 dpif_sflow_unref(xbridge->sflow);
366 xbridge->sflow = dpif_sflow_ref(sflow);
369 if (xbridge->ipfix != ipfix) {
370 dpif_ipfix_unref(xbridge->ipfix);
371 xbridge->ipfix = dpif_ipfix_ref(ipfix);
374 if (xbridge->stp != stp) {
375 stp_unref(xbridge->stp);
376 xbridge->stp = stp_ref(stp);
379 if (xbridge->netflow != netflow) {
380 netflow_unref(xbridge->netflow);
381 xbridge->netflow = netflow_ref(netflow);
385 xbridge->name = xstrdup(name);
387 xbridge->dpif = dpif;
388 xbridge->forward_bpdu = forward_bpdu;
389 xbridge->has_in_band = has_in_band;
390 xbridge->frag = frag;
391 xbridge->miss_rule = miss_rule;
392 xbridge->no_packet_in_rule = no_packet_in_rule;
393 xbridge->enable_recirc = enable_recirc;
394 xbridge->variable_length_userdata = variable_length_userdata;
395 xbridge->max_mpls_depth = max_mpls_depth;
399 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
401 struct xbridge *xbridge = xbridge_lookup(ofproto);
402 struct xbundle *xbundle, *next_xbundle;
403 struct xport *xport, *next_xport;
409 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
410 xlate_ofport_remove(xport->ofport);
413 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
414 xlate_bundle_remove(xbundle->ofbundle);
417 hmap_remove(&xbridges, &xbridge->hmap_node);
418 mac_learning_unref(xbridge->ml);
419 mbridge_unref(xbridge->mbridge);
420 dpif_sflow_unref(xbridge->sflow);
421 dpif_ipfix_unref(xbridge->ipfix);
422 stp_unref(xbridge->stp);
423 hmap_destroy(&xbridge->xports);
429 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
430 const char *name, enum port_vlan_mode vlan_mode, int vlan,
431 unsigned long *trunks, bool use_priority_tags,
432 const struct bond *bond, const struct lacp *lacp,
435 struct xbundle *xbundle = xbundle_lookup(ofbundle);
438 xbundle = xzalloc(sizeof *xbundle);
439 xbundle->ofbundle = ofbundle;
440 xbundle->xbridge = xbridge_lookup(ofproto);
442 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
443 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
444 list_init(&xbundle->xports);
447 ovs_assert(xbundle->xbridge);
450 xbundle->name = xstrdup(name);
452 xbundle->vlan_mode = vlan_mode;
453 xbundle->vlan = vlan;
454 xbundle->trunks = trunks;
455 xbundle->use_priority_tags = use_priority_tags;
456 xbundle->floodable = floodable;
458 if (xbundle->bond != bond) {
459 bond_unref(xbundle->bond);
460 xbundle->bond = bond_ref(bond);
463 if (xbundle->lacp != lacp) {
464 lacp_unref(xbundle->lacp);
465 xbundle->lacp = lacp_ref(lacp);
470 xlate_bundle_remove(struct ofbundle *ofbundle)
472 struct xbundle *xbundle = xbundle_lookup(ofbundle);
473 struct xport *xport, *next;
479 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
480 list_remove(&xport->bundle_node);
481 xport->xbundle = NULL;
484 hmap_remove(&xbundles, &xbundle->hmap_node);
485 list_remove(&xbundle->list_node);
486 bond_unref(xbundle->bond);
487 lacp_unref(xbundle->lacp);
493 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
494 struct ofport_dpif *ofport, ofp_port_t ofp_port,
495 odp_port_t odp_port, const struct netdev *netdev,
496 const struct cfm *cfm, const struct bfd *bfd,
497 struct ofport_dpif *peer, int stp_port_no,
498 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
499 enum ofputil_port_config config,
500 enum ofputil_port_state state, bool is_tunnel,
503 struct xport *xport = xport_lookup(ofport);
507 xport = xzalloc(sizeof *xport);
508 xport->ofport = ofport;
509 xport->xbridge = xbridge_lookup(ofproto);
510 xport->ofp_port = ofp_port;
512 hmap_init(&xport->skb_priorities);
513 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
514 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
515 hash_ofp_port(xport->ofp_port));
518 ovs_assert(xport->ofp_port == ofp_port);
520 xport->config = config;
521 xport->state = state;
522 xport->stp_port_no = stp_port_no;
523 xport->is_tunnel = is_tunnel;
524 xport->may_enable = may_enable;
525 xport->odp_port = odp_port;
527 if (xport->netdev != netdev) {
528 netdev_close(xport->netdev);
529 xport->netdev = netdev_ref(netdev);
532 if (xport->cfm != cfm) {
533 cfm_unref(xport->cfm);
534 xport->cfm = cfm_ref(cfm);
537 if (xport->bfd != bfd) {
538 bfd_unref(xport->bfd);
539 xport->bfd = bfd_ref(bfd);
543 xport->peer->peer = NULL;
545 xport->peer = xport_lookup(peer);
547 xport->peer->peer = xport;
550 if (xport->xbundle) {
551 list_remove(&xport->bundle_node);
553 xport->xbundle = xbundle_lookup(ofbundle);
554 if (xport->xbundle) {
555 list_insert(&xport->xbundle->xports, &xport->bundle_node);
558 clear_skb_priorities(xport);
559 for (i = 0; i < n_qdscp; i++) {
560 struct skb_priority_to_dscp *pdscp;
561 uint32_t skb_priority;
563 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
568 pdscp = xmalloc(sizeof *pdscp);
569 pdscp->skb_priority = skb_priority;
570 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
571 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
572 hash_int(pdscp->skb_priority, 0));
577 xlate_ofport_remove(struct ofport_dpif *ofport)
579 struct xport *xport = xport_lookup(ofport);
586 xport->peer->peer = NULL;
590 if (xport->xbundle) {
591 list_remove(&xport->bundle_node);
594 clear_skb_priorities(xport);
595 hmap_destroy(&xport->skb_priorities);
597 hmap_remove(&xports, &xport->hmap_node);
598 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
600 netdev_close(xport->netdev);
601 cfm_unref(xport->cfm);
602 bfd_unref(xport->bfd);
606 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
607 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
608 * Optionally populates 'ofproto' with the ofproto_dpif, 'odp_in_port' with
609 * the datapath in_port, that 'packet' ingressed, and 'ipfix', 'sflow', and
610 * 'netflow' with the appropriate handles for those protocols if they're
611 * enabled. Caller is responsible for unrefing them.
613 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
614 * 'flow''s in_port to OFPP_NONE.
616 * This function does post-processing on data returned from
617 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
618 * of the upcall processing logic. In particular, if the extracted in_port is
619 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
620 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
621 * a VLAN header onto 'packet' (if it is nonnull).
623 * Similarly, this function also includes some logic to help with tunnels. It
624 * may modify 'flow' as necessary to make the tunneling implementation
625 * transparent to the upcall processing logic.
627 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
628 * or some other positive errno if there are other problems. */
630 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
631 const struct nlattr *key, size_t key_len, struct flow *flow,
632 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
633 struct dpif_sflow **sflow, struct netflow **netflow,
634 odp_port_t *odp_in_port)
636 const struct xport *xport;
639 ovs_rwlock_rdlock(&xlate_rwlock);
640 if (odp_flow_key_to_flow(key, key_len, flow) == ODP_FIT_ERROR) {
646 *odp_in_port = flow->in_port.odp_port;
649 xport = xport_lookup(tnl_port_should_receive(flow)
650 ? tnl_port_receive(flow)
651 : odp_port_to_ofport(backer, flow->in_port.odp_port));
653 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
658 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
660 /* Make the packet resemble the flow, so that it gets sent to
661 * an OpenFlow controller properly, so that it looks correct
662 * for sFlow, and so that flow_extract() will get the correct
663 * vlan_tci if it is called on 'packet'. */
664 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
670 *ofproto = xport->xbridge->ofproto;
674 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
678 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
682 *netflow = netflow_ref(xport->xbridge->netflow);
686 ovs_rwlock_unlock(&xlate_rwlock);
690 static struct xbridge *
691 xbridge_lookup(const struct ofproto_dpif *ofproto)
693 struct xbridge *xbridge;
699 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
701 if (xbridge->ofproto == ofproto) {
708 static struct xbundle *
709 xbundle_lookup(const struct ofbundle *ofbundle)
711 struct xbundle *xbundle;
717 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
719 if (xbundle->ofbundle == ofbundle) {
726 static struct xport *
727 xport_lookup(const struct ofport_dpif *ofport)
735 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
737 if (xport->ofport == ofport) {
744 static struct stp_port *
745 xport_get_stp_port(const struct xport *xport)
747 return xport->xbridge->stp && xport->stp_port_no != -1
748 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
753 xport_stp_learn_state(const struct xport *xport)
755 struct stp_port *sp = xport_get_stp_port(xport);
756 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
760 xport_stp_forward_state(const struct xport *xport)
762 struct stp_port *sp = xport_get_stp_port(xport);
763 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
767 xport_stp_listen_state(const struct xport *xport)
769 struct stp_port *sp = xport_get_stp_port(xport);
770 return stp_listen_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
773 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
774 * were used to make the determination.*/
776 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
778 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
779 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
784 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
786 struct stp_port *sp = xport_get_stp_port(xport);
787 struct ofpbuf payload = *packet;
788 struct eth_header *eth = ofpbuf_data(&payload);
790 /* Sink packets on ports that have STP disabled when the bridge has
792 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
796 /* Trim off padding on payload. */
797 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
798 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
801 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
802 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
806 static struct xport *
807 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
811 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
813 if (xport->ofp_port == ofp_port) {
821 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
823 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
824 return xport ? xport->odp_port : ODPP_NONE;
828 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
832 xport = get_ofp_port(ctx->xbridge, ofp_port);
833 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
834 xport->state & OFPUTIL_PS_LINK_DOWN) {
841 static const struct ofputil_bucket *
842 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
846 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
848 struct group_dpif *group;
851 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
856 hit = group_first_live_bucket(ctx, group, depth) != NULL;
858 group_dpif_release(group);
862 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
865 bucket_is_alive(const struct xlate_ctx *ctx,
866 const struct ofputil_bucket *bucket, int depth)
868 if (depth >= MAX_LIVENESS_RECURSION) {
869 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
871 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
872 MAX_LIVENESS_RECURSION);
876 return !ofputil_bucket_has_liveness(bucket) ||
877 (bucket->watch_port != OFPP_ANY &&
878 odp_port_is_alive(ctx, bucket->watch_port)) ||
879 (bucket->watch_group != OFPG_ANY &&
880 group_is_alive(ctx, bucket->watch_group, depth + 1));
883 static const struct ofputil_bucket *
884 group_first_live_bucket(const struct xlate_ctx *ctx,
885 const struct group_dpif *group, int depth)
887 struct ofputil_bucket *bucket;
888 const struct list *buckets;
890 group_dpif_get_buckets(group, &buckets);
891 LIST_FOR_EACH (bucket, list_node, buckets) {
892 if (bucket_is_alive(ctx, bucket, depth)) {
900 static const struct ofputil_bucket *
901 group_best_live_bucket(const struct xlate_ctx *ctx,
902 const struct group_dpif *group,
905 const struct ofputil_bucket *best_bucket = NULL;
906 uint32_t best_score = 0;
909 const struct ofputil_bucket *bucket;
910 const struct list *buckets;
912 group_dpif_get_buckets(group, &buckets);
913 LIST_FOR_EACH (bucket, list_node, buckets) {
914 if (bucket_is_alive(ctx, bucket, 0)) {
915 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
916 if (score >= best_score) {
917 best_bucket = bucket;
928 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
930 return (bundle->vlan_mode != PORT_VLAN_ACCESS
931 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
935 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
937 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
941 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
943 return xbundle != &ofpp_none_bundle
944 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
949 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
951 return xbundle != &ofpp_none_bundle
952 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
957 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
959 return xbundle != &ofpp_none_bundle
960 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
964 static struct xbundle *
965 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
966 bool warn, struct xport **in_xportp)
970 /* Find the port and bundle for the received packet. */
971 xport = get_ofp_port(xbridge, in_port);
975 if (xport && xport->xbundle) {
976 return xport->xbundle;
979 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
980 * which a controller may use as the ingress port for traffic that
982 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
983 return &ofpp_none_bundle;
986 /* Odd. A few possible reasons here:
988 * - We deleted a port but there are still a few packets queued up
991 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
992 * we don't know about.
994 * - The ofproto client didn't configure the port as part of a bundle.
995 * This is particularly likely to happen if a packet was received on the
996 * port after it was created, but before the client had a chance to
997 * configure its bundle.
1000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1002 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1003 "port %"PRIu16, xbridge->name, in_port);
1009 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1011 const struct xbridge *xbridge = ctx->xbridge;
1012 mirror_mask_t mirrors;
1013 struct xbundle *in_xbundle;
1017 mirrors = ctx->xout->mirrors;
1018 ctx->xout->mirrors = 0;
1020 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1021 ctx->xin->packet != NULL, NULL);
1025 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
1027 /* Drop frames on bundles reserved for mirroring. */
1028 if (xbundle_mirror_out(xbridge, in_xbundle)) {
1029 if (ctx->xin->packet != NULL) {
1030 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1031 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1032 "%s, which is reserved exclusively for mirroring",
1033 ctx->xbridge->name, in_xbundle->name);
1035 ofpbuf_clear(&ctx->xout->odp_actions);
1040 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
1041 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1044 vlan = input_vid_to_vlan(in_xbundle, vid);
1050 /* Restore the original packet before adding the mirror actions. */
1051 ctx->xin->flow = *orig_flow;
1054 mirror_mask_t dup_mirrors;
1055 struct ofbundle *out;
1056 unsigned long *vlans;
1061 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1062 &vlans, &dup_mirrors, &out, &out_vlan);
1063 ovs_assert(has_mirror);
1066 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1068 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1071 if (!vlan_mirrored) {
1072 mirrors = zero_rightmost_1bit(mirrors);
1076 mirrors &= ~dup_mirrors;
1077 ctx->xout->mirrors |= dup_mirrors;
1079 struct xbundle *out_xbundle = xbundle_lookup(out);
1081 output_normal(ctx, out_xbundle, vlan);
1083 } else if (vlan != out_vlan
1084 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
1085 struct xbundle *xbundle;
1087 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1088 if (xbundle_includes_vlan(xbundle, out_vlan)
1089 && !xbundle_mirror_out(xbridge, xbundle)) {
1090 output_normal(ctx, xbundle, out_vlan);
1097 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1098 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1099 * the bundle on which the packet was received, returns the VLAN to which the
1102 * Both 'vid' and the return value are in the range 0...4095. */
1104 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1106 switch (in_xbundle->vlan_mode) {
1107 case PORT_VLAN_ACCESS:
1108 return in_xbundle->vlan;
1111 case PORT_VLAN_TRUNK:
1114 case PORT_VLAN_NATIVE_UNTAGGED:
1115 case PORT_VLAN_NATIVE_TAGGED:
1116 return vid ? vid : in_xbundle->vlan;
1123 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1124 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1127 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1128 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1131 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1133 /* Allow any VID on the OFPP_NONE port. */
1134 if (in_xbundle == &ofpp_none_bundle) {
1138 switch (in_xbundle->vlan_mode) {
1139 case PORT_VLAN_ACCESS:
1142 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1143 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1144 "packet received on port %s configured as VLAN "
1145 "%"PRIu16" access port", vid, in_xbundle->name,
1152 case PORT_VLAN_NATIVE_UNTAGGED:
1153 case PORT_VLAN_NATIVE_TAGGED:
1155 /* Port must always carry its native VLAN. */
1159 case PORT_VLAN_TRUNK:
1160 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1162 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1163 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1164 "received on port %s not configured for trunking "
1165 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1177 /* Given 'vlan', the VLAN that a packet belongs to, and
1178 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1179 * that should be included in the 802.1Q header. (If the return value is 0,
1180 * then the 802.1Q header should only be included in the packet if there is a
1183 * Both 'vlan' and the return value are in the range 0...4095. */
1185 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1187 switch (out_xbundle->vlan_mode) {
1188 case PORT_VLAN_ACCESS:
1191 case PORT_VLAN_TRUNK:
1192 case PORT_VLAN_NATIVE_TAGGED:
1195 case PORT_VLAN_NATIVE_UNTAGGED:
1196 return vlan == out_xbundle->vlan ? 0 : vlan;
1204 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1207 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1209 ovs_be16 tci, old_tci;
1210 struct xport *xport;
1212 vid = output_vlan_to_vid(out_xbundle, vlan);
1213 if (list_is_empty(&out_xbundle->xports)) {
1214 /* Partially configured bundle with no slaves. Drop the packet. */
1216 } else if (!out_xbundle->bond) {
1217 ctx->use_recirc = false;
1218 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1221 struct ofport_dpif *ofport;
1222 struct xlate_recirc *xr = &ctx->recirc;
1223 struct flow_wildcards *wc = &ctx->xout->wc;
1225 if (ctx->xbridge->enable_recirc) {
1226 ctx->use_recirc = bond_may_recirc(
1227 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
1229 if (ctx->use_recirc) {
1230 /* Only TCP mode uses recirculation. */
1231 xr->hash_alg = OVS_HASH_ALG_L4;
1232 bond_update_post_recirc_rules(out_xbundle->bond, false);
1234 /* Recirculation does not require unmasking hash fields. */
1239 ofport = bond_choose_output_slave(out_xbundle->bond,
1240 &ctx->xin->flow, wc, vid);
1241 xport = xport_lookup(ofport);
1244 /* No slaves enabled, so drop packet. */
1248 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1249 * accounting for this bond. */
1250 if (!ctx->use_recirc) {
1251 if (ctx->xin->resubmit_stats) {
1252 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1253 ctx->xin->resubmit_stats->n_bytes);
1255 if (ctx->xin->xcache) {
1256 struct xc_entry *entry;
1259 flow = &ctx->xin->flow;
1260 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1261 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1262 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1263 entry->u.bond.vid = vid;
1268 old_tci = *flow_tci;
1270 if (tci || out_xbundle->use_priority_tags) {
1271 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1273 tci |= htons(VLAN_CFI);
1278 compose_output_action(ctx, xport->ofp_port);
1279 *flow_tci = old_tci;
1282 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1283 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1284 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1286 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1288 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1292 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1293 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1297 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1298 if (flow->nw_proto == ARP_OP_REPLY) {
1300 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1301 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1302 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1304 return flow->nw_src == flow->nw_dst;
1310 /* Checks whether a MAC learning update is necessary for MAC learning table
1311 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1314 * Most packets processed through the MAC learning table do not actually
1315 * change it in any way. This function requires only a read lock on the MAC
1316 * learning table, so it is much cheaper in this common case.
1318 * Keep the code here synchronized with that in update_learning_table__()
1321 is_mac_learning_update_needed(const struct mac_learning *ml,
1322 const struct flow *flow,
1323 struct flow_wildcards *wc,
1324 int vlan, struct xbundle *in_xbundle)
1325 OVS_REQ_RDLOCK(ml->rwlock)
1327 struct mac_entry *mac;
1329 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1333 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1334 if (!mac || mac_entry_age(ml, mac)) {
1338 if (is_gratuitous_arp(flow, wc)) {
1339 /* We don't want to learn from gratuitous ARP packets that are
1340 * reflected back over bond slaves so we lock the learning table. */
1341 if (!in_xbundle->bond) {
1343 } else if (mac_entry_is_grat_arp_locked(mac)) {
1348 return mac->port.p != in_xbundle->ofbundle;
1352 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1353 * received on 'in_xbundle' in 'vlan'.
1355 * This code repeats all the checks in is_mac_learning_update_needed() because
1356 * the lock was released between there and here and thus the MAC learning state
1357 * could have changed.
1359 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1362 update_learning_table__(const struct xbridge *xbridge,
1363 const struct flow *flow, struct flow_wildcards *wc,
1364 int vlan, struct xbundle *in_xbundle)
1365 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1367 struct mac_entry *mac;
1369 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1373 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1374 if (is_gratuitous_arp(flow, wc)) {
1375 /* We don't want to learn from gratuitous ARP packets that are
1376 * reflected back over bond slaves so we lock the learning table. */
1377 if (!in_xbundle->bond) {
1378 mac_entry_set_grat_arp_lock(mac);
1379 } else if (mac_entry_is_grat_arp_locked(mac)) {
1384 if (mac->port.p != in_xbundle->ofbundle) {
1385 /* The log messages here could actually be useful in debugging,
1386 * so keep the rate limit relatively high. */
1387 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1389 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1390 "on port %s in VLAN %d",
1391 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1392 in_xbundle->name, vlan);
1394 mac->port.p = in_xbundle->ofbundle;
1395 mac_learning_changed(xbridge->ml);
1400 update_learning_table(const struct xbridge *xbridge,
1401 const struct flow *flow, struct flow_wildcards *wc,
1402 int vlan, struct xbundle *in_xbundle)
1406 /* Don't learn the OFPP_NONE port. */
1407 if (in_xbundle == &ofpp_none_bundle) {
1411 /* First try the common case: no change to MAC learning table. */
1412 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1413 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1415 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1418 /* Slow path: MAC learning table might need an update. */
1419 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1420 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1421 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1425 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1426 * dropped. Returns true if they may be forwarded, false if they should be
1429 * 'in_port' must be the xport that corresponds to flow->in_port.
1430 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1432 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1433 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1434 * checked by input_vid_is_valid().
1436 * May also add tags to '*tags', although the current implementation only does
1437 * so in one special case.
1440 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1443 struct xbundle *in_xbundle = in_port->xbundle;
1444 const struct xbridge *xbridge = ctx->xbridge;
1445 struct flow *flow = &ctx->xin->flow;
1447 /* Drop frames for reserved multicast addresses
1448 * only if forward_bpdu option is absent. */
1449 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1450 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1454 if (in_xbundle->bond) {
1455 struct mac_entry *mac;
1457 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1463 xlate_report(ctx, "bonding refused admissibility, dropping");
1466 case BV_DROP_IF_MOVED:
1467 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1468 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1469 if (mac && mac->port.p != in_xbundle->ofbundle &&
1470 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1471 || mac_entry_is_grat_arp_locked(mac))) {
1472 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1473 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1477 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1486 xlate_normal(struct xlate_ctx *ctx)
1488 struct flow_wildcards *wc = &ctx->xout->wc;
1489 struct flow *flow = &ctx->xin->flow;
1490 struct xbundle *in_xbundle;
1491 struct xport *in_port;
1492 struct mac_entry *mac;
1497 ctx->xout->has_normal = true;
1499 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1500 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1501 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1503 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1504 ctx->xin->packet != NULL, &in_port);
1506 xlate_report(ctx, "no input bundle, dropping");
1510 /* Drop malformed frames. */
1511 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1512 !(flow->vlan_tci & htons(VLAN_CFI))) {
1513 if (ctx->xin->packet != NULL) {
1514 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1515 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1516 "VLAN tag received on port %s",
1517 ctx->xbridge->name, in_xbundle->name);
1519 xlate_report(ctx, "partial VLAN tag, dropping");
1523 /* Drop frames on bundles reserved for mirroring. */
1524 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1525 if (ctx->xin->packet != NULL) {
1526 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1527 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1528 "%s, which is reserved exclusively for mirroring",
1529 ctx->xbridge->name, in_xbundle->name);
1531 xlate_report(ctx, "input port is mirror output port, dropping");
1536 vid = vlan_tci_to_vid(flow->vlan_tci);
1537 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1538 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1541 vlan = input_vid_to_vlan(in_xbundle, vid);
1543 /* Check other admissibility requirements. */
1544 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1548 /* Learn source MAC. */
1549 if (ctx->xin->may_learn) {
1550 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1552 if (ctx->xin->xcache) {
1553 struct xc_entry *entry;
1555 /* Save enough info to update mac learning table later. */
1556 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
1557 entry->u.normal.ofproto = ctx->xbridge->ofproto;
1558 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
1559 entry->u.normal.vlan = vlan;
1562 /* Determine output bundle. */
1563 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1564 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1565 mac_port = mac ? mac->port.p : NULL;
1566 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1569 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1570 if (mac_xbundle && mac_xbundle != in_xbundle) {
1571 xlate_report(ctx, "forwarding to learned port");
1572 output_normal(ctx, mac_xbundle, vlan);
1573 } else if (!mac_xbundle) {
1574 xlate_report(ctx, "learned port is unknown, dropping");
1576 xlate_report(ctx, "learned port is input port, dropping");
1579 struct xbundle *xbundle;
1581 xlate_report(ctx, "no learned MAC for destination, flooding");
1582 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1583 if (xbundle != in_xbundle
1584 && xbundle_includes_vlan(xbundle, vlan)
1585 && xbundle->floodable
1586 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1587 output_normal(ctx, xbundle, vlan);
1590 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1594 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1595 * the number of packets out of UINT32_MAX to sample. The given
1596 * cookie is passed back in the callback for each sampled packet.
1599 compose_sample_action(const struct xbridge *xbridge,
1600 struct ofpbuf *odp_actions,
1601 const struct flow *flow,
1602 const uint32_t probability,
1603 const union user_action_cookie *cookie,
1604 const size_t cookie_size)
1606 size_t sample_offset, actions_offset;
1607 odp_port_t odp_port;
1611 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1613 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1615 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1617 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1618 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
1619 flow_hash_5tuple(flow, 0));
1620 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
1623 nl_msg_end_nested(odp_actions, actions_offset);
1624 nl_msg_end_nested(odp_actions, sample_offset);
1625 return cookie_offset;
1629 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1630 odp_port_t odp_port, unsigned int n_outputs,
1631 union user_action_cookie *cookie)
1635 cookie->type = USER_ACTION_COOKIE_SFLOW;
1636 cookie->sflow.vlan_tci = vlan_tci;
1638 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1639 * port information") for the interpretation of cookie->output. */
1640 switch (n_outputs) {
1642 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1643 cookie->sflow.output = 0x40000000 | 256;
1647 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1649 cookie->sflow.output = ifindex;
1654 /* 0x80000000 means "multiple output ports. */
1655 cookie->sflow.output = 0x80000000 | n_outputs;
1660 /* Compose SAMPLE action for sFlow bridge sampling. */
1662 compose_sflow_action(const struct xbridge *xbridge,
1663 struct ofpbuf *odp_actions,
1664 const struct flow *flow,
1665 odp_port_t odp_port)
1667 uint32_t probability;
1668 union user_action_cookie cookie;
1670 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1674 probability = dpif_sflow_get_probability(xbridge->sflow);
1675 compose_sflow_cookie(xbridge, htons(0), odp_port,
1676 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1678 return compose_sample_action(xbridge, odp_actions, flow, probability,
1679 &cookie, sizeof cookie.sflow);
1683 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1684 uint32_t obs_domain_id, uint32_t obs_point_id,
1685 union user_action_cookie *cookie)
1687 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1688 cookie->flow_sample.probability = probability;
1689 cookie->flow_sample.collector_set_id = collector_set_id;
1690 cookie->flow_sample.obs_domain_id = obs_domain_id;
1691 cookie->flow_sample.obs_point_id = obs_point_id;
1695 compose_ipfix_cookie(union user_action_cookie *cookie)
1697 cookie->type = USER_ACTION_COOKIE_IPFIX;
1700 /* Compose SAMPLE action for IPFIX bridge sampling. */
1702 compose_ipfix_action(const struct xbridge *xbridge,
1703 struct ofpbuf *odp_actions,
1704 const struct flow *flow)
1706 uint32_t probability;
1707 union user_action_cookie cookie;
1709 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1713 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1714 compose_ipfix_cookie(&cookie);
1716 compose_sample_action(xbridge, odp_actions, flow, probability,
1717 &cookie, sizeof cookie.ipfix);
1720 /* SAMPLE action for sFlow must be first action in any given list of
1721 * actions. At this point we do not have all information required to
1722 * build it. So try to build sample action as complete as possible. */
1724 add_sflow_action(struct xlate_ctx *ctx)
1726 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1727 &ctx->xout->odp_actions,
1728 &ctx->xin->flow, ODPP_NONE);
1729 ctx->sflow_odp_port = 0;
1730 ctx->sflow_n_outputs = 0;
1733 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1734 * of actions, eventually after the SAMPLE action for sFlow. */
1736 add_ipfix_action(struct xlate_ctx *ctx)
1738 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1742 /* Fix SAMPLE action according to data collected while composing ODP actions.
1743 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1744 * USERSPACE action's user-cookie which is required for sflow. */
1746 fix_sflow_action(struct xlate_ctx *ctx)
1748 const struct flow *base = &ctx->base_flow;
1749 union user_action_cookie *cookie;
1751 if (!ctx->user_cookie_offset) {
1755 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1756 sizeof cookie->sflow);
1757 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1759 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1760 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1763 static enum slow_path_reason
1764 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1765 const struct xport *xport, const struct ofpbuf *packet)
1767 struct flow_wildcards *wc = &ctx->xout->wc;
1768 const struct xbridge *xbridge = ctx->xbridge;
1772 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1774 cfm_process_heartbeat(xport->cfm, packet);
1777 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1779 bfd_process_packet(xport->bfd, flow, packet);
1780 /* If POLL received, immediately sends FINAL back. */
1781 if (bfd_should_send_packet(xport->bfd)) {
1782 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1786 } else if (xport->xbundle && xport->xbundle->lacp
1787 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1789 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1792 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1794 stp_process_packet(xport, packet);
1803 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1806 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1807 struct flow_wildcards *wc = &ctx->xout->wc;
1808 struct flow *flow = &ctx->xin->flow;
1809 ovs_be16 flow_vlan_tci;
1810 uint32_t flow_pkt_mark;
1811 uint8_t flow_nw_tos;
1812 odp_port_t out_port, odp_port;
1815 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1816 * before traversing a patch port. */
1817 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
1820 xlate_report(ctx, "Nonexistent output port");
1822 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1823 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1825 } else if (check_stp) {
1826 if (is_stp(&ctx->base_flow)) {
1827 if (!xport_stp_listen_state(xport)) {
1828 xlate_report(ctx, "STP not in listening state, "
1829 "skipping bpdu output");
1832 } else if (!xport_stp_forward_state(xport)) {
1833 xlate_report(ctx, "STP not in forwarding state, "
1839 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1840 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1845 const struct xport *peer = xport->peer;
1846 struct flow old_flow = ctx->xin->flow;
1847 enum slow_path_reason special;
1849 ctx->xbridge = peer->xbridge;
1850 flow->in_port.ofp_port = peer->ofp_port;
1851 flow->metadata = htonll(0);
1852 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1853 memset(flow->regs, 0, sizeof flow->regs);
1855 special = process_special(ctx, &ctx->xin->flow, peer,
1858 ctx->xout->slow |= special;
1859 } else if (may_receive(peer, ctx)) {
1860 if (xport_stp_forward_state(peer)) {
1861 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1863 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1864 * learning action look at the packet, then drop it. */
1865 struct flow old_base_flow = ctx->base_flow;
1866 size_t old_size = ofpbuf_size(&ctx->xout->odp_actions);
1867 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1868 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
1869 ctx->xout->mirrors = old_mirrors;
1870 ctx->base_flow = old_base_flow;
1871 ofpbuf_set_size(&ctx->xout->odp_actions, old_size);
1875 ctx->xin->flow = old_flow;
1876 ctx->xbridge = xport->xbridge;
1878 if (ctx->xin->resubmit_stats) {
1879 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1880 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1882 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1885 if (ctx->xin->xcache) {
1886 struct xc_entry *entry;
1888 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
1889 entry->u.dev.tx = netdev_ref(xport->netdev);
1890 entry->u.dev.rx = netdev_ref(peer->netdev);
1891 entry->u.dev.bfd = bfd_ref(peer->bfd);
1897 flow_vlan_tci = flow->vlan_tci;
1898 flow_pkt_mark = flow->pkt_mark;
1899 flow_nw_tos = flow->nw_tos;
1901 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1902 wc->masks.nw_tos |= IP_DSCP_MASK;
1903 flow->nw_tos &= ~IP_DSCP_MASK;
1904 flow->nw_tos |= dscp;
1907 if (xport->is_tunnel) {
1908 /* Save tunnel metadata so that changes made due to
1909 * the Logical (tunnel) Port are not visible for any further
1910 * matches, while explicit set actions on tunnel metadata are.
1912 struct flow_tnl flow_tnl = flow->tunnel;
1913 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1914 if (odp_port == ODPP_NONE) {
1915 xlate_report(ctx, "Tunneling decided against output");
1916 goto out; /* restore flow_nw_tos */
1918 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1919 xlate_report(ctx, "Not tunneling to our own address");
1920 goto out; /* restore flow_nw_tos */
1922 if (ctx->xin->resubmit_stats) {
1923 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1925 if (ctx->xin->xcache) {
1926 struct xc_entry *entry;
1928 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
1929 entry->u.dev.tx = netdev_ref(xport->netdev);
1931 out_port = odp_port;
1932 commit_odp_tunnel_action(flow, &ctx->base_flow,
1933 &ctx->xout->odp_actions);
1934 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1936 odp_port = xport->odp_port;
1937 out_port = odp_port;
1938 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1939 ofp_port_t vlandev_port;
1941 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1942 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
1943 ofp_port, flow->vlan_tci);
1944 if (vlandev_port != ofp_port) {
1945 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1946 flow->vlan_tci = htons(0);
1951 if (out_port != ODPP_NONE) {
1952 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1953 &ctx->xout->odp_actions,
1956 if (ctx->use_recirc) {
1957 struct ovs_action_hash *act_hash;
1958 struct xlate_recirc *xr = &ctx->recirc;
1961 act_hash = nl_msg_put_unspec_uninit(&ctx->xout->odp_actions,
1962 OVS_ACTION_ATTR_HASH,
1964 act_hash->hash_alg = xr->hash_alg;
1965 act_hash->hash_basis = xr->hash_basis;
1967 /* Recirc action. */
1968 nl_msg_put_u32(&ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
1971 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1975 ctx->sflow_odp_port = odp_port;
1976 ctx->sflow_n_outputs++;
1977 ctx->xout->nf_output_iface = ofp_port;
1982 flow->vlan_tci = flow_vlan_tci;
1983 flow->pkt_mark = flow_pkt_mark;
1984 flow->nw_tos = flow_nw_tos;
1988 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1990 compose_output_action__(ctx, ofp_port, true);
1994 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1996 struct rule_dpif *old_rule = ctx->rule;
1997 const struct rule_actions *actions;
1999 if (ctx->xin->resubmit_stats) {
2000 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
2006 actions = rule_dpif_get_actions(rule);
2007 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
2008 ctx->rule = old_rule;
2013 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
2015 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2017 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
2018 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2019 MAX_RESUBMIT_RECURSION);
2020 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
2021 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
2022 } else if (ofpbuf_size(&ctx->xout->odp_actions) > UINT16_MAX) {
2023 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
2024 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
2025 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2034 xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2035 bool may_packet_in, bool honor_table_miss)
2037 if (xlate_resubmit_resource_check(ctx)) {
2038 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
2039 bool skip_wildcards = ctx->xin->skip_wildcards;
2040 uint8_t old_table_id = ctx->table_id;
2041 struct rule_dpif *rule;
2042 enum rule_dpif_lookup_verdict verdict;
2043 enum ofputil_port_config config = 0;
2045 ctx->table_id = table_id;
2047 /* Look up a flow with 'in_port' as the input port. Then restore the
2048 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2049 * have surprising behavior). */
2050 ctx->xin->flow.in_port.ofp_port = in_port;
2051 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2054 ? &ctx->xout->wc : NULL,
2056 &ctx->table_id, &rule,
2057 ctx->xin->xcache != NULL);
2058 ctx->xin->flow.in_port.ofp_port = old_in_port;
2060 if (ctx->xin->resubmit_hook) {
2061 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2065 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2067 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2068 if (may_packet_in) {
2069 struct xport *xport;
2071 xport = get_ofp_port(ctx->xbridge,
2072 ctx->xin->flow.in_port.ofp_port);
2073 config = xport ? xport->config : 0;
2076 /* Fall through to drop */
2077 case RULE_DPIF_LOOKUP_VERDICT_DROP:
2078 config = OFPUTIL_PC_NO_PACKET_IN;
2080 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2081 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2082 config = OFPUTIL_PC_NO_PACKET_IN;
2089 choose_miss_rule(config, ctx->xbridge->miss_rule,
2090 ctx->xbridge->no_packet_in_rule, &rule,
2091 ctx->xin->xcache != NULL);
2095 /* Fill in the cache entry here instead of xlate_recursively
2096 * to make the reference counting more explicit. We take a
2097 * reference in the lookups above if we are going to cache the
2099 if (ctx->xin->xcache) {
2100 struct xc_entry *entry;
2102 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2103 entry->u.rule = rule;
2105 xlate_recursively(ctx, rule);
2108 ctx->table_id = old_table_id;
2116 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
2118 uint64_t action_list_stub[1024 / 8];
2119 struct ofpbuf action_list, action_set;
2121 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2122 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2124 ofpacts_execute_action_set(&action_list, &action_set);
2126 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2129 ofpbuf_uninit(&action_set);
2130 ofpbuf_uninit(&action_list);
2134 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2136 const struct ofputil_bucket *bucket;
2137 const struct list *buckets;
2138 struct flow old_flow = ctx->xin->flow;
2140 group_dpif_get_buckets(group, &buckets);
2142 LIST_FOR_EACH (bucket, list_node, buckets) {
2143 xlate_group_bucket(ctx, bucket);
2144 /* Roll back flow to previous state.
2145 * This is equivalent to cloning the packet for each bucket.
2147 * As a side effect any subsequently applied actions will
2148 * also effectively be applied to a clone of the packet taken
2149 * just before applying the all or indirect group. */
2150 ctx->xin->flow = old_flow;
2155 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2157 const struct ofputil_bucket *bucket;
2159 bucket = group_first_live_bucket(ctx, group, 0);
2161 xlate_group_bucket(ctx, bucket);
2166 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2168 struct flow_wildcards *wc = &ctx->xout->wc;
2169 const struct ofputil_bucket *bucket;
2172 basis = hash_mac(ctx->xin->flow.dl_dst, 0, 0);
2173 bucket = group_best_live_bucket(ctx, group, basis);
2175 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2176 xlate_group_bucket(ctx, bucket);
2181 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2183 ctx->in_group = true;
2185 switch (group_dpif_get_type(group)) {
2187 case OFPGT11_INDIRECT:
2188 xlate_all_group(ctx, group);
2190 case OFPGT11_SELECT:
2191 xlate_select_group(ctx, group);
2194 xlate_ff_group(ctx, group);
2199 group_dpif_release(group);
2201 ctx->in_group = false;
2205 xlate_group_resource_check(struct xlate_ctx *ctx)
2207 if (!xlate_resubmit_resource_check(ctx)) {
2209 } else if (ctx->in_group) {
2210 /* Prevent nested translation of OpenFlow groups.
2212 * OpenFlow allows this restriction. We enforce this restriction only
2213 * because, with the current architecture, we would otherwise have to
2214 * take a possibly recursive read lock on the ofgroup rwlock, which is
2215 * unsafe given that POSIX allows taking a read lock to block if there
2216 * is a thread blocked on taking the write lock. Other solutions
2217 * without this restriction are also possible, but seem unwarranted
2218 * given the current limited use of groups. */
2219 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2221 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2229 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2231 if (xlate_group_resource_check(ctx)) {
2232 struct group_dpif *group;
2235 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2237 xlate_group_action__(ctx, group);
2247 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2248 const struct ofpact_resubmit *resubmit)
2252 bool may_packet_in = false;
2253 bool honor_table_miss = false;
2255 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2256 /* Still allow missed packets to be sent to the controller
2257 * if resubmitting from an internal table. */
2258 may_packet_in = true;
2259 honor_table_miss = true;
2262 in_port = resubmit->in_port;
2263 if (in_port == OFPP_IN_PORT) {
2264 in_port = ctx->xin->flow.in_port.ofp_port;
2267 table_id = resubmit->table_id;
2268 if (table_id == 255) {
2269 table_id = ctx->table_id;
2272 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2277 flood_packets(struct xlate_ctx *ctx, bool all)
2279 const struct xport *xport;
2281 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2282 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2287 compose_output_action__(ctx, xport->ofp_port, false);
2288 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2289 compose_output_action(ctx, xport->ofp_port);
2293 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2297 execute_controller_action(struct xlate_ctx *ctx, int len,
2298 enum ofp_packet_in_reason reason,
2299 uint16_t controller_id)
2301 struct ofproto_packet_in *pin;
2302 struct ofpbuf *packet;
2303 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
2305 ctx->xout->slow |= SLOW_CONTROLLER;
2306 if (!ctx->xin->packet) {
2310 packet = ofpbuf_clone(ctx->xin->packet);
2312 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2313 &ctx->xout->odp_actions,
2316 odp_execute_actions(NULL, packet, false, &md,
2317 ofpbuf_data(&ctx->xout->odp_actions),
2318 ofpbuf_size(&ctx->xout->odp_actions), NULL);
2320 pin = xmalloc(sizeof *pin);
2321 pin->up.packet_len = ofpbuf_size(packet);
2322 pin->up.packet = ofpbuf_steal_data(packet);
2323 pin->up.reason = reason;
2324 pin->up.table_id = ctx->table_id;
2325 pin->up.cookie = (ctx->rule
2326 ? rule_dpif_get_flow_cookie(ctx->rule)
2329 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2331 pin->controller_id = controller_id;
2332 pin->send_len = len;
2333 /* If a rule is a table-miss rule then this is
2334 * a table-miss handled by a table-miss rule.
2336 * Else, if rule is internal and has a controller action,
2337 * the later being implied by the rule being processed here,
2338 * then this is a table-miss handled without a table-miss rule.
2340 * Otherwise this is not a table-miss. */
2341 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
2343 if (rule_dpif_is_table_miss(ctx->rule)) {
2344 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
2345 } else if (rule_dpif_is_internal(ctx->rule)) {
2346 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
2349 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2350 ofpbuf_delete(packet);
2354 compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
2356 struct flow_wildcards *wc = &ctx->xout->wc;
2357 struct flow *flow = &ctx->xin->flow;
2360 ovs_assert(eth_type_mpls(mpls->ethertype));
2362 n = flow_count_mpls_labels(flow, wc);
2364 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
2365 &ctx->xout->odp_actions,
2367 } else if (n >= FLOW_MAX_MPLS_LABELS) {
2368 if (ctx->xin->packet != NULL) {
2369 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2370 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2371 "MPLS push action can't be performed as it would "
2372 "have more MPLS LSEs than the %d supported.",
2373 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2377 } else if (n >= ctx->xbridge->max_mpls_depth) {
2378 COVERAGE_INC(xlate_actions_mpls_overflow);
2379 ctx->xout->slow |= SLOW_ACTION;
2382 flow_push_mpls(flow, n, mpls->ethertype, wc);
2386 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2388 struct flow_wildcards *wc = &ctx->xout->wc;
2389 struct flow *flow = &ctx->xin->flow;
2390 int n = flow_count_mpls_labels(flow, wc);
2392 if (!flow_pop_mpls(flow, n, eth_type, wc) && n >= FLOW_MAX_MPLS_LABELS) {
2393 if (ctx->xin->packet != NULL) {
2394 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2395 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
2396 "MPLS pop action can't be performed as it has "
2397 "more MPLS LSEs than the %d supported.",
2398 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
2401 ofpbuf_clear(&ctx->xout->odp_actions);
2406 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2408 struct flow *flow = &ctx->xin->flow;
2410 if (!is_ip_any(flow)) {
2414 ctx->xout->wc.masks.nw_ttl = 0xff;
2415 if (flow->nw_ttl > 1) {
2421 for (i = 0; i < ids->n_controllers; i++) {
2422 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2426 /* Stop processing for current table. */
2432 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2434 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2435 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
2436 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
2441 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2443 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2444 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
2445 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
2450 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2452 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2453 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
2454 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
2459 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2461 struct flow *flow = &ctx->xin->flow;
2462 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
2463 struct flow_wildcards *wc = &ctx->xout->wc;
2465 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2466 if (eth_type_mpls(flow->dl_type)) {
2469 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
2472 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2474 /* Stop processing for current table. */
2483 xlate_output_action(struct xlate_ctx *ctx,
2484 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2486 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2488 ctx->xout->nf_output_iface = NF_OUT_DROP;
2492 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2495 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2496 0, may_packet_in, true);
2502 flood_packets(ctx, false);
2505 flood_packets(ctx, true);
2507 case OFPP_CONTROLLER:
2508 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2514 if (port != ctx->xin->flow.in_port.ofp_port) {
2515 compose_output_action(ctx, port);
2517 xlate_report(ctx, "skipping output to input port");
2522 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2523 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2524 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2525 ctx->xout->nf_output_iface = prev_nf_output_iface;
2526 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2527 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2528 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2533 xlate_output_reg_action(struct xlate_ctx *ctx,
2534 const struct ofpact_output_reg *or)
2536 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2537 if (port <= UINT16_MAX) {
2538 union mf_subvalue value;
2540 memset(&value, 0xff, sizeof value);
2541 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2542 xlate_output_action(ctx, u16_to_ofp(port),
2543 or->max_len, false);
2548 xlate_enqueue_action(struct xlate_ctx *ctx,
2549 const struct ofpact_enqueue *enqueue)
2551 ofp_port_t ofp_port = enqueue->port;
2552 uint32_t queue_id = enqueue->queue;
2553 uint32_t flow_priority, priority;
2556 /* Translate queue to priority. */
2557 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2559 /* Fall back to ordinary output action. */
2560 xlate_output_action(ctx, enqueue->port, 0, false);
2564 /* Check output port. */
2565 if (ofp_port == OFPP_IN_PORT) {
2566 ofp_port = ctx->xin->flow.in_port.ofp_port;
2567 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2571 /* Add datapath actions. */
2572 flow_priority = ctx->xin->flow.skb_priority;
2573 ctx->xin->flow.skb_priority = priority;
2574 compose_output_action(ctx, ofp_port);
2575 ctx->xin->flow.skb_priority = flow_priority;
2577 /* Update NetFlow output port. */
2578 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2579 ctx->xout->nf_output_iface = ofp_port;
2580 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2581 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2586 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2588 uint32_t skb_priority;
2590 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2591 ctx->xin->flow.skb_priority = skb_priority;
2593 /* Couldn't translate queue to a priority. Nothing to do. A warning
2594 * has already been logged. */
2599 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2601 const struct xbridge *xbridge = xbridge_;
2612 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2615 port = get_ofp_port(xbridge, ofp_port);
2616 return port ? port->may_enable : false;
2621 xlate_bundle_action(struct xlate_ctx *ctx,
2622 const struct ofpact_bundle *bundle)
2626 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2628 CONST_CAST(struct xbridge *, ctx->xbridge));
2629 if (bundle->dst.field) {
2630 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2633 xlate_output_action(ctx, port, 0, false);
2638 xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
2639 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
2641 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
2642 if (ctx->xin->may_learn) {
2643 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
2648 xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
2650 ctx->xout->has_learn = true;
2651 learn_mask(learn, &ctx->xout->wc);
2653 if (ctx->xin->xcache) {
2654 struct xc_entry *entry;
2656 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
2657 entry->u.learn.ofproto = ctx->xbridge->ofproto;
2658 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
2659 entry->u.learn.ofpacts = ofpbuf_new(64);
2660 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
2661 entry->u.learn.ofpacts);
2662 } else if (ctx->xin->may_learn) {
2663 uint64_t ofpacts_stub[1024 / 8];
2664 struct ofputil_flow_mod fm;
2665 struct ofpbuf ofpacts;
2667 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2668 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
2669 ofpbuf_uninit(&ofpacts);
2674 xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
2675 uint16_t idle_timeout, uint16_t hard_timeout)
2677 if (tcp_flags & (TCP_FIN | TCP_RST)) {
2678 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
2683 xlate_fin_timeout(struct xlate_ctx *ctx,
2684 const struct ofpact_fin_timeout *oft)
2687 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
2688 oft->fin_idle_timeout, oft->fin_hard_timeout);
2689 if (ctx->xin->xcache) {
2690 struct xc_entry *entry;
2692 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
2693 /* XC_RULE already holds a reference on the rule, none is taken
2695 entry->u.fin.rule = ctx->rule;
2696 entry->u.fin.idle = oft->fin_idle_timeout;
2697 entry->u.fin.hard = oft->fin_hard_timeout;
2703 xlate_sample_action(struct xlate_ctx *ctx,
2704 const struct ofpact_sample *os)
2706 union user_action_cookie cookie;
2707 /* Scale the probability from 16-bit to 32-bit while representing
2708 * the same percentage. */
2709 uint32_t probability = (os->probability << 16) | os->probability;
2711 if (!ctx->xbridge->variable_length_userdata) {
2712 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2714 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
2715 "lacks support (needs Linux 3.10+ or kernel module from "
2720 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2721 &ctx->xout->odp_actions,
2724 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2725 os->obs_domain_id, os->obs_point_id, &cookie);
2726 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2727 probability, &cookie, sizeof cookie.flow_sample);
2731 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2733 if (xport->config & (is_stp(&ctx->xin->flow)
2734 ? OFPUTIL_PC_NO_RECV_STP
2735 : OFPUTIL_PC_NO_RECV)) {
2739 /* Only drop packets here if both forwarding and learning are
2740 * disabled. If just learning is enabled, we need to have
2741 * OFPP_NORMAL and the learning action have a look at the packet
2742 * before we can drop it. */
2743 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2751 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2753 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2754 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2755 ofpact_pad(&ctx->action_set);
2759 xlate_action_set(struct xlate_ctx *ctx)
2761 uint64_t action_list_stub[1024 / 64];
2762 struct ofpbuf action_list;
2764 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2765 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2766 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
2767 ofpbuf_uninit(&action_list);
2771 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2772 struct xlate_ctx *ctx)
2774 struct flow_wildcards *wc = &ctx->xout->wc;
2775 struct flow *flow = &ctx->xin->flow;
2776 const struct ofpact *a;
2778 /* dl_type already in the mask, not set below. */
2780 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2781 struct ofpact_controller *controller;
2782 const struct ofpact_metadata *metadata;
2783 const struct ofpact_set_field *set_field;
2784 const struct mf_field *mf;
2792 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2793 ofpact_get_OUTPUT(a)->max_len, true);
2797 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2802 case OFPACT_CONTROLLER:
2803 controller = ofpact_get_CONTROLLER(a);
2804 execute_controller_action(ctx, controller->max_len,
2806 controller->controller_id);
2809 case OFPACT_ENQUEUE:
2810 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2813 case OFPACT_SET_VLAN_VID:
2814 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2815 if (flow->vlan_tci & htons(VLAN_CFI) ||
2816 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2817 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2818 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2823 case OFPACT_SET_VLAN_PCP:
2824 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2825 if (flow->vlan_tci & htons(VLAN_CFI) ||
2826 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2827 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2828 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2829 << VLAN_PCP_SHIFT) | VLAN_CFI);
2833 case OFPACT_STRIP_VLAN:
2834 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2835 flow->vlan_tci = htons(0);
2838 case OFPACT_PUSH_VLAN:
2839 /* XXX 802.1AD(QinQ) */
2840 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2841 flow->vlan_tci = htons(VLAN_CFI);
2844 case OFPACT_SET_ETH_SRC:
2845 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2846 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2849 case OFPACT_SET_ETH_DST:
2850 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2851 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2854 case OFPACT_SET_IPV4_SRC:
2855 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2856 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2857 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2861 case OFPACT_SET_IPV4_DST:
2862 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2863 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2864 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2868 case OFPACT_SET_IP_DSCP:
2869 if (is_ip_any(flow)) {
2870 wc->masks.nw_tos |= IP_DSCP_MASK;
2871 flow->nw_tos &= ~IP_DSCP_MASK;
2872 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2876 case OFPACT_SET_IP_ECN:
2877 if (is_ip_any(flow)) {
2878 wc->masks.nw_tos |= IP_ECN_MASK;
2879 flow->nw_tos &= ~IP_ECN_MASK;
2880 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2884 case OFPACT_SET_IP_TTL:
2885 if (is_ip_any(flow)) {
2886 wc->masks.nw_ttl = 0xff;
2887 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2891 case OFPACT_SET_L4_SRC_PORT:
2892 if (is_ip_any(flow)) {
2893 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2894 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2895 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2899 case OFPACT_SET_L4_DST_PORT:
2900 if (is_ip_any(flow)) {
2901 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2902 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2903 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2907 case OFPACT_RESUBMIT:
2908 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2911 case OFPACT_SET_TUNNEL:
2912 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2915 case OFPACT_SET_QUEUE:
2916 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2919 case OFPACT_POP_QUEUE:
2920 flow->skb_priority = ctx->orig_skb_priority;
2923 case OFPACT_REG_MOVE:
2924 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2927 case OFPACT_REG_LOAD:
2928 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2931 case OFPACT_SET_FIELD:
2932 set_field = ofpact_get_SET_FIELD(a);
2933 mf = set_field->field;
2935 /* Set field action only ever overwrites packet's outermost
2936 * applicable header fields. Do nothing if no header exists. */
2937 if (mf->id == MFF_VLAN_VID) {
2938 wc->masks.vlan_tci |= htons(VLAN_CFI);
2939 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2942 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
2943 /* 'dl_type' is already unwildcarded. */
2944 && !eth_type_mpls(flow->dl_type)) {
2948 mf_mask_field_and_prereqs(mf, &wc->masks);
2949 mf_set_flow_value(mf, &set_field->value, flow);
2952 case OFPACT_STACK_PUSH:
2953 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2957 case OFPACT_STACK_POP:
2958 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2962 case OFPACT_PUSH_MPLS:
2963 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
2966 case OFPACT_POP_MPLS:
2967 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
2970 case OFPACT_SET_MPLS_LABEL:
2971 compose_set_mpls_label_action(
2972 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
2975 case OFPACT_SET_MPLS_TC:
2976 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
2979 case OFPACT_SET_MPLS_TTL:
2980 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
2983 case OFPACT_DEC_MPLS_TTL:
2984 if (compose_dec_mpls_ttl_action(ctx)) {
2989 case OFPACT_DEC_TTL:
2990 wc->masks.nw_ttl = 0xff;
2991 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2997 /* Nothing to do. */
3000 case OFPACT_MULTIPATH:
3001 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
3005 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3008 case OFPACT_OUTPUT_REG:
3009 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
3013 xlate_learn_action(ctx, ofpact_get_LEARN(a));
3020 case OFPACT_FIN_TIMEOUT:
3021 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3022 ctx->xout->has_fin_timeout = true;
3023 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3026 case OFPACT_CLEAR_ACTIONS:
3027 ofpbuf_clear(&ctx->action_set);
3030 case OFPACT_WRITE_ACTIONS:
3031 xlate_write_actions(ctx, a);
3034 case OFPACT_WRITE_METADATA:
3035 metadata = ofpact_get_WRITE_METADATA(a);
3036 flow->metadata &= ~metadata->mask;
3037 flow->metadata |= metadata->metadata & metadata->mask;
3041 /* Not implemented yet. */
3044 case OFPACT_GOTO_TABLE: {
3045 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
3047 ovs_assert(ctx->table_id < ogt->table_id);
3048 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
3049 ogt->table_id, true, true);
3054 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3061 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
3062 const struct flow *flow, struct rule_dpif *rule,
3063 uint16_t tcp_flags, const struct ofpbuf *packet)
3065 xin->ofproto = ofproto;
3067 xin->packet = packet;
3068 xin->may_learn = packet != NULL;
3071 xin->ofpacts = NULL;
3072 xin->ofpacts_len = 0;
3073 xin->tcp_flags = tcp_flags;
3074 xin->resubmit_hook = NULL;
3075 xin->report_hook = NULL;
3076 xin->resubmit_stats = NULL;
3077 xin->skip_wildcards = false;
3081 xlate_out_uninit(struct xlate_out *xout)
3084 ofpbuf_uninit(&xout->odp_actions);
3088 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3089 * into datapath actions, using 'ctx', and discards the datapath actions. */
3091 xlate_actions_for_side_effects(struct xlate_in *xin)
3093 struct xlate_out xout;
3095 xlate_actions(xin, &xout);
3096 xlate_out_uninit(&xout);
3100 xlate_report(struct xlate_ctx *ctx, const char *s)
3102 if (ctx->xin->report_hook) {
3103 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
3108 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3111 dst->slow = src->slow;
3112 dst->has_learn = src->has_learn;
3113 dst->has_normal = src->has_normal;
3114 dst->has_fin_timeout = src->has_fin_timeout;
3115 dst->nf_output_iface = src->nf_output_iface;
3116 dst->mirrors = src->mirrors;
3118 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
3119 sizeof dst->odp_actions_stub);
3120 ofpbuf_put(&dst->odp_actions, ofpbuf_data(&src->odp_actions),
3121 ofpbuf_size(&src->odp_actions));
3124 static struct skb_priority_to_dscp *
3125 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
3127 struct skb_priority_to_dscp *pdscp;
3130 hash = hash_int(skb_priority, 0);
3131 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
3132 if (pdscp->skb_priority == skb_priority) {
3140 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
3143 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
3144 *dscp = pdscp ? pdscp->dscp : 0;
3145 return pdscp != NULL;
3149 clear_skb_priorities(struct xport *xport)
3151 struct skb_priority_to_dscp *pdscp, *next;
3153 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
3154 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
3160 actions_output_to_local_port(const struct xlate_ctx *ctx)
3162 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
3163 const struct nlattr *a;
3166 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(&ctx->xout->odp_actions),
3167 ofpbuf_size(&ctx->xout->odp_actions)) {
3168 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
3169 && nl_attr_get_odp_port(a) == local_odp_port) {
3176 /* Thread safe call to xlate_actions__(). */
3178 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
3179 OVS_EXCLUDED(xlate_rwlock)
3181 ovs_rwlock_rdlock(&xlate_rwlock);
3182 xlate_actions__(xin, xout);
3183 ovs_rwlock_unlock(&xlate_rwlock);
3186 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
3187 * into datapath actions in 'odp_actions', using 'ctx'.
3189 * The caller must take responsibility for eventually freeing 'xout', with
3190 * xlate_out_uninit(). */
3192 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3193 OVS_REQ_RDLOCK(xlate_rwlock)
3195 struct flow_wildcards *wc = &xout->wc;
3196 struct flow *flow = &xin->flow;
3197 struct rule_dpif *rule = NULL;
3199 const struct rule_actions *actions = NULL;
3200 enum slow_path_reason special;
3201 const struct ofpact *ofpacts;
3202 struct xport *in_port;
3203 struct flow orig_flow;
3204 struct xlate_ctx ctx;
3209 COVERAGE_INC(xlate_actions);
3211 /* Flow initialization rules:
3212 * - 'base_flow' must match the kernel's view of the packet at the
3213 * time that action processing starts. 'flow' represents any
3214 * transformations we wish to make through actions.
3215 * - By default 'base_flow' and 'flow' are the same since the input
3216 * packet matches the output before any actions are applied.
3217 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3218 * of the received packet as seen by the kernel. If we later output
3219 * to another device without any modifications this will cause us to
3220 * insert a new tag since the original one was stripped off by the
3222 * - Tunnel metadata as received is retained in 'flow'. This allows
3223 * tunnel metadata matching also in later tables.
3224 * Since a kernel action for setting the tunnel metadata will only be
3225 * generated with actual tunnel output, changing the tunnel metadata
3226 * values in 'flow' (such as tun_id) will only have effect with a later
3227 * tunnel output action.
3228 * - Tunnel 'base_flow' is completely cleared since that is what the
3229 * kernel does. If we wish to maintain the original values an action
3230 * needs to be generated. */
3235 ctx.xout->has_learn = false;
3236 ctx.xout->has_normal = false;
3237 ctx.xout->has_fin_timeout = false;
3238 ctx.xout->nf_output_iface = NF_OUT_DROP;
3239 ctx.xout->mirrors = 0;
3240 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3241 sizeof ctx.xout->odp_actions_stub);
3242 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3244 ctx.xbridge = xbridge_lookup(xin->ofproto);
3249 ctx.rule = xin->rule;
3251 ctx.base_flow = *flow;
3252 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3253 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3255 flow_wildcards_init_catchall(wc);
3256 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3257 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3258 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3259 if (is_ip_any(flow)) {
3260 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3262 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
3264 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3265 if (ctx.xbridge->netflow) {
3266 netflow_mask_wc(flow, wc);
3271 ctx.in_group = false;
3272 ctx.orig_skb_priority = flow->skb_priority;
3275 ctx.use_recirc = false;
3277 if (!xin->ofpacts && !ctx.rule) {
3278 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3279 !xin->skip_wildcards ? wc : NULL,
3280 &rule, ctx.xin->xcache != NULL);
3281 if (ctx.xin->resubmit_stats) {
3282 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3284 if (ctx.xin->xcache) {
3285 struct xc_entry *entry;
3287 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
3288 entry->u.rule = rule;
3292 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3295 ofpacts = xin->ofpacts;
3296 ofpacts_len = xin->ofpacts_len;
3297 } else if (ctx.rule) {
3298 actions = rule_dpif_get_actions(ctx.rule);
3299 ofpacts = actions->ofpacts;
3300 ofpacts_len = actions->ofpacts_len;
3305 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3306 ofpbuf_use_stub(&ctx.action_set,
3307 ctx.action_set_stub, sizeof ctx.action_set_stub);
3309 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3310 /* Do this conditionally because the copy is expensive enough that it
3311 * shows up in profiles. */
3315 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3316 switch (ctx.xbridge->frag) {
3317 case OFPC_FRAG_NORMAL:
3318 /* We must pretend that transport ports are unavailable. */
3319 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3320 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3323 case OFPC_FRAG_DROP:
3326 case OFPC_FRAG_REASM:
3329 case OFPC_FRAG_NX_MATCH:
3330 /* Nothing to do. */
3333 case OFPC_INVALID_TTL_TO_CONTROLLER:
3338 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3339 if (in_port && in_port->is_tunnel) {
3340 if (ctx.xin->resubmit_stats) {
3341 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3343 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3346 if (ctx.xin->xcache) {
3347 struct xc_entry *entry;
3349 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
3350 entry->u.dev.rx = netdev_ref(in_port->netdev);
3351 entry->u.dev.bfd = bfd_ref(in_port->bfd);
3355 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3357 ctx.xout->slow |= special;
3359 size_t sample_actions_len;
3361 if (flow->in_port.ofp_port
3362 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3363 flow->in_port.ofp_port,
3365 ctx.base_flow.vlan_tci = 0;
3368 add_sflow_action(&ctx);
3369 add_ipfix_action(&ctx);
3370 sample_actions_len = ofpbuf_size(&ctx.xout->odp_actions);
3372 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3373 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3375 /* We've let OFPP_NORMAL and the learning action look at the
3376 * packet, so drop it now if forwarding is disabled. */
3377 if (in_port && !xport_stp_forward_state(in_port)) {
3378 ofpbuf_set_size(&ctx.xout->odp_actions, sample_actions_len);
3382 if (ofpbuf_size(&ctx.action_set)) {
3383 xlate_action_set(&ctx);
3386 if (ctx.xbridge->has_in_band
3387 && in_band_must_output_to_local_port(flow)
3388 && !actions_output_to_local_port(&ctx)) {
3389 compose_output_action(&ctx, OFPP_LOCAL);
3392 fix_sflow_action(&ctx);
3394 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3395 add_mirror_actions(&ctx, &orig_flow);
3399 if (nl_attr_oversized(ofpbuf_size(&ctx.xout->odp_actions))) {
3400 /* These datapath actions are too big for a Netlink attribute, so we
3401 * can't hand them to the kernel directly. dpif_execute() can execute
3402 * them one by one with help, so just mark the result as SLOW_ACTION to
3403 * prevent the flow from being installed. */
3404 COVERAGE_INC(xlate_actions_oversize);
3405 ctx.xout->slow |= SLOW_ACTION;
3408 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3409 if (ctx.xin->resubmit_stats) {
3410 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3411 ctx.xin->resubmit_stats->n_packets,
3412 ctx.xin->resubmit_stats->n_bytes);
3414 if (ctx.xin->xcache) {
3415 struct xc_entry *entry;
3417 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
3418 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
3419 entry->u.mirror.mirrors = xout->mirrors;
3423 if (ctx.xbridge->netflow) {
3424 /* Only update netflow if we don't have controller flow. We don't
3425 * report NetFlow expiration messages for such facets because they
3426 * are just part of the control logic for the network, not real
3428 if (ofpacts_len == 0
3429 || ofpacts->type != OFPACT_CONTROLLER
3430 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3431 if (ctx.xin->resubmit_stats) {
3432 netflow_flow_update(ctx.xbridge->netflow, flow,
3433 xout->nf_output_iface,
3434 ctx.xin->resubmit_stats);
3436 if (ctx.xin->xcache) {
3437 struct xc_entry *entry;
3439 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
3440 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
3441 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
3442 entry->u.nf.iface = xout->nf_output_iface;
3447 ofpbuf_uninit(&ctx.stack);
3448 ofpbuf_uninit(&ctx.action_set);
3450 /* Clear the metadata and register wildcard masks, because we won't
3451 * use non-header fields as part of the cache. */
3452 flow_wildcards_clear_non_packet_fields(wc);
3454 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
3455 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
3456 * these fields. The datapath interface, on the other hand, represents
3457 * them with just 8 bits each. This means that if the high 8 bits of the
3458 * masks for these fields somehow become set, then they will get chopped
3459 * off by a round trip through the datapath, and revalidation will spot
3460 * that as an inconsistency and delete the flow. Avoid the problem here by
3461 * making sure that only the low 8 bits of either field can be unwildcarded
3465 wc->masks.tp_src &= htons(UINT8_MAX);
3466 wc->masks.tp_dst &= htons(UINT8_MAX);
3470 /* Sends 'packet' out 'ofport'.
3471 * May modify 'packet'.
3472 * Returns 0 if successful, otherwise a positive errno value. */
3474 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3476 struct xport *xport;
3477 struct ofpact_output output;
3480 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3481 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3482 flow_extract(packet, NULL, &flow);
3483 flow.in_port.ofp_port = OFPP_NONE;
3485 ovs_rwlock_rdlock(&xlate_rwlock);
3486 xport = xport_lookup(ofport);
3488 ovs_rwlock_unlock(&xlate_rwlock);
3491 output.port = xport->ofp_port;
3493 ovs_rwlock_unlock(&xlate_rwlock);
3495 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3496 &output.ofpact, sizeof output,
3500 struct xlate_cache *
3501 xlate_cache_new(void)
3503 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
3505 ofpbuf_init(&xcache->entries, 512);
3509 static struct xc_entry *
3510 xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
3512 struct xc_entry *entry;
3514 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
3521 xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
3523 if (entry->u.dev.tx) {
3524 netdev_vport_inc_tx(entry->u.dev.tx, stats);
3526 if (entry->u.dev.rx) {
3527 netdev_vport_inc_rx(entry->u.dev.rx, stats);
3529 if (entry->u.dev.bfd) {
3530 bfd_account_rx(entry->u.dev.bfd, stats);
3535 xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
3537 struct xbridge *xbridge;
3538 struct xbundle *xbundle;
3539 struct flow_wildcards wc;
3541 xbridge = xbridge_lookup(ofproto);
3546 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
3552 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
3555 /* Push stats and perform side effects of flow translation. */
3557 xlate_push_stats(struct xlate_cache *xcache, bool may_learn,
3558 const struct dpif_flow_stats *stats)
3560 struct xc_entry *entry;
3561 struct ofpbuf entries = xcache->entries;
3563 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3564 switch (entry->type) {
3566 rule_dpif_credit_stats(entry->u.rule, stats);
3569 bond_account(entry->u.bond.bond, entry->u.bond.flow,
3570 entry->u.bond.vid, stats->n_bytes);
3573 xlate_cache_netdev(entry, stats);
3576 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
3577 entry->u.nf.iface, stats);
3580 mirror_update_stats(entry->u.mirror.mbridge,
3581 entry->u.mirror.mirrors,
3582 stats->n_packets, stats->n_bytes);
3586 ofproto_dpif_flow_mod(entry->u.learn.ofproto,
3591 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
3592 entry->u.normal.vlan);
3594 case XC_FIN_TIMEOUT:
3595 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
3596 entry->u.fin.idle, entry->u.fin.hard);
3605 xlate_dev_unref(struct xc_entry *entry)
3607 if (entry->u.dev.tx) {
3608 netdev_close(entry->u.dev.tx);
3610 if (entry->u.dev.rx) {
3611 netdev_close(entry->u.dev.rx);
3613 if (entry->u.dev.bfd) {
3614 bfd_unref(entry->u.dev.bfd);
3619 xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
3621 netflow_flow_clear(netflow, flow);
3622 netflow_unref(netflow);
3627 xlate_cache_clear(struct xlate_cache *xcache)
3629 struct xc_entry *entry;
3630 struct ofpbuf entries;
3636 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
3637 switch (entry->type) {
3639 rule_dpif_unref(entry->u.rule);
3642 free(entry->u.bond.flow);
3643 bond_unref(entry->u.bond.bond);
3646 xlate_dev_unref(entry);
3649 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
3652 mbridge_unref(entry->u.mirror.mbridge);
3655 free(entry->u.learn.fm);
3656 ofpbuf_delete(entry->u.learn.ofpacts);
3659 free(entry->u.normal.flow);
3661 case XC_FIN_TIMEOUT:
3662 /* 'u.fin.rule' is always already held as a XC_RULE, which
3663 * has already released it's reference above. */
3670 ofpbuf_clear(&xcache->entries);
3674 xlate_cache_delete(struct xlate_cache *xcache)
3676 xlate_cache_clear(xcache);
3677 ofpbuf_uninit(&xcache->entries);