2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-provider.h"
26 #include "byte-order.h"
31 #include "dynamic-string.h"
32 #include "fail-open.h"
36 #include "mac-learning.h"
37 #include "multipath.h"
44 #include "ofp-print.h"
45 #include "ofproto-dpif-sflow.h"
46 #include "poll-loop.h"
48 #include "unaligned.h"
50 #include "vlan-bitmap.h"
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif
);
55 COVERAGE_DEFINE(ofproto_dpif_ctlr_action
);
56 COVERAGE_DEFINE(ofproto_dpif_expired
);
57 COVERAGE_DEFINE(ofproto_dpif_no_packet_in
);
58 COVERAGE_DEFINE(ofproto_dpif_xlate
);
59 COVERAGE_DEFINE(facet_changed_rule
);
60 COVERAGE_DEFINE(facet_invalidated
);
61 COVERAGE_DEFINE(facet_revalidate
);
62 COVERAGE_DEFINE(facet_unexpected
);
64 /* Maximum depth of flow table recursion (due to resubmit actions) in a
65 * flow translation. */
66 #define MAX_RESUBMIT_RECURSION 16
68 /* Number of implemented OpenFlow tables. */
69 enum { N_TABLES
= 255 };
70 BUILD_ASSERT_DECL(N_TABLES
>= 1 && N_TABLES
<= 255);
78 long long int used
; /* Time last used; time created if not used. */
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
91 * datapath by, e.g., dpif_flow_get() for any facet.
93 uint64_t packet_count
; /* Number of packets received. */
94 uint64_t byte_count
; /* Number of bytes received. */
96 tag_type tag
; /* Caches rule_calculate_tag() result. */
98 struct list facets
; /* List of "struct facet"s. */
101 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
103 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
106 static struct rule_dpif
*rule_dpif_lookup(struct ofproto_dpif
*,
107 const struct flow
*, uint8_t table
);
109 #define MAX_MIRRORS 32
110 typedef uint32_t mirror_mask_t
;
111 #define MIRROR_MASK_C(X) UINT32_C(X)
112 BUILD_ASSERT_DECL(sizeof(mirror_mask_t
) * CHAR_BIT
>= MAX_MIRRORS
);
114 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
115 size_t idx
; /* In ofproto's "mirrors" array. */
116 void *aux
; /* Key supplied by ofproto's client. */
117 char *name
; /* Identifier for log messages. */
119 /* Selection criteria. */
120 struct hmapx srcs
; /* Contains "struct ofbundle *"s. */
121 struct hmapx dsts
; /* Contains "struct ofbundle *"s. */
122 unsigned long *vlans
; /* Bitmap of chosen VLANs, NULL selects all. */
124 /* Output (mutually exclusive). */
125 struct ofbundle
*out
; /* Output port or NULL. */
126 int out_vlan
; /* Output VLAN or -1. */
129 static void mirror_destroy(struct ofmirror
*);
131 /* A group of one or more OpenFlow ports. */
132 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
134 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
135 struct hmap_node hmap_node
; /* In struct ofproto's "bundles" hmap. */
136 void *aux
; /* Key supplied by ofproto's client. */
137 char *name
; /* Identifier for log messages. */
140 struct list ports
; /* Contains "struct ofport"s. */
141 enum port_vlan_mode vlan_mode
; /* VLAN mode */
142 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
143 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
144 * NULL if all VLANs are trunked. */
145 struct lacp
*lacp
; /* LACP if LACP is enabled, otherwise NULL. */
146 struct bond
*bond
; /* Nonnull iff more than one port. */
149 bool floodable
; /* True if no port has OFPPC_NO_FLOOD set. */
151 /* Port mirroring info. */
152 mirror_mask_t src_mirrors
; /* Mirrors triggered when packet received. */
153 mirror_mask_t dst_mirrors
; /* Mirrors triggered when packet sent. */
154 mirror_mask_t mirror_out
; /* Mirrors that output to this bundle. */
157 static void bundle_remove(struct ofport
*);
158 static void bundle_destroy(struct ofbundle
*);
159 static void bundle_del_port(struct ofport_dpif
*);
160 static void bundle_run(struct ofbundle
*);
161 static void bundle_wait(struct ofbundle
*);
163 struct action_xlate_ctx
{
164 /* action_xlate_ctx_init() initializes these members. */
167 struct ofproto_dpif
*ofproto
;
169 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
170 * this flow when actions change header fields. */
173 /* The packet corresponding to 'flow', or a null pointer if we are
174 * revalidating without a packet to refer to. */
175 const struct ofpbuf
*packet
;
177 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
178 * want to execute them if we are actually processing a packet, or if we
179 * are accounting for packets that the datapath has processed, but not if
180 * we are just revalidating. */
183 /* If nonnull, called just before executing a resubmit action.
185 * This is normally null so the client has to set it manually after
186 * calling action_xlate_ctx_init(). */
187 void (*resubmit_hook
)(struct action_xlate_ctx
*, struct rule_dpif
*);
189 /* xlate_actions() initializes and uses these members. The client might want
190 * to look at them after it returns. */
192 struct ofpbuf
*odp_actions
; /* Datapath actions. */
193 tag_type tags
; /* Tags associated with actions. */
194 bool may_set_up_flow
; /* True ordinarily; false if the actions must
195 * be reassessed for every packet. */
196 bool has_learn
; /* Actions include NXAST_LEARN? */
197 bool has_normal
; /* Actions output to OFPP_NORMAL? */
198 uint16_t nf_output_iface
; /* Output interface index for NetFlow. */
200 /* xlate_actions() initializes and uses these members, but the client has no
201 * reason to look at them. */
203 int recurse
; /* Recursion level, via xlate_table_action. */
204 uint32_t priority
; /* Current flow priority. 0 if none. */
205 struct flow base_flow
; /* Flow at the last commit. */
206 uint32_t base_priority
; /* Priority at the last commit. */
207 uint8_t table_id
; /* OpenFlow table ID where flow was found. */
210 static void action_xlate_ctx_init(struct action_xlate_ctx
*,
211 struct ofproto_dpif
*, const struct flow
*,
212 const struct ofpbuf
*);
213 static struct ofpbuf
*xlate_actions(struct action_xlate_ctx
*,
214 const union ofp_action
*in
, size_t n_in
);
216 /* An exact-match instantiation of an OpenFlow flow. */
218 long long int used
; /* Time last used; time created if not used. */
222 * - Do include packets and bytes sent "by hand", e.g. with
225 * - Do include packets and bytes that were obtained from the datapath
226 * when its statistics were reset (e.g. dpif_flow_put() with
227 * DPIF_FP_ZERO_STATS).
229 uint64_t packet_count
; /* Number of packets received. */
230 uint64_t byte_count
; /* Number of bytes received. */
232 uint64_t dp_packet_count
; /* Last known packet count in the datapath. */
233 uint64_t dp_byte_count
; /* Last known byte count in the datapath. */
235 uint64_t rs_packet_count
; /* Packets pushed to resubmit children. */
236 uint64_t rs_byte_count
; /* Bytes pushed to resubmit children. */
237 long long int rs_used
; /* Used time pushed to resubmit children. */
239 uint64_t accounted_bytes
; /* Bytes processed by facet_account(). */
241 struct hmap_node hmap_node
; /* In owning ofproto's 'facets' hmap. */
242 struct list list_node
; /* In owning rule's 'facets' list. */
243 struct rule_dpif
*rule
; /* Owning rule. */
244 struct flow flow
; /* Exact-match flow. */
245 bool installed
; /* Installed in datapath? */
246 bool may_install
; /* True ordinarily; false if actions must
247 * be reassessed for every packet. */
248 bool has_learn
; /* Actions include NXAST_LEARN? */
249 bool has_normal
; /* Actions output to OFPP_NORMAL? */
250 size_t actions_len
; /* Number of bytes in actions[]. */
251 struct nlattr
*actions
; /* Datapath actions. */
252 tag_type tags
; /* Tags. */
253 struct netflow_flow nf_flow
; /* Per-flow NetFlow tracking data. */
256 static struct facet
*facet_create(struct rule_dpif
*, const struct flow
*,
257 const struct ofpbuf
*packet
);
258 static void facet_remove(struct ofproto_dpif
*, struct facet
*);
259 static void facet_free(struct facet
*);
261 static struct facet
*facet_find(struct ofproto_dpif
*, const struct flow
*);
262 static struct facet
*facet_lookup_valid(struct ofproto_dpif
*,
263 const struct flow
*);
264 static bool facet_revalidate(struct ofproto_dpif
*, struct facet
*);
266 static void facet_execute(struct ofproto_dpif
*, struct facet
*,
267 struct ofpbuf
*packet
);
269 static int facet_put__(struct ofproto_dpif
*, struct facet
*,
270 const struct nlattr
*actions
, size_t actions_len
,
271 struct dpif_flow_stats
*);
272 static void facet_install(struct ofproto_dpif
*, struct facet
*,
274 static void facet_uninstall(struct ofproto_dpif
*, struct facet
*);
275 static void facet_flush_stats(struct ofproto_dpif
*, struct facet
*);
277 static void facet_make_actions(struct ofproto_dpif
*, struct facet
*,
278 const struct ofpbuf
*packet
);
279 static void facet_update_time(struct ofproto_dpif
*, struct facet
*,
281 static void facet_update_stats(struct ofproto_dpif
*, struct facet
*,
282 const struct dpif_flow_stats
*);
283 static void facet_reset_counters(struct facet
*);
284 static void facet_reset_dp_stats(struct facet
*, struct dpif_flow_stats
*);
285 static void facet_push_stats(struct facet
*);
286 static void facet_account(struct ofproto_dpif
*, struct facet
*);
288 static bool facet_is_controller_flow(struct facet
*);
290 static void flow_push_stats(const struct rule_dpif
*,
291 struct flow
*, uint64_t packets
, uint64_t bytes
,
294 static uint32_t rule_calculate_tag(const struct flow
*,
295 const struct flow_wildcards
*,
297 static void rule_invalidate(const struct rule_dpif
*);
303 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
304 struct list bundle_node
; /* In struct ofbundle's "ports" list. */
305 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
306 tag_type tag
; /* Tag associated with this port. */
307 uint32_t bond_stable_id
; /* stable_id to use as bond slave, or 0. */
308 bool may_enable
; /* May be enabled in bonds. */
311 static struct ofport_dpif
*
312 ofport_dpif_cast(const struct ofport
*ofport
)
314 assert(ofport
->ofproto
->ofproto_class
== &ofproto_dpif_class
);
315 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
318 static void port_run(struct ofport_dpif
*);
319 static void port_wait(struct ofport_dpif
*);
320 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
322 struct dpif_completion
{
323 struct list list_node
;
324 struct ofoperation
*op
;
327 /* Extra information about a classifier table.
328 * Currently used just for optimized flow revalidation. */
330 /* If either of these is nonnull, then this table has a form that allows
331 * flows to be tagged to avoid revalidating most flows for the most common
332 * kinds of flow table changes. */
333 struct cls_table
*catchall_table
; /* Table that wildcards all fields. */
334 struct cls_table
*other_table
; /* Table with any other wildcard set. */
335 uint32_t basis
; /* Keeps each table's tags separate. */
338 struct ofproto_dpif
{
347 struct netflow
*netflow
;
348 struct dpif_sflow
*sflow
;
349 struct hmap bundles
; /* Contains "struct ofbundle"s. */
350 struct mac_learning
*ml
;
351 struct ofmirror
*mirrors
[MAX_MIRRORS
];
352 bool has_bonded_bundles
;
355 struct timer next_expiration
;
361 struct table_dpif tables
[N_TABLES
];
362 bool need_revalidate
;
363 struct tag_set revalidate_set
;
365 /* Support for debugging async flow mods. */
366 struct list completions
;
368 bool has_bundle_action
; /* True when the first bundle action appears. */
371 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
372 * for debugging the asynchronous flow_mod implementation.) */
375 static void ofproto_dpif_unixctl_init(void);
377 static struct ofproto_dpif
*
378 ofproto_dpif_cast(const struct ofproto
*ofproto
)
380 assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
381 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
384 static struct ofport_dpif
*get_ofp_port(struct ofproto_dpif
*,
386 static struct ofport_dpif
*get_odp_port(struct ofproto_dpif
*,
389 /* Packet processing. */
390 static void update_learning_table(struct ofproto_dpif
*,
391 const struct flow
*, int vlan
,
393 static bool is_admissible(struct ofproto_dpif
*, const struct flow
*,
394 bool have_packet
, tag_type
*, int *vlanp
,
395 struct ofbundle
**in_bundlep
);
396 static void handle_upcall(struct ofproto_dpif
*, struct dpif_upcall
*);
398 /* Flow expiration. */
399 static int expire(struct ofproto_dpif
*);
402 static int send_packet(struct ofproto_dpif
*, uint32_t odp_port
,
403 const struct ofpbuf
*packet
);
405 /* Global variables. */
406 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
408 /* Factory functions. */
411 enumerate_types(struct sset
*types
)
413 dp_enumerate_types(types
);
417 enumerate_names(const char *type
, struct sset
*names
)
419 return dp_enumerate_names(type
, names
);
423 del(const char *type
, const char *name
)
428 error
= dpif_open(name
, type
, &dpif
);
430 error
= dpif_delete(dpif
);
436 /* Basic life-cycle. */
438 static struct ofproto
*
441 struct ofproto_dpif
*ofproto
= xmalloc(sizeof *ofproto
);
446 dealloc(struct ofproto
*ofproto_
)
448 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
453 construct(struct ofproto
*ofproto_
, int *n_tablesp
)
455 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
456 const char *name
= ofproto
->up
.name
;
460 error
= dpif_create_and_open(name
, ofproto
->up
.type
, &ofproto
->dpif
);
462 VLOG_ERR("failed to open datapath %s: %s", name
, strerror(error
));
466 ofproto
->max_ports
= dpif_get_max_ports(ofproto
->dpif
);
467 ofproto
->n_matches
= 0;
469 dpif_flow_flush(ofproto
->dpif
);
470 dpif_recv_purge(ofproto
->dpif
);
472 error
= dpif_recv_set_mask(ofproto
->dpif
,
473 ((1u << DPIF_UC_MISS
) |
474 (1u << DPIF_UC_ACTION
) |
475 (1u << DPIF_UC_SAMPLE
)));
477 VLOG_ERR("failed to listen on datapath %s: %s", name
, strerror(error
));
478 dpif_close(ofproto
->dpif
);
482 ofproto
->netflow
= NULL
;
483 ofproto
->sflow
= NULL
;
484 hmap_init(&ofproto
->bundles
);
485 ofproto
->ml
= mac_learning_create();
486 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
487 ofproto
->mirrors
[i
] = NULL
;
489 ofproto
->has_bonded_bundles
= false;
491 timer_set_duration(&ofproto
->next_expiration
, 1000);
493 hmap_init(&ofproto
->facets
);
495 for (i
= 0; i
< N_TABLES
; i
++) {
496 struct table_dpif
*table
= &ofproto
->tables
[i
];
498 table
->catchall_table
= NULL
;
499 table
->other_table
= NULL
;
500 table
->basis
= random_uint32();
502 ofproto
->need_revalidate
= false;
503 tag_set_init(&ofproto
->revalidate_set
);
505 list_init(&ofproto
->completions
);
507 ofproto_dpif_unixctl_init();
509 ofproto
->has_bundle_action
= false;
511 *n_tablesp
= N_TABLES
;
516 complete_operations(struct ofproto_dpif
*ofproto
)
518 struct dpif_completion
*c
, *next
;
520 LIST_FOR_EACH_SAFE (c
, next
, list_node
, &ofproto
->completions
) {
521 ofoperation_complete(c
->op
, 0);
522 list_remove(&c
->list_node
);
528 destruct(struct ofproto
*ofproto_
)
530 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
531 struct rule_dpif
*rule
, *next_rule
;
532 struct classifier
*table
;
535 complete_operations(ofproto
);
537 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
538 struct cls_cursor cursor
;
540 cls_cursor_init(&cursor
, table
, NULL
);
541 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, up
.cr
, &cursor
) {
542 ofproto_rule_destroy(&rule
->up
);
546 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
547 mirror_destroy(ofproto
->mirrors
[i
]);
550 netflow_destroy(ofproto
->netflow
);
551 dpif_sflow_destroy(ofproto
->sflow
);
552 hmap_destroy(&ofproto
->bundles
);
553 mac_learning_destroy(ofproto
->ml
);
555 hmap_destroy(&ofproto
->facets
);
557 dpif_close(ofproto
->dpif
);
561 run(struct ofproto
*ofproto_
)
563 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
564 struct ofport_dpif
*ofport
;
565 struct ofbundle
*bundle
;
569 complete_operations(ofproto
);
571 dpif_run(ofproto
->dpif
);
573 for (i
= 0; i
< 50; i
++) {
574 struct dpif_upcall packet
;
577 error
= dpif_recv(ofproto
->dpif
, &packet
);
579 if (error
== ENODEV
) {
580 /* Datapath destroyed. */
586 handle_upcall(ofproto
, &packet
);
589 if (timer_expired(&ofproto
->next_expiration
)) {
590 int delay
= expire(ofproto
);
591 timer_set_duration(&ofproto
->next_expiration
, delay
);
594 if (ofproto
->netflow
) {
595 netflow_run(ofproto
->netflow
);
597 if (ofproto
->sflow
) {
598 dpif_sflow_run(ofproto
->sflow
);
601 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
604 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
608 mac_learning_run(ofproto
->ml
, &ofproto
->revalidate_set
);
610 /* Now revalidate if there's anything to do. */
611 if (ofproto
->need_revalidate
612 || !tag_set_is_empty(&ofproto
->revalidate_set
)) {
613 struct tag_set revalidate_set
= ofproto
->revalidate_set
;
614 bool revalidate_all
= ofproto
->need_revalidate
;
615 struct facet
*facet
, *next
;
617 /* Clear the revalidation flags. */
618 tag_set_init(&ofproto
->revalidate_set
);
619 ofproto
->need_revalidate
= false;
621 HMAP_FOR_EACH_SAFE (facet
, next
, hmap_node
, &ofproto
->facets
) {
623 || tag_set_intersects(&revalidate_set
, facet
->tags
)) {
624 facet_revalidate(ofproto
, facet
);
633 wait(struct ofproto
*ofproto_
)
635 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
636 struct ofport_dpif
*ofport
;
637 struct ofbundle
*bundle
;
639 if (!clogged
&& !list_is_empty(&ofproto
->completions
)) {
640 poll_immediate_wake();
643 dpif_wait(ofproto
->dpif
);
644 dpif_recv_wait(ofproto
->dpif
);
645 if (ofproto
->sflow
) {
646 dpif_sflow_wait(ofproto
->sflow
);
648 if (!tag_set_is_empty(&ofproto
->revalidate_set
)) {
649 poll_immediate_wake();
651 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
654 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
657 mac_learning_wait(ofproto
->ml
);
658 if (ofproto
->need_revalidate
) {
659 /* Shouldn't happen, but if it does just go around again. */
660 VLOG_DBG_RL(&rl
, "need revalidate in ofproto_wait_cb()");
661 poll_immediate_wake();
663 timer_wait(&ofproto
->next_expiration
);
668 flush(struct ofproto
*ofproto_
)
670 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
671 struct facet
*facet
, *next_facet
;
673 HMAP_FOR_EACH_SAFE (facet
, next_facet
, hmap_node
, &ofproto
->facets
) {
674 /* Mark the facet as not installed so that facet_remove() doesn't
675 * bother trying to uninstall it. There is no point in uninstalling it
676 * individually since we are about to blow away all the facets with
677 * dpif_flow_flush(). */
678 facet
->installed
= false;
679 facet
->dp_packet_count
= 0;
680 facet
->dp_byte_count
= 0;
681 facet_remove(ofproto
, facet
);
683 dpif_flow_flush(ofproto
->dpif
);
687 get_features(struct ofproto
*ofproto_ OVS_UNUSED
,
688 bool *arp_match_ip
, uint32_t *actions
)
690 *arp_match_ip
= true;
691 *actions
= ((1u << OFPAT_OUTPUT
) |
692 (1u << OFPAT_SET_VLAN_VID
) |
693 (1u << OFPAT_SET_VLAN_PCP
) |
694 (1u << OFPAT_STRIP_VLAN
) |
695 (1u << OFPAT_SET_DL_SRC
) |
696 (1u << OFPAT_SET_DL_DST
) |
697 (1u << OFPAT_SET_NW_SRC
) |
698 (1u << OFPAT_SET_NW_DST
) |
699 (1u << OFPAT_SET_NW_TOS
) |
700 (1u << OFPAT_SET_TP_SRC
) |
701 (1u << OFPAT_SET_TP_DST
) |
702 (1u << OFPAT_ENQUEUE
));
706 get_tables(struct ofproto
*ofproto_
, struct ofp_table_stats
*ots
)
708 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
709 struct ovs_dp_stats s
;
711 strcpy(ots
->name
, "classifier");
713 dpif_get_dp_stats(ofproto
->dpif
, &s
);
714 put_32aligned_be64(&ots
->lookup_count
, htonll(s
.n_hit
+ s
.n_missed
));
715 put_32aligned_be64(&ots
->matched_count
,
716 htonll(s
.n_hit
+ ofproto
->n_matches
));
720 set_netflow(struct ofproto
*ofproto_
,
721 const struct netflow_options
*netflow_options
)
723 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
725 if (netflow_options
) {
726 if (!ofproto
->netflow
) {
727 ofproto
->netflow
= netflow_create();
729 return netflow_set_options(ofproto
->netflow
, netflow_options
);
731 netflow_destroy(ofproto
->netflow
);
732 ofproto
->netflow
= NULL
;
737 static struct ofport
*
740 struct ofport_dpif
*port
= xmalloc(sizeof *port
);
745 port_dealloc(struct ofport
*port_
)
747 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
752 port_construct(struct ofport
*port_
)
754 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
755 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
757 port
->odp_port
= ofp_port_to_odp_port(port
->up
.ofp_port
);
760 port
->tag
= tag_create_random();
761 port
->may_enable
= true;
763 if (ofproto
->sflow
) {
764 dpif_sflow_add_port(ofproto
->sflow
, port
->odp_port
,
765 netdev_get_name(port
->up
.netdev
));
772 port_destruct(struct ofport
*port_
)
774 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
775 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
777 bundle_remove(port_
);
778 set_cfm(port_
, NULL
);
779 if (ofproto
->sflow
) {
780 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
785 port_modified(struct ofport
*port_
)
787 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
789 if (port
->bundle
&& port
->bundle
->bond
) {
790 bond_slave_set_netdev(port
->bundle
->bond
, port
, port
->up
.netdev
);
795 port_reconfigured(struct ofport
*port_
, ovs_be32 old_config
)
797 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
798 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
799 ovs_be32 changed
= old_config
^ port
->up
.opp
.config
;
801 if (changed
& htonl(OFPPC_NO_RECV
| OFPPC_NO_RECV_STP
|
802 OFPPC_NO_FWD
| OFPPC_NO_FLOOD
)) {
803 ofproto
->need_revalidate
= true;
808 set_sflow(struct ofproto
*ofproto_
,
809 const struct ofproto_sflow_options
*sflow_options
)
811 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
812 struct dpif_sflow
*ds
= ofproto
->sflow
;
815 struct ofport_dpif
*ofport
;
817 ds
= ofproto
->sflow
= dpif_sflow_create(ofproto
->dpif
);
818 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
819 dpif_sflow_add_port(ds
, ofport
->odp_port
,
820 netdev_get_name(ofport
->up
.netdev
));
823 dpif_sflow_set_options(ds
, sflow_options
);
825 dpif_sflow_destroy(ds
);
826 ofproto
->sflow
= NULL
;
832 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
834 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
841 struct ofproto_dpif
*ofproto
;
843 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
844 ofproto
->need_revalidate
= true;
845 ofport
->cfm
= cfm_create(netdev_get_name(ofport
->up
.netdev
));
848 if (cfm_configure(ofport
->cfm
, s
)) {
854 cfm_destroy(ofport
->cfm
);
860 get_cfm_fault(const struct ofport
*ofport_
)
862 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
864 return ofport
->cfm
? cfm_get_fault(ofport
->cfm
) : -1;
868 get_cfm_remote_mpids(const struct ofport
*ofport_
, const uint64_t **rmps
,
871 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
874 cfm_get_remote_mpids(ofport
->cfm
, rmps
, n_rmps
);
883 /* Expires all MAC learning entries associated with 'port' and forces ofproto
884 * to revalidate every flow. */
886 bundle_flush_macs(struct ofbundle
*bundle
)
888 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
889 struct mac_learning
*ml
= ofproto
->ml
;
890 struct mac_entry
*mac
, *next_mac
;
892 ofproto
->need_revalidate
= true;
893 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
894 if (mac
->port
.p
== bundle
) {
895 mac_learning_expire(ml
, mac
);
900 static struct ofbundle
*
901 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
903 struct ofbundle
*bundle
;
905 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
907 if (bundle
->aux
== aux
) {
914 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
915 * ones that are found to 'bundles'. */
917 bundle_lookup_multiple(struct ofproto_dpif
*ofproto
,
918 void **auxes
, size_t n_auxes
,
919 struct hmapx
*bundles
)
924 for (i
= 0; i
< n_auxes
; i
++) {
925 struct ofbundle
*bundle
= bundle_lookup(ofproto
, auxes
[i
]);
927 hmapx_add(bundles
, bundle
);
933 bundle_del_port(struct ofport_dpif
*port
)
935 struct ofbundle
*bundle
= port
->bundle
;
937 bundle
->ofproto
->need_revalidate
= true;
939 list_remove(&port
->bundle_node
);
943 lacp_slave_unregister(bundle
->lacp
, port
);
946 bond_slave_unregister(bundle
->bond
, port
);
949 bundle
->floodable
= true;
950 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
951 if (port
->up
.opp
.config
& htonl(OFPPC_NO_FLOOD
)) {
952 bundle
->floodable
= false;
958 bundle_add_port(struct ofbundle
*bundle
, uint32_t ofp_port
,
959 struct lacp_slave_settings
*lacp
,
960 uint32_t bond_stable_id
)
962 struct ofport_dpif
*port
;
964 port
= get_ofp_port(bundle
->ofproto
, ofp_port
);
969 if (port
->bundle
!= bundle
) {
970 bundle
->ofproto
->need_revalidate
= true;
972 bundle_del_port(port
);
975 port
->bundle
= bundle
;
976 list_push_back(&bundle
->ports
, &port
->bundle_node
);
977 if (port
->up
.opp
.config
& htonl(OFPPC_NO_FLOOD
)) {
978 bundle
->floodable
= false;
982 lacp_slave_register(bundle
->lacp
, port
, lacp
);
985 port
->bond_stable_id
= bond_stable_id
;
991 bundle_destroy(struct ofbundle
*bundle
)
993 struct ofproto_dpif
*ofproto
;
994 struct ofport_dpif
*port
, *next_port
;
1001 ofproto
= bundle
->ofproto
;
1002 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
1003 struct ofmirror
*m
= ofproto
->mirrors
[i
];
1005 if (m
->out
== bundle
) {
1007 } else if (hmapx_find_and_delete(&m
->srcs
, bundle
)
1008 || hmapx_find_and_delete(&m
->dsts
, bundle
)) {
1009 ofproto
->need_revalidate
= true;
1014 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
1015 bundle_del_port(port
);
1018 bundle_flush_macs(bundle
);
1019 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
1021 free(bundle
->trunks
);
1022 lacp_destroy(bundle
->lacp
);
1023 bond_destroy(bundle
->bond
);
1028 bundle_set(struct ofproto
*ofproto_
, void *aux
,
1029 const struct ofproto_bundle_settings
*s
)
1031 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1032 bool need_flush
= false;
1033 struct ofport_dpif
*port
;
1034 struct ofbundle
*bundle
;
1035 unsigned long *trunks
;
1041 bundle_destroy(bundle_lookup(ofproto
, aux
));
1045 assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
1046 assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
1048 bundle
= bundle_lookup(ofproto
, aux
);
1050 bundle
= xmalloc(sizeof *bundle
);
1052 bundle
->ofproto
= ofproto
;
1053 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
1054 hash_pointer(aux
, 0));
1056 bundle
->name
= NULL
;
1058 list_init(&bundle
->ports
);
1059 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
1061 bundle
->trunks
= NULL
;
1062 bundle
->lacp
= NULL
;
1063 bundle
->bond
= NULL
;
1065 bundle
->floodable
= true;
1067 bundle
->src_mirrors
= 0;
1068 bundle
->dst_mirrors
= 0;
1069 bundle
->mirror_out
= 0;
1072 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
1074 bundle
->name
= xstrdup(s
->name
);
1079 if (!bundle
->lacp
) {
1080 ofproto
->need_revalidate
= true;
1081 bundle
->lacp
= lacp_create();
1083 lacp_configure(bundle
->lacp
, s
->lacp
);
1085 lacp_destroy(bundle
->lacp
);
1086 bundle
->lacp
= NULL
;
1089 /* Update set of ports. */
1091 for (i
= 0; i
< s
->n_slaves
; i
++) {
1092 if (!bundle_add_port(bundle
, s
->slaves
[i
],
1093 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
,
1094 s
->bond_stable_ids
? s
->bond_stable_ids
[i
] : 0)) {
1098 if (!ok
|| list_size(&bundle
->ports
) != s
->n_slaves
) {
1099 struct ofport_dpif
*next_port
;
1101 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
1102 for (i
= 0; i
< s
->n_slaves
; i
++) {
1103 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
1108 bundle_del_port(port
);
1112 assert(list_size(&bundle
->ports
) <= s
->n_slaves
);
1114 if (list_is_empty(&bundle
->ports
)) {
1115 bundle_destroy(bundle
);
1119 /* Set VLAN tagging mode */
1120 if (s
->vlan_mode
!= bundle
->vlan_mode
) {
1121 bundle
->vlan_mode
= s
->vlan_mode
;
1126 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
1127 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
1129 if (vlan
!= bundle
->vlan
) {
1130 bundle
->vlan
= vlan
;
1134 /* Get trunked VLANs. */
1135 switch (s
->vlan_mode
) {
1136 case PORT_VLAN_ACCESS
:
1140 case PORT_VLAN_TRUNK
:
1141 trunks
= (unsigned long *) s
->trunks
;
1144 case PORT_VLAN_NATIVE_UNTAGGED
:
1145 case PORT_VLAN_NATIVE_TAGGED
:
1146 if (vlan
!= 0 && (!s
->trunks
1147 || !bitmap_is_set(s
->trunks
, vlan
)
1148 || bitmap_is_set(s
->trunks
, 0))) {
1149 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1151 trunks
= bitmap_clone(s
->trunks
, 4096);
1153 trunks
= bitmap_allocate1(4096);
1155 bitmap_set1(trunks
, vlan
);
1156 bitmap_set0(trunks
, 0);
1158 trunks
= (unsigned long *) s
->trunks
;
1165 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
1166 free(bundle
->trunks
);
1167 if (trunks
== s
->trunks
) {
1168 bundle
->trunks
= vlan_bitmap_clone(trunks
);
1170 bundle
->trunks
= trunks
;
1175 if (trunks
!= s
->trunks
) {
1180 if (!list_is_short(&bundle
->ports
)) {
1181 bundle
->ofproto
->has_bonded_bundles
= true;
1183 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
1184 ofproto
->need_revalidate
= true;
1187 bundle
->bond
= bond_create(s
->bond
);
1188 ofproto
->need_revalidate
= true;
1191 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
1192 bond_slave_register(bundle
->bond
, port
, port
->bond_stable_id
,
1196 bond_destroy(bundle
->bond
);
1197 bundle
->bond
= NULL
;
1200 /* If we changed something that would affect MAC learning, un-learn
1201 * everything on this port and force flow revalidation. */
1203 bundle_flush_macs(bundle
);
1210 bundle_remove(struct ofport
*port_
)
1212 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1213 struct ofbundle
*bundle
= port
->bundle
;
1216 bundle_del_port(port
);
1217 if (list_is_empty(&bundle
->ports
)) {
1218 bundle_destroy(bundle
);
1219 } else if (list_is_short(&bundle
->ports
)) {
1220 bond_destroy(bundle
->bond
);
1221 bundle
->bond
= NULL
;
1227 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
1229 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
1230 struct ofport_dpif
*port
= port_
;
1231 uint8_t ea
[ETH_ADDR_LEN
];
1234 error
= netdev_get_etheraddr(port
->up
.netdev
, ea
);
1236 struct ofpbuf packet
;
1239 ofpbuf_init(&packet
, 0);
1240 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
1242 memcpy(packet_pdu
, pdu
, pdu_size
);
1244 error
= netdev_send(port
->up
.netdev
, &packet
);
1246 VLOG_WARN_RL(&rl
, "port %s: sending LACP PDU on iface %s failed "
1247 "(%s)", port
->bundle
->name
,
1248 netdev_get_name(port
->up
.netdev
), strerror(error
));
1250 ofpbuf_uninit(&packet
);
1252 VLOG_ERR_RL(&rl
, "port %s: cannot obtain Ethernet address of iface "
1253 "%s (%s)", port
->bundle
->name
,
1254 netdev_get_name(port
->up
.netdev
), strerror(error
));
1259 bundle_send_learning_packets(struct ofbundle
*bundle
)
1261 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
1262 int error
, n_packets
, n_errors
;
1263 struct mac_entry
*e
;
1265 error
= n_packets
= n_errors
= 0;
1266 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
1267 if (e
->port
.p
!= bundle
) {
1268 int ret
= bond_send_learning_packet(bundle
->bond
, e
->mac
, e
->vlan
);
1278 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1279 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
1280 "packets, last error was: %s",
1281 bundle
->name
, n_errors
, n_packets
, strerror(error
));
1283 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1284 bundle
->name
, n_packets
);
1289 bundle_run(struct ofbundle
*bundle
)
1292 lacp_run(bundle
->lacp
, send_pdu_cb
);
1295 struct ofport_dpif
*port
;
1297 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
1298 bond_slave_set_may_enable(bundle
->bond
, port
, port
->may_enable
);
1301 bond_run(bundle
->bond
, &bundle
->ofproto
->revalidate_set
,
1302 lacp_negotiated(bundle
->lacp
));
1303 if (bond_should_send_learning_packets(bundle
->bond
)) {
1304 bundle_send_learning_packets(bundle
);
1310 bundle_wait(struct ofbundle
*bundle
)
1313 lacp_wait(bundle
->lacp
);
1316 bond_wait(bundle
->bond
);
1323 mirror_scan(struct ofproto_dpif
*ofproto
)
1327 for (idx
= 0; idx
< MAX_MIRRORS
; idx
++) {
1328 if (!ofproto
->mirrors
[idx
]) {
1335 static struct ofmirror
*
1336 mirror_lookup(struct ofproto_dpif
*ofproto
, void *aux
)
1340 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
1341 struct ofmirror
*mirror
= ofproto
->mirrors
[i
];
1342 if (mirror
&& mirror
->aux
== aux
) {
1351 mirror_set(struct ofproto
*ofproto_
, void *aux
,
1352 const struct ofproto_mirror_settings
*s
)
1354 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1355 mirror_mask_t mirror_bit
;
1356 struct ofbundle
*bundle
;
1357 struct ofmirror
*mirror
;
1358 struct ofbundle
*out
;
1359 struct hmapx srcs
; /* Contains "struct ofbundle *"s. */
1360 struct hmapx dsts
; /* Contains "struct ofbundle *"s. */
1363 mirror
= mirror_lookup(ofproto
, aux
);
1365 mirror_destroy(mirror
);
1371 idx
= mirror_scan(ofproto
);
1373 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1375 ofproto
->up
.name
, MAX_MIRRORS
, s
->name
);
1379 mirror
= ofproto
->mirrors
[idx
] = xzalloc(sizeof *mirror
);
1380 mirror
->ofproto
= ofproto
;
1383 mirror
->out_vlan
= -1;
1384 mirror
->name
= NULL
;
1387 if (!mirror
->name
|| strcmp(s
->name
, mirror
->name
)) {
1389 mirror
->name
= xstrdup(s
->name
);
1392 /* Get the new configuration. */
1393 if (s
->out_bundle
) {
1394 out
= bundle_lookup(ofproto
, s
->out_bundle
);
1396 mirror_destroy(mirror
);
1402 out_vlan
= s
->out_vlan
;
1404 bundle_lookup_multiple(ofproto
, s
->srcs
, s
->n_srcs
, &srcs
);
1405 bundle_lookup_multiple(ofproto
, s
->dsts
, s
->n_dsts
, &dsts
);
1407 /* If the configuration has not changed, do nothing. */
1408 if (hmapx_equals(&srcs
, &mirror
->srcs
)
1409 && hmapx_equals(&dsts
, &mirror
->dsts
)
1410 && vlan_bitmap_equal(mirror
->vlans
, s
->src_vlans
)
1411 && mirror
->out
== out
1412 && mirror
->out_vlan
== out_vlan
)
1414 hmapx_destroy(&srcs
);
1415 hmapx_destroy(&dsts
);
1419 hmapx_swap(&srcs
, &mirror
->srcs
);
1420 hmapx_destroy(&srcs
);
1422 hmapx_swap(&dsts
, &mirror
->dsts
);
1423 hmapx_destroy(&dsts
);
1425 free(mirror
->vlans
);
1426 mirror
->vlans
= vlan_bitmap_clone(s
->src_vlans
);
1429 mirror
->out_vlan
= out_vlan
;
1431 /* Update bundles. */
1432 mirror_bit
= MIRROR_MASK_C(1) << mirror
->idx
;
1433 HMAP_FOR_EACH (bundle
, hmap_node
, &mirror
->ofproto
->bundles
) {
1434 if (hmapx_contains(&mirror
->srcs
, bundle
)) {
1435 bundle
->src_mirrors
|= mirror_bit
;
1437 bundle
->src_mirrors
&= ~mirror_bit
;
1440 if (hmapx_contains(&mirror
->dsts
, bundle
)) {
1441 bundle
->dst_mirrors
|= mirror_bit
;
1443 bundle
->dst_mirrors
&= ~mirror_bit
;
1446 if (mirror
->out
== bundle
) {
1447 bundle
->mirror_out
|= mirror_bit
;
1449 bundle
->mirror_out
&= ~mirror_bit
;
1453 ofproto
->need_revalidate
= true;
1454 mac_learning_flush(ofproto
->ml
);
1460 mirror_destroy(struct ofmirror
*mirror
)
1462 struct ofproto_dpif
*ofproto
;
1463 mirror_mask_t mirror_bit
;
1464 struct ofbundle
*bundle
;
1470 ofproto
= mirror
->ofproto
;
1471 ofproto
->need_revalidate
= true;
1472 mac_learning_flush(ofproto
->ml
);
1474 mirror_bit
= MIRROR_MASK_C(1) << mirror
->idx
;
1475 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1476 bundle
->src_mirrors
&= ~mirror_bit
;
1477 bundle
->dst_mirrors
&= ~mirror_bit
;
1478 bundle
->mirror_out
&= ~mirror_bit
;
1481 hmapx_destroy(&mirror
->srcs
);
1482 hmapx_destroy(&mirror
->dsts
);
1483 free(mirror
->vlans
);
1485 ofproto
->mirrors
[mirror
->idx
] = NULL
;
1491 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
1493 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1494 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
1495 ofproto
->need_revalidate
= true;
1496 mac_learning_flush(ofproto
->ml
);
1502 is_mirror_output_bundle(struct ofproto
*ofproto_
, void *aux
)
1504 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1505 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
1506 return bundle
&& bundle
->mirror_out
!= 0;
1510 forward_bpdu_changed(struct ofproto
*ofproto_
)
1512 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1513 /* Revalidate cached flows whenever forward_bpdu option changes. */
1514 ofproto
->need_revalidate
= true;
1519 static struct ofport_dpif
*
1520 get_ofp_port(struct ofproto_dpif
*ofproto
, uint16_t ofp_port
)
1522 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
1523 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
1526 static struct ofport_dpif
*
1527 get_odp_port(struct ofproto_dpif
*ofproto
, uint32_t odp_port
)
1529 return get_ofp_port(ofproto
, odp_port_to_ofp_port(odp_port
));
1533 ofproto_port_from_dpif_port(struct ofproto_port
*ofproto_port
,
1534 struct dpif_port
*dpif_port
)
1536 ofproto_port
->name
= dpif_port
->name
;
1537 ofproto_port
->type
= dpif_port
->type
;
1538 ofproto_port
->ofp_port
= odp_port_to_ofp_port(dpif_port
->port_no
);
1542 port_run(struct ofport_dpif
*ofport
)
1544 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
1547 cfm_run(ofport
->cfm
);
1549 if (cfm_should_send_ccm(ofport
->cfm
)) {
1550 struct ofpbuf packet
;
1552 ofpbuf_init(&packet
, 0);
1553 cfm_compose_ccm(ofport
->cfm
, &packet
, ofport
->up
.opp
.hw_addr
);
1554 send_packet(ofproto_dpif_cast(ofport
->up
.ofproto
),
1555 ofport
->odp_port
, &packet
);
1556 ofpbuf_uninit(&packet
);
1559 enable
= enable
&& !cfm_get_fault(ofport
->cfm
);
1562 if (ofport
->bundle
) {
1563 enable
= enable
&& lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
);
1566 if (ofport
->may_enable
!= enable
) {
1567 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
1569 if (ofproto
->has_bundle_action
) {
1570 ofproto
->need_revalidate
= true;
1574 ofport
->may_enable
= enable
;
1578 port_wait(struct ofport_dpif
*ofport
)
1581 cfm_wait(ofport
->cfm
);
1586 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
1587 struct ofproto_port
*ofproto_port
)
1589 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1590 struct dpif_port dpif_port
;
1593 error
= dpif_port_query_by_name(ofproto
->dpif
, devname
, &dpif_port
);
1595 ofproto_port_from_dpif_port(ofproto_port
, &dpif_port
);
1601 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
, uint16_t *ofp_portp
)
1603 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1607 error
= dpif_port_add(ofproto
->dpif
, netdev
, &odp_port
);
1609 *ofp_portp
= odp_port_to_ofp_port(odp_port
);
1615 port_del(struct ofproto
*ofproto_
, uint16_t ofp_port
)
1617 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1620 error
= dpif_port_del(ofproto
->dpif
, ofp_port_to_odp_port(ofp_port
));
1622 struct ofport_dpif
*ofport
= get_ofp_port(ofproto
, ofp_port
);
1624 /* The caller is going to close ofport->up.netdev. If this is a
1625 * bonded port, then the bond is using that netdev, so remove it
1626 * from the bond. The client will need to reconfigure everything
1627 * after deleting ports, so then the slave will get re-added. */
1628 bundle_remove(&ofport
->up
);
1634 struct port_dump_state
{
1635 struct dpif_port_dump dump
;
1640 port_dump_start(const struct ofproto
*ofproto_
, void **statep
)
1642 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1643 struct port_dump_state
*state
;
1645 *statep
= state
= xmalloc(sizeof *state
);
1646 dpif_port_dump_start(&state
->dump
, ofproto
->dpif
);
1647 state
->done
= false;
1652 port_dump_next(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
,
1653 struct ofproto_port
*port
)
1655 struct port_dump_state
*state
= state_
;
1656 struct dpif_port dpif_port
;
1658 if (dpif_port_dump_next(&state
->dump
, &dpif_port
)) {
1659 ofproto_port_from_dpif_port(port
, &dpif_port
);
1662 int error
= dpif_port_dump_done(&state
->dump
);
1664 return error
? error
: EOF
;
1669 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
1671 struct port_dump_state
*state
= state_
;
1674 dpif_port_dump_done(&state
->dump
);
1681 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
1683 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1684 return dpif_port_poll(ofproto
->dpif
, devnamep
);
1688 port_poll_wait(const struct ofproto
*ofproto_
)
1690 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1691 dpif_port_poll_wait(ofproto
->dpif
);
1695 port_is_lacp_current(const struct ofport
*ofport_
)
1697 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1698 return (ofport
->bundle
&& ofport
->bundle
->lacp
1699 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
1703 /* Upcall handling. */
1705 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1706 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1707 * their individual configurations.
1709 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1710 * Otherwise, ownership is transferred to this function. */
1712 send_packet_in(struct ofproto_dpif
*ofproto
, struct dpif_upcall
*upcall
,
1713 const struct flow
*flow
, bool clone
)
1715 struct ofputil_packet_in pin
;
1717 pin
.packet
= upcall
->packet
;
1718 pin
.in_port
= flow
->in_port
;
1719 pin
.reason
= upcall
->type
== DPIF_UC_MISS
? OFPR_NO_MATCH
: OFPR_ACTION
;
1720 pin
.buffer_id
= 0; /* not yet known */
1721 pin
.send_len
= upcall
->userdata
;
1722 connmgr_send_packet_in(ofproto
->up
.connmgr
, &pin
, flow
,
1723 clone
? NULL
: upcall
->packet
);
1727 process_special(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
1728 const struct ofpbuf
*packet
)
1730 struct ofport_dpif
*ofport
= get_ofp_port(ofproto
, flow
->in_port
);
1736 if (ofport
->cfm
&& cfm_should_process_flow(ofport
->cfm
, flow
)) {
1738 cfm_process_heartbeat(ofport
->cfm
, packet
);
1741 } else if (ofport
->bundle
&& ofport
->bundle
->lacp
1742 && flow
->dl_type
== htons(ETH_TYPE_LACP
)) {
1744 lacp_process_packet(ofport
->bundle
->lacp
, ofport
, packet
);
1752 handle_miss_upcall(struct ofproto_dpif
*ofproto
, struct dpif_upcall
*upcall
)
1754 struct facet
*facet
;
1757 /* Obtain in_port and tun_id, at least. */
1758 odp_flow_key_to_flow(upcall
->key
, upcall
->key_len
, &flow
);
1760 /* Set header pointers in 'flow'. */
1761 flow_extract(upcall
->packet
, flow
.tun_id
, flow
.in_port
, &flow
);
1763 /* Handle 802.1ag and LACP. */
1764 if (process_special(ofproto
, &flow
, upcall
->packet
)) {
1765 ofpbuf_delete(upcall
->packet
);
1766 ofproto
->n_matches
++;
1770 /* Check with in-band control to see if this packet should be sent
1771 * to the local port regardless of the flow table. */
1772 if (connmgr_msg_in_hook(ofproto
->up
.connmgr
, &flow
, upcall
->packet
)) {
1773 send_packet(ofproto
, OVSP_LOCAL
, upcall
->packet
);
1776 facet
= facet_lookup_valid(ofproto
, &flow
);
1778 struct rule_dpif
*rule
= rule_dpif_lookup(ofproto
, &flow
, 0);
1780 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1781 struct ofport_dpif
*port
= get_ofp_port(ofproto
, flow
.in_port
);
1783 if (port
->up
.opp
.config
& htonl(OFPPC_NO_PACKET_IN
)) {
1784 COVERAGE_INC(ofproto_dpif_no_packet_in
);
1785 /* XXX install 'drop' flow entry */
1786 ofpbuf_delete(upcall
->packet
);
1790 VLOG_WARN_RL(&rl
, "packet-in on unknown port %"PRIu16
,
1794 send_packet_in(ofproto
, upcall
, &flow
, false);
1798 facet
= facet_create(rule
, &flow
, upcall
->packet
);
1799 } else if (!facet
->may_install
) {
1800 /* The facet is not installable, that is, we need to process every
1801 * packet, so process the current packet's actions into 'facet'. */
1802 facet_make_actions(ofproto
, facet
, upcall
->packet
);
1805 if (facet
->rule
->up
.cr
.priority
== FAIL_OPEN_PRIORITY
) {
1807 * Extra-special case for fail-open mode.
1809 * We are in fail-open mode and the packet matched the fail-open rule,
1810 * but we are connected to a controller too. We should send the packet
1811 * up to the controller in the hope that it will try to set up a flow
1812 * and thereby allow us to exit fail-open.
1814 * See the top-level comment in fail-open.c for more information.
1816 send_packet_in(ofproto
, upcall
, &flow
, true);
1819 facet_execute(ofproto
, facet
, upcall
->packet
);
1820 facet_install(ofproto
, facet
, false);
1821 ofproto
->n_matches
++;
1825 handle_upcall(struct ofproto_dpif
*ofproto
, struct dpif_upcall
*upcall
)
1829 switch (upcall
->type
) {
1830 case DPIF_UC_ACTION
:
1831 COVERAGE_INC(ofproto_dpif_ctlr_action
);
1832 odp_flow_key_to_flow(upcall
->key
, upcall
->key_len
, &flow
);
1833 send_packet_in(ofproto
, upcall
, &flow
, false);
1836 case DPIF_UC_SAMPLE
:
1837 if (ofproto
->sflow
) {
1838 odp_flow_key_to_flow(upcall
->key
, upcall
->key_len
, &flow
);
1839 dpif_sflow_received(ofproto
->sflow
, upcall
, &flow
);
1841 ofpbuf_delete(upcall
->packet
);
1845 handle_miss_upcall(ofproto
, upcall
);
1848 case DPIF_N_UC_TYPES
:
1850 VLOG_WARN_RL(&rl
, "upcall has unexpected type %"PRIu32
, upcall
->type
);
1855 /* Flow expiration. */
1857 static int facet_max_idle(const struct ofproto_dpif
*);
1858 static void update_stats(struct ofproto_dpif
*);
1859 static void rule_expire(struct rule_dpif
*);
1860 static void expire_facets(struct ofproto_dpif
*, int dp_max_idle
);
1862 /* This function is called periodically by run(). Its job is to collect
1863 * updates for the flows that have been installed into the datapath, most
1864 * importantly when they last were used, and then use that information to
1865 * expire flows that have not been used recently.
1867 * Returns the number of milliseconds after which it should be called again. */
1869 expire(struct ofproto_dpif
*ofproto
)
1871 struct rule_dpif
*rule
, *next_rule
;
1872 struct classifier
*table
;
1875 /* Update stats for each flow in the datapath. */
1876 update_stats(ofproto
);
1878 /* Expire facets that have been idle too long. */
1879 dp_max_idle
= facet_max_idle(ofproto
);
1880 expire_facets(ofproto
, dp_max_idle
);
1882 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1883 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1884 struct cls_cursor cursor
;
1886 cls_cursor_init(&cursor
, table
, NULL
);
1887 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, up
.cr
, &cursor
) {
1892 /* All outstanding data in existing flows has been accounted, so it's a
1893 * good time to do bond rebalancing. */
1894 if (ofproto
->has_bonded_bundles
) {
1895 struct ofbundle
*bundle
;
1897 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1899 bond_rebalance(bundle
->bond
, &ofproto
->revalidate_set
);
1904 return MIN(dp_max_idle
, 1000);
1907 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1909 * This function also pushes statistics updates to rules which each facet
1910 * resubmits into. Generally these statistics will be accurate. However, if a
1911 * facet changes the rule it resubmits into at some time in between
1912 * update_stats() runs, it is possible that statistics accrued to the
1913 * old rule will be incorrectly attributed to the new rule. This could be
1914 * avoided by calling update_stats() whenever rules are created or
1915 * deleted. However, the performance impact of making so many calls to the
1916 * datapath do not justify the benefit of having perfectly accurate statistics.
1919 update_stats(struct ofproto_dpif
*p
)
1921 const struct dpif_flow_stats
*stats
;
1922 struct dpif_flow_dump dump
;
1923 const struct nlattr
*key
;
1926 dpif_flow_dump_start(&dump
, p
->dpif
);
1927 while (dpif_flow_dump_next(&dump
, &key
, &key_len
, NULL
, NULL
, &stats
)) {
1928 struct facet
*facet
;
1931 if (odp_flow_key_to_flow(key
, key_len
, &flow
)) {
1935 odp_flow_key_format(key
, key_len
, &s
);
1936 VLOG_WARN_RL(&rl
, "failed to convert datapath flow key to flow: %s",
1942 facet
= facet_find(p
, &flow
);
1944 if (facet
&& facet
->installed
) {
1946 if (stats
->n_packets
>= facet
->dp_packet_count
) {
1947 uint64_t extra
= stats
->n_packets
- facet
->dp_packet_count
;
1948 facet
->packet_count
+= extra
;
1950 VLOG_WARN_RL(&rl
, "unexpected packet count from the datapath");
1953 if (stats
->n_bytes
>= facet
->dp_byte_count
) {
1954 facet
->byte_count
+= stats
->n_bytes
- facet
->dp_byte_count
;
1956 VLOG_WARN_RL(&rl
, "unexpected byte count from datapath");
1959 facet
->dp_packet_count
= stats
->n_packets
;
1960 facet
->dp_byte_count
= stats
->n_bytes
;
1962 facet_update_time(p
, facet
, stats
->used
);
1963 facet_account(p
, facet
);
1964 facet_push_stats(facet
);
1966 /* There's a flow in the datapath that we know nothing about.
1968 COVERAGE_INC(facet_unexpected
);
1969 dpif_flow_del(p
->dpif
, key
, key_len
, NULL
);
1972 dpif_flow_dump_done(&dump
);
1975 /* Calculates and returns the number of milliseconds of idle time after which
1976 * facets should expire from the datapath and we should fold their statistics
1977 * into their parent rules in userspace. */
1979 facet_max_idle(const struct ofproto_dpif
*ofproto
)
1982 * Idle time histogram.
1984 * Most of the time a switch has a relatively small number of facets. When
1985 * this is the case we might as well keep statistics for all of them in
1986 * userspace and to cache them in the kernel datapath for performance as
1989 * As the number of facets increases, the memory required to maintain
1990 * statistics about them in userspace and in the kernel becomes
1991 * significant. However, with a large number of facets it is likely that
1992 * only a few of them are "heavy hitters" that consume a large amount of
1993 * bandwidth. At this point, only heavy hitters are worth caching in the
1994 * kernel and maintaining in userspaces; other facets we can discard.
1996 * The technique used to compute the idle time is to build a histogram with
1997 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1998 * that is installed in the kernel gets dropped in the appropriate bucket.
1999 * After the histogram has been built, we compute the cutoff so that only
2000 * the most-recently-used 1% of facets (but at least
2001 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2002 * the most-recently-used bucket of facets is kept, so actually an
2003 * arbitrary number of facets can be kept in any given expiration run
2004 * (though the next run will delete most of those unless they receive
2007 * This requires a second pass through the facets, in addition to the pass
2008 * made by update_stats(), because the former function never looks
2009 * at uninstallable facets.
2011 enum { BUCKET_WIDTH
= ROUND_UP(100, TIME_UPDATE_INTERVAL
) };
2012 enum { N_BUCKETS
= 5000 / BUCKET_WIDTH
};
2013 int buckets
[N_BUCKETS
] = { 0 };
2014 int total
, subtotal
, bucket
;
2015 struct facet
*facet
;
2019 total
= hmap_count(&ofproto
->facets
);
2020 if (total
<= ofproto
->up
.flow_eviction_threshold
) {
2021 return N_BUCKETS
* BUCKET_WIDTH
;
2024 /* Build histogram. */
2026 HMAP_FOR_EACH (facet
, hmap_node
, &ofproto
->facets
) {
2027 long long int idle
= now
- facet
->used
;
2028 int bucket
= (idle
<= 0 ? 0
2029 : idle
>= BUCKET_WIDTH
* N_BUCKETS
? N_BUCKETS
- 1
2030 : (unsigned int) idle
/ BUCKET_WIDTH
);
2034 /* Find the first bucket whose flows should be expired. */
2035 subtotal
= bucket
= 0;
2037 subtotal
+= buckets
[bucket
++];
2038 } while (bucket
< N_BUCKETS
&&
2039 subtotal
< MAX(ofproto
->up
.flow_eviction_threshold
, total
/ 100));
2041 if (VLOG_IS_DBG_ENABLED()) {
2045 ds_put_cstr(&s
, "keep");
2046 for (i
= 0; i
< N_BUCKETS
; i
++) {
2048 ds_put_cstr(&s
, ", drop");
2051 ds_put_format(&s
, " %d:%d", i
* BUCKET_WIDTH
, buckets
[i
]);
2054 VLOG_INFO("%s: %s (msec:count)", ofproto
->up
.name
, ds_cstr(&s
));
2058 return bucket
* BUCKET_WIDTH
;
2062 facet_active_timeout(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2064 if (ofproto
->netflow
&& !facet_is_controller_flow(facet
) &&
2065 netflow_active_timeout_expired(ofproto
->netflow
, &facet
->nf_flow
)) {
2066 struct ofexpired expired
;
2068 if (facet
->installed
) {
2069 struct dpif_flow_stats stats
;
2071 facet_put__(ofproto
, facet
, facet
->actions
, facet
->actions_len
,
2073 facet_update_stats(ofproto
, facet
, &stats
);
2076 expired
.flow
= facet
->flow
;
2077 expired
.packet_count
= facet
->packet_count
;
2078 expired
.byte_count
= facet
->byte_count
;
2079 expired
.used
= facet
->used
;
2080 netflow_expire(ofproto
->netflow
, &facet
->nf_flow
, &expired
);
2085 expire_facets(struct ofproto_dpif
*ofproto
, int dp_max_idle
)
2087 long long int cutoff
= time_msec() - dp_max_idle
;
2088 struct facet
*facet
, *next_facet
;
2090 HMAP_FOR_EACH_SAFE (facet
, next_facet
, hmap_node
, &ofproto
->facets
) {
2091 facet_active_timeout(ofproto
, facet
);
2092 if (facet
->used
< cutoff
) {
2093 facet_remove(ofproto
, facet
);
2098 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2099 * then delete it entirely. */
2101 rule_expire(struct rule_dpif
*rule
)
2103 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2104 struct facet
*facet
, *next_facet
;
2108 /* Has 'rule' expired? */
2110 if (rule
->up
.hard_timeout
2111 && now
> rule
->up
.modified
+ rule
->up
.hard_timeout
* 1000) {
2112 reason
= OFPRR_HARD_TIMEOUT
;
2113 } else if (rule
->up
.idle_timeout
&& list_is_empty(&rule
->facets
)
2114 && now
> rule
->used
+ rule
->up
.idle_timeout
* 1000) {
2115 reason
= OFPRR_IDLE_TIMEOUT
;
2120 COVERAGE_INC(ofproto_dpif_expired
);
2122 /* Update stats. (This is a no-op if the rule expired due to an idle
2123 * timeout, because that only happens when the rule has no facets left.) */
2124 LIST_FOR_EACH_SAFE (facet
, next_facet
, list_node
, &rule
->facets
) {
2125 facet_remove(ofproto
, facet
);
2128 /* Get rid of the rule. */
2129 ofproto_rule_expire(&rule
->up
, reason
);
2134 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2135 * example 'packet' within that flow.
2137 * The caller must already have determined that no facet with an identical
2138 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2139 * the ofproto's classifier table. */
2140 static struct facet
*
2141 facet_create(struct rule_dpif
*rule
, const struct flow
*flow
,
2142 const struct ofpbuf
*packet
)
2144 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2145 struct facet
*facet
;
2147 facet
= xzalloc(sizeof *facet
);
2148 facet
->used
= time_msec();
2149 hmap_insert(&ofproto
->facets
, &facet
->hmap_node
, flow_hash(flow
, 0));
2150 list_push_back(&rule
->facets
, &facet
->list_node
);
2152 facet
->flow
= *flow
;
2153 netflow_flow_init(&facet
->nf_flow
);
2154 netflow_flow_update_time(ofproto
->netflow
, &facet
->nf_flow
, facet
->used
);
2156 facet_make_actions(ofproto
, facet
, packet
);
2162 facet_free(struct facet
*facet
)
2164 free(facet
->actions
);
2168 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2169 * 'packet', which arrived on 'in_port'.
2171 * Takes ownership of 'packet'. */
2173 execute_odp_actions(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
2174 const struct nlattr
*odp_actions
, size_t actions_len
,
2175 struct ofpbuf
*packet
)
2177 if (actions_len
== NLA_ALIGN(NLA_HDRLEN
+ sizeof(uint64_t))
2178 && odp_actions
->nla_type
== OVS_ACTION_ATTR_USERSPACE
) {
2179 /* As an optimization, avoid a round-trip from userspace to kernel to
2180 * userspace. This also avoids possibly filling up kernel packet
2181 * buffers along the way. */
2182 struct dpif_upcall upcall
;
2184 upcall
.type
= DPIF_UC_ACTION
;
2185 upcall
.packet
= packet
;
2188 upcall
.userdata
= nl_attr_get_u64(odp_actions
);
2189 upcall
.sample_pool
= 0;
2190 upcall
.actions
= NULL
;
2191 upcall
.actions_len
= 0;
2193 send_packet_in(ofproto
, &upcall
, flow
, false);
2197 struct odputil_keybuf keybuf
;
2201 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2202 odp_flow_key_from_flow(&key
, flow
);
2204 error
= dpif_execute(ofproto
->dpif
, key
.data
, key
.size
,
2205 odp_actions
, actions_len
, packet
);
2207 ofpbuf_delete(packet
);
2212 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2213 * statistics appropriately. 'packet' must have at least sizeof(struct
2214 * ofp_packet_in) bytes of headroom.
2216 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2217 * applying flow_extract() to 'packet' would yield the same flow as
2220 * 'facet' must have accurately composed datapath actions; that is, it must
2221 * not be in need of revalidation.
2223 * Takes ownership of 'packet'. */
2225 facet_execute(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2226 struct ofpbuf
*packet
)
2228 struct dpif_flow_stats stats
;
2230 assert(ofpbuf_headroom(packet
) >= sizeof(struct ofp_packet_in
));
2232 flow_extract_stats(&facet
->flow
, packet
, &stats
);
2233 stats
.used
= time_msec();
2234 if (execute_odp_actions(ofproto
, &facet
->flow
,
2235 facet
->actions
, facet
->actions_len
, packet
)) {
2236 facet_update_stats(ofproto
, facet
, &stats
);
2240 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2242 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2243 * rule's statistics, via facet_uninstall().
2245 * - Removes 'facet' from its rule and from ofproto->facets.
2248 facet_remove(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2250 facet_uninstall(ofproto
, facet
);
2251 facet_flush_stats(ofproto
, facet
);
2252 hmap_remove(&ofproto
->facets
, &facet
->hmap_node
);
2253 list_remove(&facet
->list_node
);
2257 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2259 facet_make_actions(struct ofproto_dpif
*p
, struct facet
*facet
,
2260 const struct ofpbuf
*packet
)
2262 const struct rule_dpif
*rule
= facet
->rule
;
2263 struct ofpbuf
*odp_actions
;
2264 struct action_xlate_ctx ctx
;
2266 action_xlate_ctx_init(&ctx
, p
, &facet
->flow
, packet
);
2267 odp_actions
= xlate_actions(&ctx
, rule
->up
.actions
, rule
->up
.n_actions
);
2268 facet
->tags
= ctx
.tags
;
2269 facet
->may_install
= ctx
.may_set_up_flow
;
2270 facet
->has_learn
= ctx
.has_learn
;
2271 facet
->has_normal
= ctx
.has_normal
;
2272 facet
->nf_flow
.output_iface
= ctx
.nf_output_iface
;
2274 if (facet
->actions_len
!= odp_actions
->size
2275 || memcmp(facet
->actions
, odp_actions
->data
, odp_actions
->size
)) {
2276 free(facet
->actions
);
2277 facet
->actions_len
= odp_actions
->size
;
2278 facet
->actions
= xmemdup(odp_actions
->data
, odp_actions
->size
);
2281 ofpbuf_delete(odp_actions
);
2284 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2285 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2286 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2287 * since 'facet' was last updated.
2289 * Returns 0 if successful, otherwise a positive errno value.*/
2291 facet_put__(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2292 const struct nlattr
*actions
, size_t actions_len
,
2293 struct dpif_flow_stats
*stats
)
2295 struct odputil_keybuf keybuf
;
2296 enum dpif_flow_put_flags flags
;
2300 flags
= DPIF_FP_CREATE
| DPIF_FP_MODIFY
;
2302 flags
|= DPIF_FP_ZERO_STATS
;
2305 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2306 odp_flow_key_from_flow(&key
, &facet
->flow
);
2308 ret
= dpif_flow_put(ofproto
->dpif
, flags
, key
.data
, key
.size
,
2309 actions
, actions_len
, stats
);
2312 facet_reset_dp_stats(facet
, stats
);
2318 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2319 * 'zero_stats' is true, clears any existing statistics from the datapath for
2322 facet_install(struct ofproto_dpif
*p
, struct facet
*facet
, bool zero_stats
)
2324 struct dpif_flow_stats stats
;
2326 if (facet
->may_install
2327 && !facet_put__(p
, facet
, facet
->actions
, facet
->actions_len
,
2328 zero_stats
? &stats
: NULL
)) {
2329 facet
->installed
= true;
2334 facet_account(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2337 const struct nlattr
*a
;
2341 if (facet
->byte_count
<= facet
->accounted_bytes
) {
2344 n_bytes
= facet
->byte_count
- facet
->accounted_bytes
;
2345 facet
->accounted_bytes
= facet
->byte_count
;
2347 /* Feed information from the active flows back into the learning table to
2348 * ensure that table is always in sync with what is actually flowing
2349 * through the datapath. */
2350 if (facet
->has_learn
|| facet
->has_normal
) {
2351 struct action_xlate_ctx ctx
;
2353 action_xlate_ctx_init(&ctx
, ofproto
, &facet
->flow
, NULL
);
2354 ctx
.may_learn
= true;
2355 ofpbuf_delete(xlate_actions(&ctx
, facet
->rule
->up
.actions
,
2356 facet
->rule
->up
.n_actions
));
2359 if (!facet
->has_normal
|| !ofproto
->has_bonded_bundles
) {
2363 /* This loop feeds byte counters to bond_account() for rebalancing to use
2364 * as a basis. We also need to track the actual VLAN on which the packet
2365 * is going to be sent to ensure that it matches the one passed to
2366 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2368 vlan_tci
= facet
->flow
.vlan_tci
;
2369 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, facet
->actions
, facet
->actions_len
) {
2370 struct ofport_dpif
*port
;
2372 switch (nl_attr_type(a
)) {
2373 case OVS_ACTION_ATTR_OUTPUT
:
2374 port
= get_odp_port(ofproto
, nl_attr_get_u32(a
));
2375 if (port
&& port
->bundle
&& port
->bundle
->bond
) {
2376 bond_account(port
->bundle
->bond
, &facet
->flow
,
2377 vlan_tci_to_vid(vlan_tci
), n_bytes
);
2381 case OVS_ACTION_ATTR_POP_VLAN
:
2382 vlan_tci
= htons(0);
2385 case OVS_ACTION_ATTR_PUSH_VLAN
:
2386 vlan_tci
= nl_attr_get_be16(a
);
2392 /* If 'rule' is installed in the datapath, uninstalls it. */
2394 facet_uninstall(struct ofproto_dpif
*p
, struct facet
*facet
)
2396 if (facet
->installed
) {
2397 struct odputil_keybuf keybuf
;
2398 struct dpif_flow_stats stats
;
2402 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2403 odp_flow_key_from_flow(&key
, &facet
->flow
);
2405 error
= dpif_flow_del(p
->dpif
, key
.data
, key
.size
, &stats
);
2406 facet_reset_dp_stats(facet
, &stats
);
2408 facet_update_stats(p
, facet
, &stats
);
2410 facet
->installed
= false;
2412 assert(facet
->dp_packet_count
== 0);
2413 assert(facet
->dp_byte_count
== 0);
2417 /* Returns true if the only action for 'facet' is to send to the controller.
2418 * (We don't report NetFlow expiration messages for such facets because they
2419 * are just part of the control logic for the network, not real traffic). */
2421 facet_is_controller_flow(struct facet
*facet
)
2424 && facet
->rule
->up
.n_actions
== 1
2425 && action_outputs_to_port(&facet
->rule
->up
.actions
[0],
2426 htons(OFPP_CONTROLLER
)));
2429 /* Resets 'facet''s datapath statistics counters. This should be called when
2430 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2431 * it should contain the statistics returned by dpif when 'facet' was reset in
2432 * the datapath. 'stats' will be modified to only included statistics new
2433 * since 'facet' was last updated. */
2435 facet_reset_dp_stats(struct facet
*facet
, struct dpif_flow_stats
*stats
)
2437 if (stats
&& facet
->dp_packet_count
<= stats
->n_packets
2438 && facet
->dp_byte_count
<= stats
->n_bytes
) {
2439 stats
->n_packets
-= facet
->dp_packet_count
;
2440 stats
->n_bytes
-= facet
->dp_byte_count
;
2443 facet
->dp_packet_count
= 0;
2444 facet
->dp_byte_count
= 0;
2447 /* Folds all of 'facet''s statistics into its rule. Also updates the
2448 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2449 * 'facet''s statistics in the datapath should have been zeroed and folded into
2450 * its packet and byte counts before this function is called. */
2452 facet_flush_stats(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2454 assert(!facet
->dp_byte_count
);
2455 assert(!facet
->dp_packet_count
);
2457 facet_push_stats(facet
);
2458 facet_account(ofproto
, facet
);
2460 if (ofproto
->netflow
&& !facet_is_controller_flow(facet
)) {
2461 struct ofexpired expired
;
2462 expired
.flow
= facet
->flow
;
2463 expired
.packet_count
= facet
->packet_count
;
2464 expired
.byte_count
= facet
->byte_count
;
2465 expired
.used
= facet
->used
;
2466 netflow_expire(ofproto
->netflow
, &facet
->nf_flow
, &expired
);
2469 facet
->rule
->packet_count
+= facet
->packet_count
;
2470 facet
->rule
->byte_count
+= facet
->byte_count
;
2472 /* Reset counters to prevent double counting if 'facet' ever gets
2474 facet_reset_counters(facet
);
2476 netflow_flow_clear(&facet
->nf_flow
);
2479 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2480 * Returns it if found, otherwise a null pointer.
2482 * The returned facet might need revalidation; use facet_lookup_valid()
2483 * instead if that is important. */
2484 static struct facet
*
2485 facet_find(struct ofproto_dpif
*ofproto
, const struct flow
*flow
)
2487 struct facet
*facet
;
2489 HMAP_FOR_EACH_WITH_HASH (facet
, hmap_node
, flow_hash(flow
, 0),
2491 if (flow_equal(flow
, &facet
->flow
)) {
2499 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2500 * Returns it if found, otherwise a null pointer.
2502 * The returned facet is guaranteed to be valid. */
2503 static struct facet
*
2504 facet_lookup_valid(struct ofproto_dpif
*ofproto
, const struct flow
*flow
)
2506 struct facet
*facet
= facet_find(ofproto
, flow
);
2508 /* The facet we found might not be valid, since we could be in need of
2509 * revalidation. If it is not valid, don't return it. */
2511 && ofproto
->need_revalidate
2512 && !facet_revalidate(ofproto
, facet
)) {
2513 COVERAGE_INC(facet_invalidated
);
2520 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2522 * - If the rule found is different from 'facet''s current rule, moves
2523 * 'facet' to the new rule and recompiles its actions.
2525 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2526 * where it is and recompiles its actions anyway.
2528 * - If there is none, destroys 'facet'.
2530 * Returns true if 'facet' still exists, false if it has been destroyed. */
2532 facet_revalidate(struct ofproto_dpif
*ofproto
, struct facet
*facet
)
2534 struct action_xlate_ctx ctx
;
2535 struct ofpbuf
*odp_actions
;
2536 struct rule_dpif
*new_rule
;
2537 bool actions_changed
;
2539 COVERAGE_INC(facet_revalidate
);
2541 /* Determine the new rule. */
2542 new_rule
= rule_dpif_lookup(ofproto
, &facet
->flow
, 0);
2544 /* No new rule, so delete the facet. */
2545 facet_remove(ofproto
, facet
);
2549 /* Calculate new datapath actions.
2551 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2552 * emit a NetFlow expiration and, if so, we need to have the old state
2553 * around to properly compose it. */
2554 action_xlate_ctx_init(&ctx
, ofproto
, &facet
->flow
, NULL
);
2555 odp_actions
= xlate_actions(&ctx
,
2556 new_rule
->up
.actions
, new_rule
->up
.n_actions
);
2557 actions_changed
= (facet
->actions_len
!= odp_actions
->size
2558 || memcmp(facet
->actions
, odp_actions
->data
,
2559 facet
->actions_len
));
2561 /* If the datapath actions changed or the installability changed,
2562 * then we need to talk to the datapath. */
2563 if (actions_changed
|| ctx
.may_set_up_flow
!= facet
->installed
) {
2564 if (ctx
.may_set_up_flow
) {
2565 struct dpif_flow_stats stats
;
2567 facet_put__(ofproto
, facet
,
2568 odp_actions
->data
, odp_actions
->size
, &stats
);
2569 facet_update_stats(ofproto
, facet
, &stats
);
2571 facet_uninstall(ofproto
, facet
);
2574 /* The datapath flow is gone or has zeroed stats, so push stats out of
2575 * 'facet' into 'rule'. */
2576 facet_flush_stats(ofproto
, facet
);
2579 /* Update 'facet' now that we've taken care of all the old state. */
2580 facet
->tags
= ctx
.tags
;
2581 facet
->nf_flow
.output_iface
= ctx
.nf_output_iface
;
2582 facet
->may_install
= ctx
.may_set_up_flow
;
2583 facet
->has_learn
= ctx
.has_learn
;
2584 facet
->has_normal
= ctx
.has_normal
;
2585 if (actions_changed
) {
2586 free(facet
->actions
);
2587 facet
->actions_len
= odp_actions
->size
;
2588 facet
->actions
= xmemdup(odp_actions
->data
, odp_actions
->size
);
2590 if (facet
->rule
!= new_rule
) {
2591 COVERAGE_INC(facet_changed_rule
);
2592 list_remove(&facet
->list_node
);
2593 list_push_back(&new_rule
->facets
, &facet
->list_node
);
2594 facet
->rule
= new_rule
;
2595 facet
->used
= new_rule
->up
.created
;
2596 facet
->rs_used
= facet
->used
;
2599 ofpbuf_delete(odp_actions
);
2604 /* Updates 'facet''s used time. Caller is responsible for calling
2605 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2607 facet_update_time(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2610 if (used
> facet
->used
) {
2612 if (used
> facet
->rule
->used
) {
2613 facet
->rule
->used
= used
;
2615 netflow_flow_update_time(ofproto
->netflow
, &facet
->nf_flow
, used
);
2619 /* Folds the statistics from 'stats' into the counters in 'facet'.
2621 * Because of the meaning of a facet's counters, it only makes sense to do this
2622 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2623 * packet that was sent by hand or if it represents statistics that have been
2624 * cleared out of the datapath. */
2626 facet_update_stats(struct ofproto_dpif
*ofproto
, struct facet
*facet
,
2627 const struct dpif_flow_stats
*stats
)
2629 if (stats
->n_packets
|| stats
->used
> facet
->used
) {
2630 facet_update_time(ofproto
, facet
, stats
->used
);
2631 facet
->packet_count
+= stats
->n_packets
;
2632 facet
->byte_count
+= stats
->n_bytes
;
2633 facet_push_stats(facet
);
2634 netflow_flow_update_flags(&facet
->nf_flow
, stats
->tcp_flags
);
2639 facet_reset_counters(struct facet
*facet
)
2641 facet
->packet_count
= 0;
2642 facet
->byte_count
= 0;
2643 facet
->rs_packet_count
= 0;
2644 facet
->rs_byte_count
= 0;
2645 facet
->accounted_bytes
= 0;
2649 facet_push_stats(struct facet
*facet
)
2651 uint64_t rs_packets
, rs_bytes
;
2653 assert(facet
->packet_count
>= facet
->rs_packet_count
);
2654 assert(facet
->byte_count
>= facet
->rs_byte_count
);
2655 assert(facet
->used
>= facet
->rs_used
);
2657 rs_packets
= facet
->packet_count
- facet
->rs_packet_count
;
2658 rs_bytes
= facet
->byte_count
- facet
->rs_byte_count
;
2660 if (rs_packets
|| rs_bytes
|| facet
->used
> facet
->rs_used
) {
2661 facet
->rs_packet_count
= facet
->packet_count
;
2662 facet
->rs_byte_count
= facet
->byte_count
;
2663 facet
->rs_used
= facet
->used
;
2665 flow_push_stats(facet
->rule
, &facet
->flow
,
2666 rs_packets
, rs_bytes
, facet
->used
);
2670 struct ofproto_push
{
2671 struct action_xlate_ctx ctx
;
2678 push_resubmit(struct action_xlate_ctx
*ctx
, struct rule_dpif
*rule
)
2680 struct ofproto_push
*push
= CONTAINER_OF(ctx
, struct ofproto_push
, ctx
);
2683 rule
->packet_count
+= push
->packets
;
2684 rule
->byte_count
+= push
->bytes
;
2685 rule
->used
= MAX(push
->used
, rule
->used
);
2689 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2690 * 'rule''s actions. */
2692 flow_push_stats(const struct rule_dpif
*rule
,
2693 struct flow
*flow
, uint64_t packets
, uint64_t bytes
,
2696 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2697 struct ofproto_push push
;
2699 push
.packets
= packets
;
2703 action_xlate_ctx_init(&push
.ctx
, ofproto
, flow
, NULL
);
2704 push
.ctx
.resubmit_hook
= push_resubmit
;
2705 ofpbuf_delete(xlate_actions(&push
.ctx
,
2706 rule
->up
.actions
, rule
->up
.n_actions
));
2711 static struct rule_dpif
*
2712 rule_dpif_lookup(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
2715 if (table_id
>= N_TABLES
) {
2719 return rule_dpif_cast(rule_from_cls_rule(
2720 classifier_lookup(&ofproto
->up
.tables
[table_id
],
2725 complete_operation(struct rule_dpif
*rule
)
2727 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2729 rule_invalidate(rule
);
2731 struct dpif_completion
*c
= xmalloc(sizeof *c
);
2732 c
->op
= rule
->up
.pending
;
2733 list_push_back(&ofproto
->completions
, &c
->list_node
);
2735 ofoperation_complete(rule
->up
.pending
, 0);
2739 static struct rule
*
2742 struct rule_dpif
*rule
= xmalloc(sizeof *rule
);
2747 rule_dealloc(struct rule
*rule_
)
2749 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2754 rule_construct(struct rule
*rule_
)
2756 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2757 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2758 struct rule_dpif
*victim
;
2762 error
= validate_actions(rule
->up
.actions
, rule
->up
.n_actions
,
2763 &rule
->up
.cr
.flow
, ofproto
->max_ports
);
2768 rule
->used
= rule
->up
.created
;
2769 rule
->packet_count
= 0;
2770 rule
->byte_count
= 0;
2772 victim
= rule_dpif_cast(ofoperation_get_victim(rule
->up
.pending
));
2773 if (victim
&& !list_is_empty(&victim
->facets
)) {
2774 struct facet
*facet
;
2776 rule
->facets
= victim
->facets
;
2777 list_moved(&rule
->facets
);
2778 LIST_FOR_EACH (facet
, list_node
, &rule
->facets
) {
2779 /* XXX: We're only clearing our local counters here. It's possible
2780 * that quite a few packets are unaccounted for in the datapath
2781 * statistics. These will be accounted to the new rule instead of
2782 * cleared as required. This could be fixed by clearing out the
2783 * datapath statistics for this facet, but currently it doesn't
2785 facet_reset_counters(facet
);
2789 /* Must avoid list_moved() in this case. */
2790 list_init(&rule
->facets
);
2793 table_id
= rule
->up
.table_id
;
2794 rule
->tag
= (victim
? victim
->tag
2796 : rule_calculate_tag(&rule
->up
.cr
.flow
, &rule
->up
.cr
.wc
,
2797 ofproto
->tables
[table_id
].basis
));
2799 complete_operation(rule
);
2804 rule_destruct(struct rule
*rule_
)
2806 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2807 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2808 struct facet
*facet
, *next_facet
;
2810 LIST_FOR_EACH_SAFE (facet
, next_facet
, list_node
, &rule
->facets
) {
2811 facet_revalidate(ofproto
, facet
);
2814 complete_operation(rule
);
2818 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
)
2820 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2821 struct facet
*facet
;
2823 /* Start from historical data for 'rule' itself that are no longer tracked
2824 * in facets. This counts, for example, facets that have expired. */
2825 *packets
= rule
->packet_count
;
2826 *bytes
= rule
->byte_count
;
2828 /* Add any statistics that are tracked by facets. This includes
2829 * statistical data recently updated by ofproto_update_stats() as well as
2830 * stats for packets that were executed "by hand" via dpif_execute(). */
2831 LIST_FOR_EACH (facet
, list_node
, &rule
->facets
) {
2832 *packets
+= facet
->packet_count
;
2833 *bytes
+= facet
->byte_count
;
2838 rule_execute(struct rule
*rule_
, struct flow
*flow
, struct ofpbuf
*packet
)
2840 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2841 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2842 struct action_xlate_ctx ctx
;
2843 struct ofpbuf
*odp_actions
;
2844 struct facet
*facet
;
2847 /* First look for a related facet. If we find one, account it to that. */
2848 facet
= facet_lookup_valid(ofproto
, flow
);
2849 if (facet
&& facet
->rule
== rule
) {
2850 facet_execute(ofproto
, facet
, packet
);
2854 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2855 * create a new facet for it and use that. */
2856 if (rule_dpif_lookup(ofproto
, flow
, 0) == rule
) {
2857 facet
= facet_create(rule
, flow
, packet
);
2858 facet_execute(ofproto
, facet
, packet
);
2859 facet_install(ofproto
, facet
, true);
2863 /* We can't account anything to a facet. If we were to try, then that
2864 * facet would have a non-matching rule, busting our invariants. */
2865 action_xlate_ctx_init(&ctx
, ofproto
, flow
, packet
);
2866 odp_actions
= xlate_actions(&ctx
, rule
->up
.actions
, rule
->up
.n_actions
);
2867 size
= packet
->size
;
2868 if (execute_odp_actions(ofproto
, flow
, odp_actions
->data
,
2869 odp_actions
->size
, packet
)) {
2870 rule
->used
= time_msec();
2871 rule
->packet_count
++;
2872 rule
->byte_count
+= size
;
2873 flow_push_stats(rule
, flow
, 1, size
, rule
->used
);
2875 ofpbuf_delete(odp_actions
);
2881 rule_modify_actions(struct rule
*rule_
)
2883 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
2884 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
2887 error
= validate_actions(rule
->up
.actions
, rule
->up
.n_actions
,
2888 &rule
->up
.cr
.flow
, ofproto
->max_ports
);
2890 ofoperation_complete(rule
->up
.pending
, error
);
2894 complete_operation(rule
);
2897 /* Sends 'packet' out of port 'odp_port' within 'p'.
2898 * Returns 0 if successful, otherwise a positive errno value. */
2900 send_packet(struct ofproto_dpif
*ofproto
, uint32_t odp_port
,
2901 const struct ofpbuf
*packet
)
2903 struct ofpbuf key
, odp_actions
;
2904 struct odputil_keybuf keybuf
;
2908 flow_extract((struct ofpbuf
*) packet
, 0, 0, &flow
);
2909 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
2910 odp_flow_key_from_flow(&key
, &flow
);
2912 ofpbuf_init(&odp_actions
, 32);
2913 nl_msg_put_u32(&odp_actions
, OVS_ACTION_ATTR_OUTPUT
, odp_port
);
2914 error
= dpif_execute(ofproto
->dpif
,
2916 odp_actions
.data
, odp_actions
.size
,
2918 ofpbuf_uninit(&odp_actions
);
2921 VLOG_WARN_RL(&rl
, "%s: failed to send packet on port %"PRIu32
" (%s)",
2922 ofproto
->up
.name
, odp_port
, strerror(error
));
2927 /* OpenFlow to datapath action translation. */
2929 static void do_xlate_actions(const union ofp_action
*in
, size_t n_in
,
2930 struct action_xlate_ctx
*ctx
);
2931 static void xlate_normal(struct action_xlate_ctx
*);
2934 commit_vlan_tci(struct action_xlate_ctx
*ctx
, ovs_be16 vlan_tci
)
2936 struct flow
*base
= &ctx
->base_flow
;
2937 struct ofpbuf
*odp_actions
= ctx
->odp_actions
;
2939 if (base
->vlan_tci
!= vlan_tci
) {
2940 if (!(vlan_tci
& htons(VLAN_CFI
))) {
2941 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2943 if (base
->vlan_tci
!= htons(0)) {
2944 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2946 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2947 vlan_tci
& ~htons(VLAN_CFI
));
2949 base
->vlan_tci
= vlan_tci
;
2954 commit_odp_actions(struct action_xlate_ctx
*ctx
)
2956 const struct flow
*flow
= &ctx
->flow
;
2957 struct flow
*base
= &ctx
->base_flow
;
2958 struct ofpbuf
*odp_actions
= ctx
->odp_actions
;
2960 if (base
->tun_id
!= flow
->tun_id
) {
2961 nl_msg_put_be64(odp_actions
, OVS_ACTION_ATTR_SET_TUNNEL
, flow
->tun_id
);
2962 base
->tun_id
= flow
->tun_id
;
2965 if (base
->nw_src
!= flow
->nw_src
) {
2966 nl_msg_put_be32(odp_actions
, OVS_ACTION_ATTR_SET_NW_SRC
, flow
->nw_src
);
2967 base
->nw_src
= flow
->nw_src
;
2970 if (base
->nw_dst
!= flow
->nw_dst
) {
2971 nl_msg_put_be32(odp_actions
, OVS_ACTION_ATTR_SET_NW_DST
, flow
->nw_dst
);
2972 base
->nw_dst
= flow
->nw_dst
;
2975 if (base
->nw_tos
!= flow
->nw_tos
) {
2976 nl_msg_put_u8(odp_actions
, OVS_ACTION_ATTR_SET_NW_TOS
, flow
->nw_tos
);
2977 base
->nw_tos
= flow
->nw_tos
;
2980 commit_vlan_tci(ctx
, flow
->vlan_tci
);
2982 if (base
->tp_src
!= flow
->tp_src
) {
2983 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_SET_TP_SRC
, flow
->tp_src
);
2984 base
->tp_src
= flow
->tp_src
;
2987 if (base
->tp_dst
!= flow
->tp_dst
) {
2988 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_SET_TP_DST
, flow
->tp_dst
);
2989 base
->tp_dst
= flow
->tp_dst
;
2992 if (!eth_addr_equals(base
->dl_src
, flow
->dl_src
)) {
2993 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_SET_DL_SRC
,
2994 flow
->dl_src
, ETH_ADDR_LEN
);
2995 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2998 if (!eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2999 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_SET_DL_DST
,
3000 flow
->dl_dst
, ETH_ADDR_LEN
);
3001 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
3004 if (ctx
->base_priority
!= ctx
->priority
) {
3005 if (ctx
->priority
) {
3006 nl_msg_put_u32(odp_actions
, OVS_ACTION_ATTR_SET_PRIORITY
,
3009 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_PRIORITY
);
3011 ctx
->base_priority
= ctx
->priority
;
3016 add_output_action(struct action_xlate_ctx
*ctx
, uint16_t ofp_port
)
3018 const struct ofport_dpif
*ofport
= get_ofp_port(ctx
->ofproto
, ofp_port
);
3019 uint16_t odp_port
= ofp_port_to_odp_port(ofp_port
);
3022 if (ofport
->up
.opp
.config
& htonl(OFPPC_NO_FWD
)) {
3023 /* Forwarding disabled on port. */
3028 * We don't have an ofport record for this port, but it doesn't hurt to
3029 * allow forwarding to it anyhow. Maybe such a port will appear later
3030 * and we're pre-populating the flow table.
3034 commit_odp_actions(ctx
);
3035 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
, odp_port
);
3036 ctx
->nf_output_iface
= ofp_port
;
3040 xlate_table_action(struct action_xlate_ctx
*ctx
,
3041 uint16_t in_port
, uint8_t table_id
)
3043 if (ctx
->recurse
< MAX_RESUBMIT_RECURSION
) {
3044 struct ofproto_dpif
*ofproto
= ctx
->ofproto
;
3045 struct rule_dpif
*rule
;
3046 uint16_t old_in_port
;
3047 uint8_t old_table_id
;
3049 old_table_id
= ctx
->table_id
;
3050 ctx
->table_id
= table_id
;
3052 /* Look up a flow with 'in_port' as the input port. */
3053 old_in_port
= ctx
->flow
.in_port
;
3054 ctx
->flow
.in_port
= in_port
;
3055 rule
= rule_dpif_lookup(ofproto
, &ctx
->flow
, table_id
);
3058 if (table_id
> 0 && table_id
< N_TABLES
) {
3059 struct table_dpif
*table
= &ofproto
->tables
[table_id
];
3060 if (table
->other_table
) {
3063 : rule_calculate_tag(&ctx
->flow
,
3064 &table
->other_table
->wc
,
3069 /* Restore the original input port. Otherwise OFPP_NORMAL and
3070 * OFPP_IN_PORT will have surprising behavior. */
3071 ctx
->flow
.in_port
= old_in_port
;
3073 if (ctx
->resubmit_hook
) {
3074 ctx
->resubmit_hook(ctx
, rule
);
3079 do_xlate_actions(rule
->up
.actions
, rule
->up
.n_actions
, ctx
);
3083 ctx
->table_id
= old_table_id
;
3085 static struct vlog_rate_limit recurse_rl
= VLOG_RATE_LIMIT_INIT(1, 1);
3087 VLOG_ERR_RL(&recurse_rl
, "resubmit actions recursed over %d times",
3088 MAX_RESUBMIT_RECURSION
);
3093 xlate_resubmit_table(struct action_xlate_ctx
*ctx
,
3094 const struct nx_action_resubmit
*nar
)
3099 in_port
= (nar
->in_port
== htons(OFPP_IN_PORT
)
3101 : ntohs(nar
->in_port
));
3102 table_id
= nar
->table
== 255 ? ctx
->table_id
: nar
->table
;
3104 xlate_table_action(ctx
, in_port
, table_id
);
3108 flood_packets(struct action_xlate_ctx
*ctx
, ovs_be32 mask
)
3110 struct ofport_dpif
*ofport
;
3112 commit_odp_actions(ctx
);
3113 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ctx
->ofproto
->up
.ports
) {
3114 uint16_t ofp_port
= ofport
->up
.ofp_port
;
3115 if (ofp_port
!= ctx
->flow
.in_port
&& !(ofport
->up
.opp
.config
& mask
)) {
3116 nl_msg_put_u32(ctx
->odp_actions
, OVS_ACTION_ATTR_OUTPUT
,
3121 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3125 xlate_output_action__(struct action_xlate_ctx
*ctx
,
3126 uint16_t port
, uint16_t max_len
)
3128 uint16_t prev_nf_output_iface
= ctx
->nf_output_iface
;
3130 ctx
->nf_output_iface
= NF_OUT_DROP
;
3134 add_output_action(ctx
, ctx
->flow
.in_port
);
3137 xlate_table_action(ctx
, ctx
->flow
.in_port
, ctx
->table_id
);
3143 flood_packets(ctx
, htonl(OFPPC_NO_FLOOD
));
3146 flood_packets(ctx
, htonl(0));
3148 case OFPP_CONTROLLER
:
3149 commit_odp_actions(ctx
);
3150 nl_msg_put_u64(ctx
->odp_actions
, OVS_ACTION_ATTR_USERSPACE
, max_len
);
3153 add_output_action(ctx
, OFPP_LOCAL
);
3158 if (port
!= ctx
->flow
.in_port
) {
3159 add_output_action(ctx
, port
);
3164 if (prev_nf_output_iface
== NF_OUT_FLOOD
) {
3165 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3166 } else if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
3167 ctx
->nf_output_iface
= prev_nf_output_iface
;
3168 } else if (prev_nf_output_iface
!= NF_OUT_DROP
&&
3169 ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
3170 ctx
->nf_output_iface
= NF_OUT_MULTI
;
3175 xlate_output_reg_action(struct action_xlate_ctx
*ctx
,
3176 const struct nx_action_output_reg
*naor
)
3180 ofp_port
= nxm_read_field_bits(naor
->src
, naor
->ofs_nbits
, &ctx
->flow
);
3182 if (ofp_port
<= UINT16_MAX
) {
3183 xlate_output_action__(ctx
, ofp_port
, ntohs(naor
->max_len
));
3188 xlate_output_action(struct action_xlate_ctx
*ctx
,
3189 const struct ofp_action_output
*oao
)
3191 xlate_output_action__(ctx
, ntohs(oao
->port
), ntohs(oao
->max_len
));
3195 xlate_enqueue_action(struct action_xlate_ctx
*ctx
,
3196 const struct ofp_action_enqueue
*oae
)
3198 uint16_t ofp_port
, odp_port
;
3199 uint32_t ctx_priority
, priority
;
3202 error
= dpif_queue_to_priority(ctx
->ofproto
->dpif
, ntohl(oae
->queue_id
),
3205 /* Fall back to ordinary output action. */
3206 xlate_output_action__(ctx
, ntohs(oae
->port
), 0);
3210 /* Figure out datapath output port. */
3211 ofp_port
= ntohs(oae
->port
);
3212 if (ofp_port
== OFPP_IN_PORT
) {
3213 ofp_port
= ctx
->flow
.in_port
;
3215 odp_port
= ofp_port_to_odp_port(ofp_port
);
3217 /* Add datapath actions. */
3218 ctx_priority
= ctx
->priority
;
3219 ctx
->priority
= priority
;
3220 add_output_action(ctx
, odp_port
);
3221 ctx
->priority
= ctx_priority
;
3223 /* Update NetFlow output port. */
3224 if (ctx
->nf_output_iface
== NF_OUT_DROP
) {
3225 ctx
->nf_output_iface
= odp_port
;
3226 } else if (ctx
->nf_output_iface
!= NF_OUT_FLOOD
) {
3227 ctx
->nf_output_iface
= NF_OUT_MULTI
;
3232 xlate_set_queue_action(struct action_xlate_ctx
*ctx
,
3233 const struct nx_action_set_queue
*nasq
)
3238 error
= dpif_queue_to_priority(ctx
->ofproto
->dpif
, ntohl(nasq
->queue_id
),
3241 /* Couldn't translate queue to a priority, so ignore. A warning
3242 * has already been logged. */
3246 ctx
->priority
= priority
;
3249 struct xlate_reg_state
{
3255 xlate_autopath(struct action_xlate_ctx
*ctx
,
3256 const struct nx_action_autopath
*naa
)
3258 uint16_t ofp_port
= ntohl(naa
->id
);
3259 struct ofport_dpif
*port
= get_ofp_port(ctx
->ofproto
, ofp_port
);
3261 if (!port
|| !port
->bundle
) {
3262 ofp_port
= OFPP_NONE
;
3263 } else if (port
->bundle
->bond
) {
3264 /* Autopath does not support VLAN hashing. */
3265 struct ofport_dpif
*slave
= bond_choose_output_slave(
3266 port
->bundle
->bond
, &ctx
->flow
, 0, &ctx
->tags
);
3268 ofp_port
= slave
->up
.ofp_port
;
3271 autopath_execute(naa
, &ctx
->flow
, ofp_port
);
3275 slave_enabled_cb(uint16_t ofp_port
, void *ofproto_
)
3277 struct ofproto_dpif
*ofproto
= ofproto_
;
3278 struct ofport_dpif
*port
;
3288 case OFPP_CONTROLLER
: /* Not supported by the bundle action. */
3291 port
= get_ofp_port(ofproto
, ofp_port
);
3292 return port
? port
->may_enable
: false;
3297 xlate_learn_action(struct action_xlate_ctx
*ctx
,
3298 const struct nx_action_learn
*learn
)
3300 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
3301 struct ofputil_flow_mod fm
;
3304 learn_execute(learn
, &ctx
->flow
, &fm
);
3306 error
= ofproto_flow_mod(&ctx
->ofproto
->up
, &fm
);
3307 if (error
&& !VLOG_DROP_WARN(&rl
)) {
3308 char *msg
= ofputil_error_to_string(error
);
3309 VLOG_WARN("learning action failed to modify flow table (%s)", msg
);
3317 do_xlate_actions(const union ofp_action
*in
, size_t n_in
,
3318 struct action_xlate_ctx
*ctx
)
3320 const struct ofport_dpif
*port
;
3321 const union ofp_action
*ia
;
3324 port
= get_ofp_port(ctx
->ofproto
, ctx
->flow
.in_port
);
3326 && port
->up
.opp
.config
& htonl(OFPPC_NO_RECV
| OFPPC_NO_RECV_STP
) &&
3327 port
->up
.opp
.config
& (eth_addr_equals(ctx
->flow
.dl_dst
, eth_addr_stp
)
3328 ? htonl(OFPPC_NO_RECV_STP
)
3329 : htonl(OFPPC_NO_RECV
))) {
3330 /* Drop this flow. */
3334 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia
, left
, in
, n_in
) {
3335 const struct ofp_action_dl_addr
*oada
;
3336 const struct nx_action_resubmit
*nar
;
3337 const struct nx_action_set_tunnel
*nast
;
3338 const struct nx_action_set_queue
*nasq
;
3339 const struct nx_action_multipath
*nam
;
3340 const struct nx_action_autopath
*naa
;
3341 const struct nx_action_bundle
*nab
;
3342 const struct nx_action_output_reg
*naor
;
3343 enum ofputil_action_code code
;
3346 code
= ofputil_decode_action_unsafe(ia
);
3348 case OFPUTIL_OFPAT_OUTPUT
:
3349 xlate_output_action(ctx
, &ia
->output
);
3352 case OFPUTIL_OFPAT_SET_VLAN_VID
:
3353 ctx
->flow
.vlan_tci
&= ~htons(VLAN_VID_MASK
);
3354 ctx
->flow
.vlan_tci
|= ia
->vlan_vid
.vlan_vid
| htons(VLAN_CFI
);
3357 case OFPUTIL_OFPAT_SET_VLAN_PCP
:
3358 ctx
->flow
.vlan_tci
&= ~htons(VLAN_PCP_MASK
);
3359 ctx
->flow
.vlan_tci
|= htons(
3360 (ia
->vlan_pcp
.vlan_pcp
<< VLAN_PCP_SHIFT
) | VLAN_CFI
);
3363 case OFPUTIL_OFPAT_STRIP_VLAN
:
3364 ctx
->flow
.vlan_tci
= htons(0);
3367 case OFPUTIL_OFPAT_SET_DL_SRC
:
3368 oada
= ((struct ofp_action_dl_addr
*) ia
);
3369 memcpy(ctx
->flow
.dl_src
, oada
->dl_addr
, ETH_ADDR_LEN
);
3372 case OFPUTIL_OFPAT_SET_DL_DST
:
3373 oada
= ((struct ofp_action_dl_addr
*) ia
);
3374 memcpy(ctx
->flow
.dl_dst
, oada
->dl_addr
, ETH_ADDR_LEN
);
3377 case OFPUTIL_OFPAT_SET_NW_SRC
:
3378 ctx
->flow
.nw_src
= ia
->nw_addr
.nw_addr
;
3381 case OFPUTIL_OFPAT_SET_NW_DST
:
3382 ctx
->flow
.nw_dst
= ia
->nw_addr
.nw_addr
;
3385 case OFPUTIL_OFPAT_SET_NW_TOS
:
3386 ctx
->flow
.nw_tos
= ia
->nw_tos
.nw_tos
& IP_DSCP_MASK
;
3389 case OFPUTIL_OFPAT_SET_TP_SRC
:
3390 ctx
->flow
.tp_src
= ia
->tp_port
.tp_port
;
3393 case OFPUTIL_OFPAT_SET_TP_DST
:
3394 ctx
->flow
.tp_dst
= ia
->tp_port
.tp_port
;
3397 case OFPUTIL_OFPAT_ENQUEUE
:
3398 xlate_enqueue_action(ctx
, (const struct ofp_action_enqueue
*) ia
);
3401 case OFPUTIL_NXAST_RESUBMIT
:
3402 nar
= (const struct nx_action_resubmit
*) ia
;
3403 xlate_table_action(ctx
, ntohs(nar
->in_port
), ctx
->table_id
);
3406 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
3407 xlate_resubmit_table(ctx
, (const struct nx_action_resubmit
*) ia
);
3410 case OFPUTIL_NXAST_SET_TUNNEL
:
3411 nast
= (const struct nx_action_set_tunnel
*) ia
;
3412 tun_id
= htonll(ntohl(nast
->tun_id
));
3413 ctx
->flow
.tun_id
= tun_id
;
3416 case OFPUTIL_NXAST_SET_QUEUE
:
3417 nasq
= (const struct nx_action_set_queue
*) ia
;
3418 xlate_set_queue_action(ctx
, nasq
);
3421 case OFPUTIL_NXAST_POP_QUEUE
:
3425 case OFPUTIL_NXAST_REG_MOVE
:
3426 nxm_execute_reg_move((const struct nx_action_reg_move
*) ia
,
3430 case OFPUTIL_NXAST_REG_LOAD
:
3431 nxm_execute_reg_load((const struct nx_action_reg_load
*) ia
,
3435 case OFPUTIL_NXAST_NOTE
:
3436 /* Nothing to do. */
3439 case OFPUTIL_NXAST_SET_TUNNEL64
:
3440 tun_id
= ((const struct nx_action_set_tunnel64
*) ia
)->tun_id
;
3441 ctx
->flow
.tun_id
= tun_id
;
3444 case OFPUTIL_NXAST_MULTIPATH
:
3445 nam
= (const struct nx_action_multipath
*) ia
;
3446 multipath_execute(nam
, &ctx
->flow
);
3449 case OFPUTIL_NXAST_AUTOPATH
:
3450 naa
= (const struct nx_action_autopath
*) ia
;
3451 xlate_autopath(ctx
, naa
);
3454 case OFPUTIL_NXAST_BUNDLE
:
3455 ctx
->ofproto
->has_bundle_action
= true;
3456 nab
= (const struct nx_action_bundle
*) ia
;
3457 xlate_output_action__(ctx
, bundle_execute(nab
, &ctx
->flow
,
3462 case OFPUTIL_NXAST_BUNDLE_LOAD
:
3463 ctx
->ofproto
->has_bundle_action
= true;
3464 nab
= (const struct nx_action_bundle
*) ia
;
3465 bundle_execute_load(nab
, &ctx
->flow
, slave_enabled_cb
,
3469 case OFPUTIL_NXAST_OUTPUT_REG
:
3470 naor
= (const struct nx_action_output_reg
*) ia
;
3471 xlate_output_reg_action(ctx
, naor
);
3474 case OFPUTIL_NXAST_LEARN
:
3475 ctx
->has_learn
= true;
3476 if (ctx
->may_learn
) {
3477 xlate_learn_action(ctx
, (const struct nx_action_learn
*) ia
);
3485 action_xlate_ctx_init(struct action_xlate_ctx
*ctx
,
3486 struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
3487 const struct ofpbuf
*packet
)
3489 ctx
->ofproto
= ofproto
;
3491 ctx
->packet
= packet
;
3492 ctx
->may_learn
= packet
!= NULL
;
3493 ctx
->resubmit_hook
= NULL
;
3496 static struct ofpbuf
*
3497 xlate_actions(struct action_xlate_ctx
*ctx
,
3498 const union ofp_action
*in
, size_t n_in
)
3500 COVERAGE_INC(ofproto_dpif_xlate
);
3502 ctx
->odp_actions
= ofpbuf_new(512);
3504 ctx
->may_set_up_flow
= true;
3505 ctx
->has_learn
= false;
3506 ctx
->has_normal
= false;
3507 ctx
->nf_output_iface
= NF_OUT_DROP
;
3510 ctx
->base_priority
= 0;
3511 ctx
->base_flow
= ctx
->flow
;
3512 ctx
->base_flow
.tun_id
= 0;
3515 if (process_special(ctx
->ofproto
, &ctx
->flow
, ctx
->packet
)) {
3516 ctx
->may_set_up_flow
= false;
3518 do_xlate_actions(in
, n_in
, ctx
);
3521 /* Check with in-band control to see if we're allowed to set up this
3523 if (!connmgr_may_set_up_flow(ctx
->ofproto
->up
.connmgr
, &ctx
->flow
,
3524 ctx
->odp_actions
->data
,
3525 ctx
->odp_actions
->size
)) {
3526 ctx
->may_set_up_flow
= false;
3529 return ctx
->odp_actions
;
3532 /* OFPP_NORMAL implementation. */
3535 struct ofport_dpif
*port
;
3540 struct dst builtin
[32];
3542 size_t n
, allocated
;
3545 static void dst_set_init(struct dst_set
*);
3546 static void dst_set_add(struct dst_set
*, const struct dst
*);
3547 static void dst_set_free(struct dst_set
*);
3549 static struct ofport_dpif
*ofbundle_get_a_port(const struct ofbundle
*);
3551 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
3552 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
3553 * the bundle on which the packet was received, returns the VLAN to which the
3556 * Both 'vid' and the return value are in the range 0...4095. */
3558 input_vid_to_vlan(const struct ofbundle
*in_bundle
, uint16_t vid
)
3560 switch (in_bundle
->vlan_mode
) {
3561 case PORT_VLAN_ACCESS
:
3562 return in_bundle
->vlan
;
3565 case PORT_VLAN_TRUNK
:
3568 case PORT_VLAN_NATIVE_UNTAGGED
:
3569 case PORT_VLAN_NATIVE_TAGGED
:
3570 return vid
? vid
: in_bundle
->vlan
;
3577 /* Given 'vlan', the VLAN that a packet belongs to, and
3578 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
3579 * that should be included in the 802.1Q header. (If the return value is 0,
3580 * then the 802.1Q header should only be included in the packet if there is a
3583 * Both 'vlan' and the return value are in the range 0...4095. */
3585 output_vlan_to_vid(const struct ofbundle
*out_bundle
, uint16_t vlan
)
3587 switch (out_bundle
->vlan_mode
) {
3588 case PORT_VLAN_ACCESS
:
3591 case PORT_VLAN_TRUNK
:
3592 case PORT_VLAN_NATIVE_TAGGED
:
3595 case PORT_VLAN_NATIVE_UNTAGGED
:
3596 return vlan
== out_bundle
->vlan
? 0 : vlan
;
3604 set_dst(struct action_xlate_ctx
*ctx
, struct dst
*dst
,
3605 const struct ofbundle
*in_bundle
, const struct ofbundle
*out_bundle
)
3609 vlan
= input_vid_to_vlan(in_bundle
, vlan_tci_to_vid(ctx
->flow
.vlan_tci
));
3610 dst
->vid
= output_vlan_to_vid(out_bundle
, vlan
);
3612 dst
->port
= (!out_bundle
->bond
3613 ? ofbundle_get_a_port(out_bundle
)
3614 : bond_choose_output_slave(out_bundle
->bond
, &ctx
->flow
,
3615 dst
->vid
, &ctx
->tags
));
3616 return dst
->port
!= NULL
;
3620 mirror_mask_ffs(mirror_mask_t mask
)
3622 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask
));
3627 dst_set_init(struct dst_set
*set
)
3629 set
->dsts
= set
->builtin
;
3631 set
->allocated
= ARRAY_SIZE(set
->builtin
);
3635 dst_set_add(struct dst_set
*set
, const struct dst
*dst
)
3637 if (set
->n
>= set
->allocated
) {
3638 size_t new_allocated
;
3639 struct dst
*new_dsts
;
3641 new_allocated
= set
->allocated
* 2;
3642 new_dsts
= xmalloc(new_allocated
* sizeof *new_dsts
);
3643 memcpy(new_dsts
, set
->dsts
, set
->n
* sizeof *new_dsts
);
3647 set
->dsts
= new_dsts
;
3648 set
->allocated
= new_allocated
;
3650 set
->dsts
[set
->n
++] = *dst
;
3654 dst_set_free(struct dst_set
*set
)
3656 if (set
->dsts
!= set
->builtin
) {
3662 dst_is_duplicate(const struct dst_set
*set
, const struct dst
*test
)
3665 for (i
= 0; i
< set
->n
; i
++) {
3666 if (set
->dsts
[i
].vid
== test
->vid
3667 && set
->dsts
[i
].port
== test
->port
) {
3675 ofbundle_trunks_vlan(const struct ofbundle
*bundle
, uint16_t vlan
)
3677 return (bundle
->vlan_mode
!= PORT_VLAN_ACCESS
3678 && (!bundle
->trunks
|| bitmap_is_set(bundle
->trunks
, vlan
)));
3682 ofbundle_includes_vlan(const struct ofbundle
*bundle
, uint16_t vlan
)
3684 return vlan
== bundle
->vlan
|| ofbundle_trunks_vlan(bundle
, vlan
);
3687 /* Returns an arbitrary interface within 'bundle'. */
3688 static struct ofport_dpif
*
3689 ofbundle_get_a_port(const struct ofbundle
*bundle
)
3691 return CONTAINER_OF(list_front(&bundle
->ports
),
3692 struct ofport_dpif
, bundle_node
);
3696 compose_dsts(struct action_xlate_ctx
*ctx
, uint16_t vlan
,
3697 const struct ofbundle
*in_bundle
,
3698 const struct ofbundle
*out_bundle
, struct dst_set
*set
)
3702 if (out_bundle
== OFBUNDLE_FLOOD
) {
3703 struct ofbundle
*bundle
;
3705 HMAP_FOR_EACH (bundle
, hmap_node
, &ctx
->ofproto
->bundles
) {
3706 if (bundle
!= in_bundle
3707 && ofbundle_includes_vlan(bundle
, vlan
)
3708 && bundle
->floodable
3709 && !bundle
->mirror_out
3710 && set_dst(ctx
, &dst
, in_bundle
, bundle
)) {
3711 dst_set_add(set
, &dst
);
3714 ctx
->nf_output_iface
= NF_OUT_FLOOD
;
3715 } else if (out_bundle
&& set_dst(ctx
, &dst
, in_bundle
, out_bundle
)) {
3716 dst_set_add(set
, &dst
);
3717 ctx
->nf_output_iface
= dst
.port
->odp_port
;
3722 vlan_is_mirrored(const struct ofmirror
*m
, int vlan
)
3724 return !m
->vlans
|| bitmap_is_set(m
->vlans
, vlan
);
3727 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3728 * to a VLAN. In general most packets may be mirrored but we want to drop
3729 * protocols that may confuse switches. */
3731 eth_dst_may_rspan(const uint8_t dst
[ETH_ADDR_LEN
])
3733 /* If you change this function's behavior, please update corresponding
3734 * documentation in vswitch.xml at the same time. */
3735 if (dst
[0] != 0x01) {
3736 /* All the currently banned MACs happen to start with 01 currently, so
3737 * this is a quick way to eliminate most of the good ones. */
3739 if (eth_addr_is_reserved(dst
)) {
3740 /* Drop STP, IEEE pause frames, and other reserved protocols
3741 * (01-80-c2-00-00-0x). */
3745 if (dst
[0] == 0x01 && dst
[1] == 0x00 && dst
[2] == 0x0c) {
3747 if ((dst
[3] & 0xfe) == 0xcc &&
3748 (dst
[4] & 0xfe) == 0xcc &&
3749 (dst
[5] & 0xfe) == 0xcc) {
3750 /* Drop the following protocols plus others following the same
3753 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3754 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3755 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3759 if (!(dst
[3] | dst
[4] | dst
[5])) {
3760 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3769 compose_mirror_dsts(struct action_xlate_ctx
*ctx
,
3770 uint16_t vlan
, const struct ofbundle
*in_bundle
,
3771 struct dst_set
*set
)
3773 struct ofproto_dpif
*ofproto
= ctx
->ofproto
;
3774 mirror_mask_t mirrors
;
3778 mirrors
= in_bundle
->src_mirrors
;
3779 for (i
= 0; i
< set
->n
; i
++) {
3780 mirrors
|= set
->dsts
[i
].port
->bundle
->dst_mirrors
;
3787 flow_vid
= vlan_tci_to_vid(ctx
->flow
.vlan_tci
);
3789 struct ofmirror
*m
= ofproto
->mirrors
[mirror_mask_ffs(mirrors
) - 1];
3790 if (vlan_is_mirrored(m
, vlan
)) {
3794 if (set_dst(ctx
, &dst
, in_bundle
, m
->out
)
3795 && !dst_is_duplicate(set
, &dst
)) {
3796 dst_set_add(set
, &dst
);
3798 } else if (eth_dst_may_rspan(ctx
->flow
.dl_dst
)) {
3799 struct ofbundle
*bundle
;
3801 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
3802 if (ofbundle_includes_vlan(bundle
, m
->out_vlan
)
3803 && set_dst(ctx
, &dst
, in_bundle
, bundle
))
3805 /* set_dst() got dst->vid from the input packet's VLAN,
3806 * not from m->out_vlan, so recompute it. */
3807 dst
.vid
= output_vlan_to_vid(bundle
, m
->out_vlan
);
3809 if (dst_is_duplicate(set
, &dst
)) {
3813 if (bundle
== in_bundle
&& dst
.vid
== flow_vid
) {
3814 /* Don't send out input port on same VLAN. */
3817 dst_set_add(set
, &dst
);
3822 mirrors
&= mirrors
- 1;
3827 compose_actions(struct action_xlate_ctx
*ctx
, uint16_t vlan
,
3828 const struct ofbundle
*in_bundle
,
3829 const struct ofbundle
*out_bundle
)
3831 uint16_t initial_vid
, cur_vid
;
3832 const struct dst
*dst
;
3836 compose_dsts(ctx
, vlan
, in_bundle
, out_bundle
, &set
);
3837 compose_mirror_dsts(ctx
, vlan
, in_bundle
, &set
);
3843 /* Output all the packets we can without having to change the VLAN. */
3844 commit_odp_actions(ctx
);
3845 initial_vid
= vlan_tci_to_vid(ctx
->flow
.vlan_tci
);
3846 for (dst
= set
.dsts
; dst
< &set
.dsts
[set
.n
]; dst
++) {
3847 if (dst
->vid
!= initial_vid
) {
3850 nl_msg_put_u32(ctx
->odp_actions
,
3851 OVS_ACTION_ATTR_OUTPUT
, dst
->port
->odp_port
);
3854 /* Then output the rest. */
3855 cur_vid
= initial_vid
;
3856 for (dst
= set
.dsts
; dst
< &set
.dsts
[set
.n
]; dst
++) {
3857 if (dst
->vid
== initial_vid
) {
3860 if (dst
->vid
!= cur_vid
) {
3863 tci
= htons(dst
->vid
);
3864 tci
|= ctx
->flow
.vlan_tci
& htons(VLAN_PCP_MASK
);
3866 tci
|= htons(VLAN_CFI
);
3868 commit_vlan_tci(ctx
, tci
);
3872 nl_msg_put_u32(ctx
->odp_actions
,
3873 OVS_ACTION_ATTR_OUTPUT
, dst
->port
->odp_port
);
3879 /* Returns the effective vlan of a packet, taking into account both the
3880 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3881 * the packet is untagged and -1 indicates it has an invalid header and
3882 * should be dropped. */
3884 flow_get_vlan(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
3885 struct ofbundle
*in_bundle
, bool have_packet
)
3887 int vlan
= vlan_tci_to_vid(flow
->vlan_tci
);
3889 if (in_bundle
->vlan_mode
== PORT_VLAN_ACCESS
) {
3890 /* Drop tagged packet on access port */
3892 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3893 VLOG_WARN_RL(&rl
, "bridge %s: dropping VLAN %d tagged "
3894 "packet received on port %s configured with "
3895 "implicit VLAN %"PRIu16
,
3896 ofproto
->up
.name
, vlan
,
3897 in_bundle
->name
, in_bundle
->vlan
);
3900 } else if (ofbundle_includes_vlan(in_bundle
, vlan
)) {
3903 /* Drop packets from a VLAN not member of the trunk */
3905 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3906 VLOG_WARN_RL(&rl
, "bridge %s: dropping VLAN %d tagged "
3907 "packet received on port %s not configured for "
3909 ofproto
->up
.name
, vlan
, in_bundle
->name
, vlan
);
3914 if (in_bundle
->vlan_mode
!= PORT_VLAN_TRUNK
) {
3915 return in_bundle
->vlan
;
3917 return ofbundle_includes_vlan(in_bundle
, 0) ? 0 : -1;
3922 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3923 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3924 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3926 is_gratuitous_arp(const struct flow
*flow
)
3928 return (flow
->dl_type
== htons(ETH_TYPE_ARP
)
3929 && eth_addr_is_broadcast(flow
->dl_dst
)
3930 && (flow
->nw_proto
== ARP_OP_REPLY
3931 || (flow
->nw_proto
== ARP_OP_REQUEST
3932 && flow
->nw_src
== flow
->nw_dst
)));
3936 update_learning_table(struct ofproto_dpif
*ofproto
,
3937 const struct flow
*flow
, int vlan
,
3938 struct ofbundle
*in_bundle
)
3940 struct mac_entry
*mac
;
3942 if (!mac_learning_may_learn(ofproto
->ml
, flow
->dl_src
, vlan
)) {
3946 mac
= mac_learning_insert(ofproto
->ml
, flow
->dl_src
, vlan
);
3947 if (is_gratuitous_arp(flow
)) {
3948 /* We don't want to learn from gratuitous ARP packets that are
3949 * reflected back over bond slaves so we lock the learning table. */
3950 if (!in_bundle
->bond
) {
3951 mac_entry_set_grat_arp_lock(mac
);
3952 } else if (mac_entry_is_grat_arp_locked(mac
)) {
3957 if (mac_entry_is_new(mac
) || mac
->port
.p
!= in_bundle
) {
3958 /* The log messages here could actually be useful in debugging,
3959 * so keep the rate limit relatively high. */
3960 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 300);
3961 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
3962 "on port %s in VLAN %d",
3963 ofproto
->up
.name
, ETH_ADDR_ARGS(flow
->dl_src
),
3964 in_bundle
->name
, vlan
);
3966 mac
->port
.p
= in_bundle
;
3967 tag_set_add(&ofproto
->revalidate_set
,
3968 mac_learning_changed(ofproto
->ml
, mac
));
3972 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3973 * dropped. Returns true if they may be forwarded, false if they should be
3976 * If 'have_packet' is true, it indicates that the caller is processing a
3977 * received packet. If 'have_packet' is false, then the caller is just
3978 * revalidating an existing flow because configuration has changed. Either
3979 * way, 'have_packet' only affects logging (there is no point in logging errors
3980 * during revalidation).
3982 * Sets '*in_portp' to the input port. This will be a null pointer if
3983 * flow->in_port does not designate a known input port (in which case
3984 * is_admissible() returns false).
3986 * When returning true, sets '*vlanp' to the effective VLAN of the input
3987 * packet, as returned by flow_get_vlan().
3989 * May also add tags to '*tags', although the current implementation only does
3990 * so in one special case.
3993 is_admissible(struct ofproto_dpif
*ofproto
, const struct flow
*flow
,
3995 tag_type
*tags
, int *vlanp
, struct ofbundle
**in_bundlep
)
3997 struct ofport_dpif
*in_port
;
3998 struct ofbundle
*in_bundle
;
4001 /* Find the port and bundle for the received packet. */
4002 in_port
= get_ofp_port(ofproto
, flow
->in_port
);
4003 *in_bundlep
= in_bundle
= in_port
? in_port
->bundle
: NULL
;
4004 if (!in_port
|| !in_bundle
) {
4005 /* No interface? Something fishy... */
4007 /* Odd. A few possible reasons here:
4009 * - We deleted a port but there are still a few packets queued up
4012 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4013 * we don't know about.
4015 * - Packet arrived on the local port but the local port is not
4018 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4020 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
4022 ofproto
->up
.name
, flow
->in_port
);
4027 *vlanp
= vlan
= flow_get_vlan(ofproto
, flow
, in_bundle
, have_packet
);
4032 /* Drop frames for reserved multicast addresses
4033 * only if forward_bpdu option is absent. */
4034 if (eth_addr_is_reserved(flow
->dl_dst
) &&
4035 !ofproto
->up
.forward_bpdu
) {
4039 /* Drop frames on bundles reserved for mirroring. */
4040 if (in_bundle
->mirror_out
) {
4042 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4043 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port "
4044 "%s, which is reserved exclusively for mirroring",
4045 ofproto
->up
.name
, in_bundle
->name
);
4050 if (in_bundle
->bond
) {
4051 struct mac_entry
*mac
;
4053 switch (bond_check_admissibility(in_bundle
->bond
, in_port
,
4054 flow
->dl_dst
, tags
)) {
4061 case BV_DROP_IF_MOVED
:
4062 mac
= mac_learning_lookup(ofproto
->ml
, flow
->dl_src
, vlan
, NULL
);
4063 if (mac
&& mac
->port
.p
!= in_bundle
&&
4064 (!is_gratuitous_arp(flow
)
4065 || mac_entry_is_grat_arp_locked(mac
))) {
4076 xlate_normal(struct action_xlate_ctx
*ctx
)
4078 struct ofbundle
*in_bundle
;
4079 struct ofbundle
*out_bundle
;
4080 struct mac_entry
*mac
;
4083 ctx
->has_normal
= true;
4085 /* Check whether we should drop packets in this flow. */
4086 if (!is_admissible(ctx
->ofproto
, &ctx
->flow
, ctx
->packet
!= NULL
,
4087 &ctx
->tags
, &vlan
, &in_bundle
)) {
4092 /* Learn source MAC. */
4093 if (ctx
->may_learn
) {
4094 update_learning_table(ctx
->ofproto
, &ctx
->flow
, vlan
, in_bundle
);
4097 /* Determine output bundle. */
4098 mac
= mac_learning_lookup(ctx
->ofproto
->ml
, ctx
->flow
.dl_dst
, vlan
,
4101 out_bundle
= mac
->port
.p
;
4102 } else if (!ctx
->packet
&& !eth_addr_is_multicast(ctx
->flow
.dl_dst
)) {
4103 /* If we are revalidating but don't have a learning entry then eject
4104 * the flow. Installing a flow that floods packets opens up a window
4105 * of time where we could learn from a packet reflected on a bond and
4106 * blackhole packets before the learning table is updated to reflect
4107 * the correct port. */
4108 ctx
->may_set_up_flow
= false;
4111 out_bundle
= OFBUNDLE_FLOOD
;
4114 /* Don't send packets out their input bundles. */
4115 if (in_bundle
== out_bundle
) {
4121 compose_actions(ctx
, vlan
, in_bundle
, out_bundle
);
4125 /* Optimized flow revalidation.
4127 * It's a difficult problem, in general, to tell which facets need to have
4128 * their actions recalculated whenever the OpenFlow flow table changes. We
4129 * don't try to solve that general problem: for most kinds of OpenFlow flow
4130 * table changes, we recalculate the actions for every facet. This is
4131 * relatively expensive, but it's good enough if the OpenFlow flow table
4132 * doesn't change very often.
4134 * However, we can expect one particular kind of OpenFlow flow table change to
4135 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4136 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4137 * table, we add a special case that applies to flow tables in which every rule
4138 * has the same form (that is, the same wildcards), except that the table is
4139 * also allowed to have a single "catch-all" flow that matches all packets. We
4140 * optimize this case by tagging all of the facets that resubmit into the table
4141 * and invalidating the same tag whenever a flow changes in that table. The
4142 * end result is that we revalidate just the facets that need it (and sometimes
4143 * a few more, but not all of the facets or even all of the facets that
4144 * resubmit to the table modified by MAC learning). */
4146 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4147 * into an OpenFlow table with the given 'basis'. */
4149 rule_calculate_tag(const struct flow
*flow
, const struct flow_wildcards
*wc
,
4152 if (flow_wildcards_is_catchall(wc
)) {
4155 struct flow tag_flow
= *flow
;
4156 flow_zero_wildcards(&tag_flow
, wc
);
4157 return tag_create_deterministic(flow_hash(&tag_flow
, secret
));
4161 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
4162 * taggability of that table.
4164 * This function must be called after *each* change to a flow table. If you
4165 * skip calling it on some changes then the pointer comparisons at the end can
4166 * be invalid if you get unlucky. For example, if a flow removal causes a
4167 * cls_table to be destroyed and then a flow insertion causes a cls_table with
4168 * different wildcards to be created with the same address, then this function
4169 * will incorrectly skip revalidation. */
4171 table_update_taggable(struct ofproto_dpif
*ofproto
, uint8_t table_id
)
4173 struct table_dpif
*table
= &ofproto
->tables
[table_id
];
4174 const struct classifier
*cls
= &ofproto
->up
.tables
[table_id
];
4175 struct cls_table
*catchall
, *other
;
4176 struct cls_table
*t
;
4178 catchall
= other
= NULL
;
4180 switch (hmap_count(&cls
->tables
)) {
4182 /* We could tag this OpenFlow table but it would make the logic a
4183 * little harder and it's a corner case that doesn't seem worth it
4189 HMAP_FOR_EACH (t
, hmap_node
, &cls
->tables
) {
4190 if (cls_table_is_catchall(t
)) {
4192 } else if (!other
) {
4195 /* Indicate that we can't tag this by setting both tables to
4196 * NULL. (We know that 'catchall' is already NULL.) */
4203 /* Can't tag this table. */
4207 if (table
->catchall_table
!= catchall
|| table
->other_table
!= other
) {
4208 table
->catchall_table
= catchall
;
4209 table
->other_table
= other
;
4210 ofproto
->need_revalidate
= true;
4214 /* Given 'rule' that has changed in some way (either it is a rule being
4215 * inserted, a rule being deleted, or a rule whose actions are being
4216 * modified), marks facets for revalidation to ensure that packets will be
4217 * forwarded correctly according to the new state of the flow table.
4219 * This function must be called after *each* change to a flow table. See
4220 * the comment on table_update_taggable() for more information. */
4222 rule_invalidate(const struct rule_dpif
*rule
)
4224 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
4226 table_update_taggable(ofproto
, rule
->up
.table_id
);
4228 if (!ofproto
->need_revalidate
) {
4229 struct table_dpif
*table
= &ofproto
->tables
[rule
->up
.table_id
];
4231 if (table
->other_table
&& rule
->tag
) {
4232 tag_set_add(&ofproto
->revalidate_set
, rule
->tag
);
4234 ofproto
->need_revalidate
= true;
4240 get_drop_frags(struct ofproto
*ofproto_
)
4242 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4245 dpif_get_drop_frags(ofproto
->dpif
, &drop_frags
);
4250 set_drop_frags(struct ofproto
*ofproto_
, bool drop_frags
)
4252 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4254 dpif_set_drop_frags(ofproto
->dpif
, drop_frags
);
4258 packet_out(struct ofproto
*ofproto_
, struct ofpbuf
*packet
,
4259 const struct flow
*flow
,
4260 const union ofp_action
*ofp_actions
, size_t n_ofp_actions
)
4262 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4265 error
= validate_actions(ofp_actions
, n_ofp_actions
, flow
,
4266 ofproto
->max_ports
);
4268 struct odputil_keybuf keybuf
;
4269 struct action_xlate_ctx ctx
;
4270 struct ofpbuf
*odp_actions
;
4273 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
4274 odp_flow_key_from_flow(&key
, flow
);
4276 action_xlate_ctx_init(&ctx
, ofproto
, flow
, packet
);
4277 odp_actions
= xlate_actions(&ctx
, ofp_actions
, n_ofp_actions
);
4278 dpif_execute(ofproto
->dpif
, key
.data
, key
.size
,
4279 odp_actions
->data
, odp_actions
->size
, packet
);
4280 ofpbuf_delete(odp_actions
);
4286 get_netflow_ids(const struct ofproto
*ofproto_
,
4287 uint8_t *engine_type
, uint8_t *engine_id
)
4289 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4291 dpif_get_netflow_ids(ofproto
->dpif
, engine_type
, engine_id
);
4294 static struct ofproto_dpif
*
4295 ofproto_dpif_lookup(const char *name
)
4297 struct ofproto
*ofproto
= ofproto_lookup(name
);
4298 return (ofproto
&& ofproto
->ofproto_class
== &ofproto_dpif_class
4299 ? ofproto_dpif_cast(ofproto
)
4304 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
,
4305 const char *args
, void *aux OVS_UNUSED
)
4307 struct ds ds
= DS_EMPTY_INITIALIZER
;
4308 const struct ofproto_dpif
*ofproto
;
4309 const struct mac_entry
*e
;
4311 ofproto
= ofproto_dpif_lookup(args
);
4313 unixctl_command_reply(conn
, 501, "no such bridge");
4317 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
4318 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
4319 struct ofbundle
*bundle
= e
->port
.p
;
4320 ds_put_format(&ds
, "%5d %4d "ETH_ADDR_FMT
" %3d\n",
4321 ofbundle_get_a_port(bundle
)->odp_port
,
4322 e
->vlan
, ETH_ADDR_ARGS(e
->mac
), mac_entry_age(e
));
4324 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));
4328 struct ofproto_trace
{
4329 struct action_xlate_ctx ctx
;
4335 trace_format_rule(struct ds
*result
, uint8_t table_id
, int level
,
4336 const struct rule_dpif
*rule
)
4338 ds_put_char_multiple(result
, '\t', level
);
4340 ds_put_cstr(result
, "No match\n");
4344 ds_put_format(result
, "Rule: table=%"PRIu8
" cookie=%#"PRIx64
" ",
4345 table_id
, ntohll(rule
->up
.flow_cookie
));
4346 cls_rule_format(&rule
->up
.cr
, result
);
4347 ds_put_char(result
, '\n');
4349 ds_put_char_multiple(result
, '\t', level
);
4350 ds_put_cstr(result
, "OpenFlow ");
4351 ofp_print_actions(result
, rule
->up
.actions
, rule
->up
.n_actions
);
4352 ds_put_char(result
, '\n');
4356 trace_format_flow(struct ds
*result
, int level
, const char *title
,
4357 struct ofproto_trace
*trace
)
4359 ds_put_char_multiple(result
, '\t', level
);
4360 ds_put_format(result
, "%s: ", title
);
4361 if (flow_equal(&trace
->ctx
.flow
, &trace
->flow
)) {
4362 ds_put_cstr(result
, "unchanged");
4364 flow_format(result
, &trace
->ctx
.flow
);
4365 trace
->flow
= trace
->ctx
.flow
;
4367 ds_put_char(result
, '\n');
4371 trace_format_regs(struct ds
*result
, int level
, const char *title
,
4372 struct ofproto_trace
*trace
)
4376 ds_put_char_multiple(result
, '\t', level
);
4377 ds_put_format(result
, "%s:", title
);
4378 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
4379 ds_put_format(result
, " reg%zu=0x%"PRIx32
, i
, trace
->flow
.regs
[i
]);
4381 ds_put_char(result
, '\n');
4385 trace_resubmit(struct action_xlate_ctx
*ctx
, struct rule_dpif
*rule
)
4387 struct ofproto_trace
*trace
= CONTAINER_OF(ctx
, struct ofproto_trace
, ctx
);
4388 struct ds
*result
= trace
->result
;
4390 ds_put_char(result
, '\n');
4391 trace_format_flow(result
, ctx
->recurse
+ 1, "Resubmitted flow", trace
);
4392 trace_format_regs(result
, ctx
->recurse
+ 1, "Resubmitted regs", trace
);
4393 trace_format_rule(result
, ctx
->table_id
, ctx
->recurse
+ 1, rule
);
4397 ofproto_unixctl_trace(struct unixctl_conn
*conn
, const char *args_
,
4398 void *aux OVS_UNUSED
)
4400 char *dpname
, *arg1
, *arg2
, *arg3
;
4401 char *args
= xstrdup(args_
);
4402 char *save_ptr
= NULL
;
4403 struct ofproto_dpif
*ofproto
;
4404 struct ofpbuf odp_key
;
4405 struct ofpbuf
*packet
;
4406 struct rule_dpif
*rule
;
4412 ofpbuf_init(&odp_key
, 0);
4415 dpname
= strtok_r(args
, " ", &save_ptr
);
4416 arg1
= strtok_r(NULL
, " ", &save_ptr
);
4417 arg2
= strtok_r(NULL
, " ", &save_ptr
);
4418 arg3
= strtok_r(NULL
, "", &save_ptr
); /* Get entire rest of line. */
4419 if (dpname
&& arg1
&& (!arg2
|| !strcmp(arg2
, "-generate")) && !arg3
) {
4420 /* ofproto/trace dpname flow [-generate] */
4423 /* Convert string to datapath key. */
4424 ofpbuf_init(&odp_key
, 0);
4425 error
= odp_flow_key_from_string(arg1
, &odp_key
);
4427 unixctl_command_reply(conn
, 501, "Bad flow syntax");
4431 /* Convert odp_key to flow. */
4432 error
= odp_flow_key_to_flow(odp_key
.data
, odp_key
.size
, &flow
);
4434 unixctl_command_reply(conn
, 501, "Invalid flow");
4438 /* Generate a packet, if requested. */
4440 packet
= ofpbuf_new(0);
4441 flow_compose(packet
, &flow
);
4443 } else if (dpname
&& arg1
&& arg2
&& arg3
) {
4444 /* ofproto/trace dpname tun_id in_port packet */
4448 tun_id
= htonll(strtoull(arg1
, NULL
, 0));
4449 in_port
= ofp_port_to_odp_port(atoi(arg2
));
4451 packet
= ofpbuf_new(strlen(args
) / 2);
4452 arg3
= ofpbuf_put_hex(packet
, arg3
, NULL
);
4453 arg3
+= strspn(arg3
, " ");
4454 if (*arg3
!= '\0') {
4455 unixctl_command_reply(conn
, 501, "Trailing garbage in command");
4458 if (packet
->size
< ETH_HEADER_LEN
) {
4459 unixctl_command_reply(conn
, 501,
4460 "Packet data too short for Ethernet");
4464 ds_put_cstr(&result
, "Packet: ");
4465 s
= ofp_packet_to_string(packet
->data
, packet
->size
, packet
->size
);
4466 ds_put_cstr(&result
, s
);
4469 flow_extract(packet
, tun_id
, in_port
, &flow
);
4471 unixctl_command_reply(conn
, 501, "Bad command syntax");
4475 ofproto
= ofproto_dpif_lookup(dpname
);
4477 unixctl_command_reply(conn
, 501, "Unknown ofproto (use ofproto/list "
4482 ds_put_cstr(&result
, "Flow: ");
4483 flow_format(&result
, &flow
);
4484 ds_put_char(&result
, '\n');
4486 rule
= rule_dpif_lookup(ofproto
, &flow
, 0);
4487 trace_format_rule(&result
, 0, 0, rule
);
4489 struct ofproto_trace trace
;
4490 struct ofpbuf
*odp_actions
;
4492 trace
.result
= &result
;
4494 action_xlate_ctx_init(&trace
.ctx
, ofproto
, &flow
, packet
);
4495 trace
.ctx
.resubmit_hook
= trace_resubmit
;
4496 odp_actions
= xlate_actions(&trace
.ctx
,
4497 rule
->up
.actions
, rule
->up
.n_actions
);
4499 ds_put_char(&result
, '\n');
4500 trace_format_flow(&result
, 0, "Final flow", &trace
);
4501 ds_put_cstr(&result
, "Datapath actions: ");
4502 format_odp_actions(&result
, odp_actions
->data
, odp_actions
->size
);
4503 ofpbuf_delete(odp_actions
);
4505 if (!trace
.ctx
.may_set_up_flow
) {
4507 ds_put_cstr(&result
, "\nThis flow is not cachable.");
4509 ds_put_cstr(&result
, "\nThe datapath actions are incomplete--"
4510 "for complete actions, please supply a packet.");
4515 unixctl_command_reply(conn
, 200, ds_cstr(&result
));
4518 ds_destroy(&result
);
4519 ofpbuf_delete(packet
);
4520 ofpbuf_uninit(&odp_key
);
4525 ofproto_dpif_clog(struct unixctl_conn
*conn OVS_UNUSED
,
4526 const char *args_ OVS_UNUSED
, void *aux OVS_UNUSED
)
4529 unixctl_command_reply(conn
, 200, NULL
);
4533 ofproto_dpif_unclog(struct unixctl_conn
*conn OVS_UNUSED
,
4534 const char *args_ OVS_UNUSED
, void *aux OVS_UNUSED
)
4537 unixctl_command_reply(conn
, 200, NULL
);
4541 ofproto_dpif_unixctl_init(void)
4543 static bool registered
;
4549 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace
, NULL
);
4550 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show
, NULL
);
4552 unixctl_command_register("ofproto/clog", ofproto_dpif_clog
, NULL
);
4553 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog
, NULL
);
4556 const struct ofproto_class ofproto_dpif_class
= {
4583 port_is_lacp_current
,
4584 NULL
, /* rule_choose_table */
4591 rule_modify_actions
,
4600 get_cfm_remote_mpids
,
4605 is_mirror_output_bundle
,
4606 forward_bpdu_changed
,