2 * Copyright (c) 2010 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 "wdp-xflow.h"
26 #include "mac-learning.h"
32 #include "openflow/nicira-ext.h"
33 #include "openflow/openflow.h"
35 #include "poll-loop.h"
36 #include "port-array.h"
43 #include "wdp-provider.h"
45 #include "xflow-util.h"
49 VLOG_DEFINE_THIS_MODULE(wdp_xflow)
53 TABLEID_CLASSIFIER = 1
56 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
58 /* Maximum numbers of rules. */
59 #define WX_MAX_WILD 65536 /* Wildcarded rules. */
60 #define WX_MAX_EXACT 1048576 /* Exact-match rules. */
63 struct list list_node;
66 struct classifier cls;
67 struct netdev_monitor *netdev_monitor;
68 struct port_array ports; /* Index is xflow port nr;
69 * wdp_port->opp.port_no is OFP port nr. */
70 struct shash port_by_name;
71 long long int next_expiration;
73 /* Rules that might need to be revalidated. */
74 bool need_revalidate; /* Revalidate all subrules? */
75 bool revalidate_all; /* Revalidate all subrules and other rules? */
76 struct tag_set revalidate_set; /* Tag set of (sub)rules to revalidate. */
78 /* Hooks for ovs-vswitchd. */
79 const struct ofhooks *ofhooks;
82 /* Used by default ofhooks. */
83 struct mac_learning *ml;
86 static const struct ofhooks default_ofhooks;
88 static struct list all_wx = LIST_INITIALIZER(&all_wx);
90 static int wx_port_init(struct wx *);
91 static void wx_port_process_change(struct wx *wx, int error, char *devname,
92 wdp_port_poll_cb_func *cb, void *aux);
93 static void wx_port_refresh_groups(struct wx *);
101 wx_cast(const struct wdp *wdp)
103 return CONTAINER_OF(wdp, struct wx, wdp);
107 wx_xlate_actions(struct wx *, const union ofp_action *, size_t n,
108 const flow_t *flow, const struct ofpbuf *packet,
109 tag_type *tags, struct xflow_actions *out,
110 bool *may_set_up_flow);
115 uint64_t packet_count; /* Number of packets received. */
116 uint64_t byte_count; /* Number of bytes received. */
117 uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */
118 long long int used; /* Last-used time (0 if never used). */
119 tag_type tags; /* Tags (set only by hooks). */
121 /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
122 * exact-match rule (having cr.wc.wildcards of 0) generated from the
123 * wildcard rule 'super'. In this case, 'list' is an element of the
126 * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
127 * a list of subrules. A super-rule with no wildcards (where
128 * cr.wc.wildcards is 0) will never have any subrules. */
129 struct wx_rule *super;
134 * A super-rule with wildcard fields never has xflow actions (since the
135 * datapath only supports exact-match flows). */
136 bool installed; /* Installed in datapath? */
137 bool may_install; /* True ordinarily; false if actions must
138 * be reassessed for every packet. */
140 union xflow_action *xflow_actions;
143 static void wx_rule_destroy(struct wx *, struct wx_rule *);
144 static void wx_rule_update_actions(struct wx *, struct wx_rule *);
145 static void wx_rule_execute(struct wx *, struct wx_rule *,
146 struct ofpbuf *packet, const flow_t *);
147 static bool wx_rule_make_actions(struct wx *, struct wx_rule *,
148 const struct ofpbuf *packet);
149 static void wx_rule_install(struct wx *, struct wx_rule *,
150 struct wx_rule *displaced_rule);
152 static struct wx_rule *
153 wx_rule_cast(const struct cls_rule *cls_rule)
155 return cls_rule ? CONTAINER_OF(cls_rule, struct wx_rule, wr.cr) : NULL;
158 /* Returns true if 'rule' is merely an implementation detail that should be
159 * hidden from the client. */
161 wx_rule_is_hidden(const struct wx_rule *rule)
163 return rule->super != NULL;
167 wx_rule_free(struct wx_rule *rule)
169 wdp_rule_uninit(&rule->wr);
170 free(rule->xflow_actions);
175 wx_rule_account(struct wx *wx OVS_UNUSED, struct wx_rule *rule OVS_UNUSED,
176 uint64_t extra_bytes OVS_UNUSED)
178 /* XXX call account_cb hook */
182 wx_rule_post_uninstall(struct wx *wx, struct wx_rule *rule)
184 struct wx_rule *super = rule->super;
186 wx_rule_account(wx, rule, 0);
188 /* XXX netflow expiration */
191 super->packet_count += rule->packet_count;
192 super->byte_count += rule->byte_count;
194 /* Reset counters to prevent double counting if the rule ever gets
196 rule->packet_count = 0;
197 rule->byte_count = 0;
198 rule->accounted_bytes = 0;
200 //XXX netflow_flow_clear(&rule->nf_flow);
205 xflow_flow_stats_to_msec(const struct xflow_flow_stats *stats)
207 return (stats->used_sec
208 ? stats->used_sec * 1000 + stats->used_nsec / 1000000
213 wx_rule_update_time(struct wx *wx OVS_UNUSED, struct wx_rule *rule,
214 const struct xflow_flow_stats *stats)
216 long long int used = xflow_flow_stats_to_msec(stats);
217 if (used > rule->used) {
219 if (rule->super && used > rule->super->used) {
220 rule->super->used = used;
222 //XXX netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
227 wx_rule_update_stats(struct wx *wx, struct wx_rule *rule,
228 const struct xflow_flow_stats *stats)
230 if (stats->n_packets) {
231 wx_rule_update_time(wx, rule, stats);
232 rule->packet_count += stats->n_packets;
233 rule->byte_count += stats->n_bytes;
234 /* XXX netflow_flow_update_flags(&rule->nf_flow, stats->tcp_flags); */
239 wx_rule_uninstall(struct wx *wx, struct wx_rule *rule)
241 assert(!rule->wr.cr.flow.wildcards);
242 if (rule->installed) {
243 struct xflow_flow xflow_flow;
245 xflow_key_from_flow(&xflow_flow.key, &rule->wr.cr.flow);
246 xflow_flow.actions = NULL;
247 xflow_flow.n_actions = 0;
248 xflow_flow.flags = 0;
249 if (!xfif_flow_del(wx->xfif, &xflow_flow)) {
250 wx_rule_update_stats(wx, rule, &xflow_flow.stats);
252 rule->installed = false;
254 wx_rule_post_uninstall(wx, rule);
260 is_controller_rule(struct wx_rule *rule)
262 /* If the only action is send to the controller then don't report
263 * NetFlow expiration messages since it is just part of the control
264 * logic for the network and not real traffic. */
268 && rule->super->n_actions == 1
269 && action_outputs_to_port(&rule->super->actions[0],
270 htons(OFPP_CONTROLLER)));
275 wx_rule_remove(struct wx *wx, struct wx_rule *rule)
277 if (rule->wr.cr.flow.wildcards) {
278 COVERAGE_INC(wx_del_wc_flow);
279 wx->need_revalidate = true;
281 wx_rule_uninstall(wx, rule);
283 classifier_remove(&wx->cls, &rule->wr.cr);
284 wx_rule_destroy(wx, rule);
288 wx_rule_revalidate(struct wx *wx, struct wx_rule *rule)
290 const flow_t *flow = &rule->wr.cr.flow;
292 COVERAGE_INC(wx_rule_revalidate);
294 struct wx_rule *super;
295 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
297 wx_rule_remove(wx, rule);
299 } else if (super != rule->super) {
300 COVERAGE_INC(wx_revalidate_moved);
301 list_remove(&rule->list);
302 list_push_back(&super->list, &rule->list);
304 rule->wr.hard_timeout = super->wr.hard_timeout;
305 rule->wr.idle_timeout = super->wr.idle_timeout;
306 rule->wr.created = super->wr.created;
311 wx_rule_update_actions(wx, rule);
315 /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its
316 * super-rule's list of subrules. If 'rule' is a super-rule, also iterates
317 * through all of its subrules and revalidates them, destroying any that no
318 * longer has a super-rule (which is probably all of them).
320 * Before calling this function, the caller must make have removed 'rule' from
321 * the classifier. If 'rule' is an exact-match rule, the caller is also
322 * responsible for ensuring that it has been uninstalled from the datapath. */
324 wx_rule_destroy(struct wx *wx, struct wx_rule *rule)
327 struct wx_rule *subrule, *next;
328 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
329 wx_rule_revalidate(wx, subrule);
332 list_remove(&rule->list);
339 wx_rule_has_out_port(const struct wx_rule *rule, uint16_t out_port)
341 const union ofp_action *oa;
342 struct actions_iterator i;
344 if (out_port == htons(OFPP_NONE)) {
347 for (oa = actions_first(&i, rule->wr.actions,
350 oa = actions_next(&i)) {
351 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
359 /* Caller is responsible for initializing the 'cr' and ofp_table_id members of
360 * the returned rule. */
361 static struct wx_rule *
362 wx_rule_create(struct wx_rule *super,
363 const union ofp_action *actions, size_t n_actions,
364 uint16_t idle_timeout, uint16_t hard_timeout)
366 struct wx_rule *rule = xzalloc(sizeof *rule);
367 wdp_rule_init(&rule->wr, actions, n_actions);
368 rule->wr.idle_timeout = idle_timeout;
369 rule->wr.hard_timeout = hard_timeout;
370 rule->used = rule->wr.created;
373 list_push_back(&super->list, &rule->list);
375 list_init(&rule->list);
378 netflow_flow_clear(&rule->nf_flow);
379 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
385 /* Executes the actions indicated by 'rule' on 'packet', which is in flow
386 * 'flow' and is considered to have arrived on xflow port 'in_port'.
388 * The flow that 'packet' actually contains does not need to actually match
389 * 'rule'; the actions in 'rule' will be applied to it either way. Likewise,
390 * the packet and byte counters for 'rule' will be credited for the packet sent
391 * out whether or not the packet actually matches 'rule'.
393 * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
394 * the caller must already have accurately composed xflow actions for it given
395 * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if
396 * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
397 * function will compose a set of xflow actions based on 'rule''s OpenFlow
398 * actions and apply them to 'packet'. */
400 wx_rule_execute(struct wx *wx, struct wx_rule *rule,
401 struct ofpbuf *packet, const flow_t *flow)
403 const union xflow_action *actions;
405 struct xflow_actions a;
407 /* Grab or compose the xflow actions.
409 * The special case for an exact-match 'rule' where 'flow' is not the
410 * rule's flow is important to avoid, e.g., sending a packet out its input
411 * port simply because the xflow actions were composed for the wrong
413 if (rule->wr.cr.flow.wildcards
414 || !flow_equal_headers(flow, &rule->wr.cr.flow))
416 struct wx_rule *super = rule->super ? rule->super : rule;
417 if (wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions, flow,
418 packet, NULL, &a, NULL)) {
422 n_actions = a.n_actions;
424 actions = rule->xflow_actions;
425 n_actions = rule->n_xflow_actions;
428 /* Execute the xflow actions. */
429 if (!xfif_execute(wx->xfif, flow->in_port,
430 actions, n_actions, packet)) {
431 struct xflow_flow_stats stats;
432 flow_extract_stats(flow, packet, &stats);
433 wx_rule_update_stats(wx, rule, &stats);
434 rule->used = time_msec();
435 //XXX netflow_flow_update_time(wx->netflow, &rule->nf_flow, rule->used);
440 wx_rule_insert(struct wx *wx, struct wx_rule *rule, struct ofpbuf *packet,
443 struct wx_rule *displaced_rule;
445 /* Insert the rule in the classifier. */
446 displaced_rule = wx_rule_cast(classifier_insert(&wx->cls, &rule->wr.cr));
447 if (!rule->wr.cr.flow.wildcards) {
448 wx_rule_make_actions(wx, rule, packet);
451 /* Send the packet and credit it to the rule. */
454 flow_extract(packet, 0, in_port, &flow);
455 wx_rule_execute(wx, rule, packet, &flow);
458 /* Install the rule in the datapath only after sending the packet, to
459 * avoid packet reordering. */
460 if (rule->wr.cr.flow.wildcards) {
461 COVERAGE_INC(wx_add_wc_flow);
462 wx->need_revalidate = true;
464 wx_rule_install(wx, rule, displaced_rule);
467 /* Free the rule that was displaced, if any. */
468 if (displaced_rule) {
469 rule->wr.client_data = displaced_rule->wr.client_data;
470 wx_rule_destroy(wx, displaced_rule);
474 static struct wx_rule *
475 wx_rule_create_subrule(struct wx *wx, struct wx_rule *rule, const flow_t *flow)
477 struct wx_rule *subrule;
479 subrule = wx_rule_create(rule, NULL, 0,
480 rule->wr.idle_timeout,
481 rule->wr.hard_timeout);
482 /* Subrules aren't really in any OpenFlow table, so don't bother with
483 * subrule->wr.ofp_table_id. */
484 COVERAGE_INC(wx_subrule_create);
485 cls_rule_from_flow(flow, &subrule->wr.cr);
486 classifier_insert_exact(&wx->cls, &subrule->wr.cr);
491 /* Returns true if the actions changed, false otherwise. */
493 wx_rule_make_actions(struct wx *wx, struct wx_rule *rule,
494 const struct ofpbuf *packet)
496 const struct wx_rule *super;
497 struct xflow_actions a;
500 assert(!rule->wr.cr.flow.wildcards);
502 super = rule->super ? rule->super : rule;
503 wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions,
504 &rule->wr.cr.flow, packet,
505 &rule->tags, &a, &rule->may_install);
507 actions_len = a.n_actions * sizeof *a.actions;
508 if (rule->n_xflow_actions != a.n_actions
509 || memcmp(rule->xflow_actions, a.actions, actions_len)) {
510 COVERAGE_INC(wx_xflow_unchanged);
511 free(rule->xflow_actions);
512 rule->n_xflow_actions = a.n_actions;
513 rule->xflow_actions = xmemdup(a.actions, actions_len);
521 do_put_flow(struct wx *wx, struct wx_rule *rule, int flags,
522 struct xflow_flow_put *put)
524 memset(&put->flow.stats, 0, sizeof put->flow.stats);
525 xflow_key_from_flow(&put->flow.key, &rule->wr.cr.flow);
526 put->flow.actions = rule->xflow_actions;
527 put->flow.n_actions = rule->n_xflow_actions;
530 return xfif_flow_put(wx->xfif, put);
534 wx_rule_install(struct wx *wx, struct wx_rule *rule, struct wx_rule *displaced_rule)
536 assert(!rule->wr.cr.flow.wildcards);
538 if (rule->may_install) {
539 struct xflow_flow_put put;
540 if (!do_put_flow(wx, rule,
541 XFLOWPF_CREATE | XFLOWPF_MODIFY | XFLOWPF_ZERO_STATS,
543 rule->installed = true;
544 if (displaced_rule) {
545 wx_rule_update_stats(wx, displaced_rule, &put.flow.stats);
546 wx_rule_post_uninstall(wx, displaced_rule);
549 } else if (displaced_rule) {
550 wx_rule_uninstall(wx, displaced_rule);
555 wx_rule_reinstall(struct wx *wx, struct wx_rule *rule)
557 if (rule->installed) {
558 struct xflow_flow_put put;
559 COVERAGE_INC(wx_dp_missed);
560 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY, &put);
562 wx_rule_install(wx, rule, NULL);
567 wx_rule_update_actions(struct wx *wx, struct wx_rule *rule)
569 bool actions_changed;
571 uint16_t new_out_iface, old_out_iface;
573 old_out_iface = rule->nf_flow.output_iface;
575 actions_changed = wx_rule_make_actions(wx, rule, NULL);
577 if (rule->may_install) {
578 if (rule->installed) {
579 if (actions_changed) {
580 struct xflow_flow_put put;
581 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY
582 | XFLOWPF_ZERO_STATS, &put);
583 wx_rule_update_stats(wx, rule, &put.flow.stats);
585 /* Temporarily set the old output iface so that NetFlow
586 * messages have the correct output interface for the old
588 new_out_iface = rule->nf_flow.output_iface;
589 rule->nf_flow.output_iface = old_out_iface;
591 wx_rule_post_uninstall(wx, rule);
592 //rule->nf_flow.output_iface = new_out_iface;
595 wx_rule_install(wx, rule, NULL);
598 wx_rule_uninstall(wx, rule);
603 add_output_group_action(struct xflow_actions *actions, uint16_t group,
604 uint16_t *nf_output_iface)
606 xflow_actions_add(actions, XFLOWAT_OUTPUT_GROUP)->output_group.group = group;
608 if (group == WX_GROUP_ALL || group == WX_GROUP_FLOOD) {
609 *nf_output_iface = NF_OUT_FLOOD;
614 add_controller_action(struct xflow_actions *actions, uint16_t max_len)
616 union xflow_action *a = xflow_actions_add(actions, XFLOWAT_CONTROLLER);
617 a->controller.arg = max_len;
620 struct wx_xlate_ctx {
622 flow_t flow; /* Flow to which these actions correspond. */
623 int recurse; /* Recursion level, via xlate_table_action. */
625 const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
626 * null pointer if we are revalidating
627 * without a packet to refer to. */
630 struct xflow_actions *out; /* Datapath actions. */
631 tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */
632 bool may_set_up_flow; /* True ordinarily; false if the actions must
633 * be reassessed for every packet. */
634 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
637 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
638 struct wx_xlate_ctx *ctx);
641 add_output_action(struct wx_xlate_ctx *ctx, uint16_t port)
643 const struct wdp_port *wdp_port = port_array_get(&ctx->wx->ports, port);
646 if (wdp_port->opp.config & OFPPC_NO_FWD) {
647 /* Forwarding disabled on port. */
652 * We don't have an ofport record for this port, but it doesn't hurt to
653 * allow forwarding to it anyhow. Maybe such a port will appear later
654 * and we're pre-populating the flow table.
658 xflow_actions_add(ctx->out, XFLOWAT_OUTPUT)->output.port = port;
659 //ctx->nf_output_iface = port;
662 static struct wx_rule *
663 wx_rule_lookup_valid(struct wx *wx, const flow_t *flow)
665 struct wx_rule *rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
667 /* The rule we found might not be valid, since we could be in need of
668 * revalidation. If it is not valid, don't return it. */
671 && wx->need_revalidate
672 && !wx_rule_revalidate(wx, rule)) {
673 COVERAGE_INC(wx_invalidated);
681 xlate_table_action(struct wx_xlate_ctx *ctx, uint16_t in_port)
684 uint16_t old_in_port;
685 struct wx_rule *rule;
687 /* Look up a flow with 'in_port' as the input port. Then restore the
688 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
689 * have surprising behavior). */
690 old_in_port = ctx->flow.in_port;
691 ctx->flow.in_port = in_port;
692 rule = wx_rule_lookup_valid(ctx->wx, &ctx->flow);
693 ctx->flow.in_port = old_in_port;
701 do_xlate_actions(rule->wr.actions, rule->wr.n_actions, ctx);
708 xlate_output_action__(struct wx_xlate_ctx *ctx,
709 uint16_t port, uint16_t max_len)
712 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
714 ctx->nf_output_iface = NF_OUT_DROP;
718 add_output_action(ctx, ctx->flow.in_port);
721 xlate_table_action(ctx, ctx->flow.in_port);
724 if (!ctx->wx->ofhooks->normal_cb(&ctx->flow, ctx->packet,
726 &ctx->nf_output_iface,
728 COVERAGE_INC(wx_uninstallable);
729 ctx->may_set_up_flow = false;
734 add_output_group_action(ctx->out, WX_GROUP_FLOOD,
735 &ctx->nf_output_iface);
738 add_output_group_action(ctx->out, WX_GROUP_ALL, &ctx->nf_output_iface);
740 case OFPP_CONTROLLER:
741 add_controller_action(ctx->out, max_len);
744 add_output_action(ctx, XFLOWP_LOCAL);
747 xflow_port = ofp_port_to_xflow_port(port);
748 if (xflow_port != ctx->flow.in_port) {
749 add_output_action(ctx, xflow_port);
754 if (prev_nf_output_iface == NF_OUT_FLOOD) {
755 ctx->nf_output_iface = NF_OUT_FLOOD;
756 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
757 ctx->nf_output_iface = prev_nf_output_iface;
758 } else if (prev_nf_output_iface != NF_OUT_DROP &&
759 ctx->nf_output_iface != NF_OUT_FLOOD) {
760 ctx->nf_output_iface = NF_OUT_MULTI;
765 xlate_output_action(struct wx_xlate_ctx *ctx,
766 const struct ofp_action_output *oao)
768 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
771 /* If the final xflow action in 'ctx' is "pop priority", drop it, as an
772 * optimization, because we're going to add another action that sets the
773 * priority immediately after, or because there are no actions following the
776 remove_pop_action(struct wx_xlate_ctx *ctx)
778 size_t n = ctx->out->n_actions;
779 if (n > 0 && ctx->out->actions[n - 1].type == XFLOWAT_POP_PRIORITY) {
780 ctx->out->n_actions--;
785 xlate_enqueue_action(struct wx_xlate_ctx *ctx,
786 const struct ofp_action_enqueue *oae)
788 uint16_t ofp_port, xflow_port;
792 error = xfif_queue_to_priority(ctx->wx->xfif, ntohl(oae->queue_id),
795 /* Fall back to ordinary output action. */
796 xlate_output_action__(ctx, ntohs(oae->port), 0);
800 /* Figure out xflow output port. */
801 ofp_port = ntohs(oae->port);
802 if (ofp_port != OFPP_IN_PORT) {
803 xflow_port = ofp_port_to_xflow_port(ofp_port);
805 xflow_port = ctx->flow.in_port;
808 /* Add xflow actions. */
809 remove_pop_action(ctx);
810 xflow_actions_add(ctx->out, XFLOWAT_SET_PRIORITY)->priority.priority
812 add_output_action(ctx, xflow_port);
813 xflow_actions_add(ctx->out, XFLOWAT_POP_PRIORITY);
815 /* Update NetFlow output port. */
816 if (ctx->nf_output_iface == NF_OUT_DROP) {
817 ctx->nf_output_iface = xflow_port;
818 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
819 ctx->nf_output_iface = NF_OUT_MULTI;
824 xlate_nicira_action(struct wx_xlate_ctx *ctx,
825 const struct nx_action_header *nah)
827 const struct nx_action_resubmit *nar;
828 const struct nx_action_set_tunnel *nast;
829 union xflow_action *oa;
830 int subtype = ntohs(nah->subtype);
832 assert(nah->vendor == htonl(NX_VENDOR_ID));
835 nar = (const struct nx_action_resubmit *) nah;
836 xlate_table_action(ctx, ofp_port_to_xflow_port(ntohs(nar->in_port)));
839 case NXAST_SET_TUNNEL:
840 nast = (const struct nx_action_set_tunnel *) nah;
841 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TUNNEL);
842 ctx->flow.tun_id = oa->tunnel.tun_id = nast->tun_id;
845 /* If you add a new action here that modifies flow data, don't forget to
846 * update the flow key in ctx->flow at the same time. */
849 VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
855 do_xlate_actions(const union ofp_action *in, size_t n_in,
856 struct wx_xlate_ctx *ctx)
858 struct actions_iterator iter;
859 const union ofp_action *ia;
860 const struct wdp_port *port;
862 port = port_array_get(&ctx->wx->ports, ctx->flow.in_port);
863 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
864 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, stp_eth_addr)
865 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
866 /* Drop this flow. */
870 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
871 uint16_t type = ntohs(ia->type);
872 union xflow_action *oa;
876 xlate_output_action(ctx, &ia->output);
879 case OFPAT_SET_VLAN_VID:
880 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
881 oa->dl_tci.tci = ia->vlan_vid.vlan_vid & htons(VLAN_VID_MASK);
882 oa->dl_tci.mask = htons(VLAN_VID_MASK);
883 ctx->flow.dl_vlan = ia->vlan_vid.vlan_vid;
886 case OFPAT_SET_VLAN_PCP:
887 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
888 oa->dl_tci.tci = htons((ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT)
890 oa->dl_tci.mask = htons(VLAN_PCP_MASK);
892 if (ctx->flow.dl_vlan == htons(OFP_VLAN_NONE)) {
893 ctx->flow.dl_vlan = htons(0);
895 ctx->flow.dl_vlan_pcp = ia->vlan_pcp.vlan_pcp;
898 case OFPAT_STRIP_VLAN:
899 xflow_actions_add(ctx->out, XFLOWAT_STRIP_VLAN);
900 ctx->flow.dl_vlan = htons(OFP_VLAN_NONE);
901 ctx->flow.dl_vlan_pcp = 0;
904 case OFPAT_SET_DL_SRC:
905 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_SRC);
906 memcpy(oa->dl_addr.dl_addr,
907 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
908 memcpy(ctx->flow.dl_src,
909 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
912 case OFPAT_SET_DL_DST:
913 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_DST);
914 memcpy(oa->dl_addr.dl_addr,
915 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
916 memcpy(ctx->flow.dl_dst,
917 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
920 case OFPAT_SET_NW_SRC:
921 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_SRC);
922 ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
925 case OFPAT_SET_NW_DST:
926 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_DST);
927 ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
930 case OFPAT_SET_NW_TOS:
931 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_TOS);
932 ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
935 case OFPAT_SET_TP_SRC:
936 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_SRC);
937 ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port;
940 case OFPAT_SET_TP_DST:
941 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_DST);
942 ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
946 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
950 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
954 VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
960 /* Returns true if 'flow' and 'actions' may be set up as a flow in the kernel.
961 * This is true most of the time, but we don't allow flows that would prevent
962 * DHCP replies from being seen by the local port to be set up in the
965 * We only need this, strictly speaking, when in-band control is turned on. */
967 wx_may_set_up(const flow_t *flow, const struct xflow_actions *actions)
969 if (flow->dl_type == htons(ETH_TYPE_IP)
970 && flow->nw_proto == IP_TYPE_UDP
971 && flow->tp_src == htons(DHCP_SERVER_PORT)
972 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
975 for (i = 0; i < actions->n_actions; i++) {
976 const struct xflow_action_output *oao = &actions->actions[i].output;
977 if (oao->type == XFLOWAT_OUTPUT && oao->port == XFLOWP_LOCAL) {
988 wx_xlate_actions(struct wx *wx, const union ofp_action *in, size_t n_in,
989 const flow_t *flow, const struct ofpbuf *packet,
990 tag_type *tags, struct xflow_actions *out,
991 bool *may_set_up_flow)
993 tag_type no_tags = 0;
994 struct wx_xlate_ctx ctx;
995 COVERAGE_INC(wx_ofp2xflow);
996 xflow_actions_init(out);
1000 ctx.packet = packet;
1002 ctx.tags = tags ? tags : &no_tags;
1003 ctx.may_set_up_flow = true;
1004 ctx.nf_output_iface = NF_OUT_DROP;
1005 do_xlate_actions(in, n_in, &ctx);
1006 remove_pop_action(&ctx);
1008 if (may_set_up_flow) {
1009 *may_set_up_flow = ctx.may_set_up_flow && wx_may_set_up(flow, out);
1012 if (nf_output_iface) {
1013 *nf_output_iface = ctx.nf_output_iface;
1016 if (xflow_actions_overflow(out)) {
1017 xflow_actions_init(out);
1018 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
1024 update_used(struct wx *wx)
1026 struct xflow_flow *flows;
1031 error = xfif_flow_list_all(wx->xfif, &flows, &n_flows);
1036 for (i = 0; i < n_flows; i++) {
1037 struct xflow_flow *f = &flows[i];
1038 struct wx_rule *rule;
1041 xflow_key_to_flow(&f->key, &flow);
1042 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, &flow));
1043 if (!rule || !rule->installed) {
1044 COVERAGE_INC(wx_unexpected_rule);
1045 xfif_flow_del(wx->xfif, f);
1049 wx_rule_update_time(wx, rule, &f->stats);
1050 wx_rule_account(wx, rule, f->stats.n_bytes);
1056 uninstall_idle_flow(struct wx *wx, struct wx_rule *rule)
1058 assert(rule->installed);
1059 assert(!rule->wr.cr.flow.wildcards);
1062 wx_rule_remove(wx, rule);
1064 wx_rule_uninstall(wx, rule);
1069 expire_rule(struct cls_rule *cls_rule, void *wx_)
1071 struct wx *wx = wx_;
1072 struct wx_rule *rule = wx_rule_cast(cls_rule);
1073 long long int hard_expire, idle_expire, expire, now;
1075 hard_expire = (rule->wr.hard_timeout
1076 ? rule->wr.created + rule->wr.hard_timeout * 1000
1078 idle_expire = (rule->wr.idle_timeout
1079 && (rule->super || list_is_empty(&rule->list))
1080 ? rule->used + rule->wr.idle_timeout * 1000
1082 expire = MIN(hard_expire, idle_expire);
1086 if (rule->installed && now >= rule->used + 5000) {
1087 uninstall_idle_flow(wx, rule);
1088 } else if (!rule->wr.cr.flow.wildcards) {
1089 //XXX active_timeout(wx, rule);
1095 COVERAGE_INC(wx_expired);
1097 /* Update stats. This code will be a no-op if the rule expired
1098 * due to an idle timeout. */
1099 if (rule->wr.cr.flow.wildcards) {
1100 struct wx_rule *subrule, *next;
1101 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
1102 wx_rule_remove(wx, subrule);
1105 wx_rule_uninstall(wx, rule);
1109 if (!wx_rule_is_hidden(rule)) {
1110 send_flow_removed(wx, rule, now,
1112 ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
1115 wx_rule_remove(wx, rule);
1118 struct revalidate_cbdata {
1120 bool revalidate_all; /* Revalidate all exact-match rules? */
1121 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
1122 struct tag_set revalidate_set; /* Set of tags to revalidate. */
1126 revalidate_rule(struct wx *wx, struct wx_rule *rule)
1128 const flow_t *flow = &rule->wr.cr.flow;
1130 COVERAGE_INC(wx_revalidate_rule);
1132 struct wx_rule *super;
1133 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
1135 wx_rule_remove(wx, rule);
1137 } else if (super != rule->super) {
1138 COVERAGE_INC(wx_revalidate_moved);
1139 list_remove(&rule->list);
1140 list_push_back(&super->list, &rule->list);
1141 rule->super = super;
1142 rule->wr.hard_timeout = super->wr.hard_timeout;
1143 rule->wr.idle_timeout = super->wr.idle_timeout;
1144 rule->wr.created = super->wr.created;
1149 wx_rule_update_actions(wx, rule);
1154 revalidate_cb(struct cls_rule *sub_, void *cbdata_)
1156 struct wx_rule *sub = wx_rule_cast(sub_);
1157 struct revalidate_cbdata *cbdata = cbdata_;
1159 if (cbdata->revalidate_all
1160 || (cbdata->revalidate_subrules && sub->super)
1161 || tag_set_intersects(&cbdata->revalidate_set, sub->tags)) {
1162 revalidate_rule(cbdata->wx, sub);
1167 wx_run_one(struct wx *wx)
1169 if (time_msec() >= wx->next_expiration) {
1170 COVERAGE_INC(wx_expiration);
1171 wx->next_expiration = time_msec() + 1000;
1174 classifier_for_each(&wx->cls, CLS_INC_ALL, expire_rule, wx);
1176 /* XXX account_checkpoint_cb */
1179 if (wx->need_revalidate || !tag_set_is_empty(&wx->revalidate_set)) {
1180 struct revalidate_cbdata cbdata;
1182 cbdata.revalidate_all = wx->revalidate_all;
1183 cbdata.revalidate_subrules = wx->need_revalidate;
1184 cbdata.revalidate_set = wx->revalidate_set;
1185 tag_set_init(&wx->revalidate_set);
1186 COVERAGE_INC(wx_revalidate);
1187 classifier_for_each(&wx->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
1188 wx->need_revalidate = false;
1197 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1204 wx_wait_one(struct wx *wx)
1206 if (wx->need_revalidate || !tag_set_is_empty(&wx->revalidate_set)) {
1207 poll_immediate_wake();
1208 } else if (wx->next_expiration != LLONG_MAX) {
1209 poll_timer_wait_until(wx->next_expiration);
1218 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1224 static int wx_flow_flush(struct wdp *);
1227 wx_enumerate(const struct wdp_class *wdp_class, struct svec *all_wdps)
1229 struct svec names = SVEC_EMPTY_INITIALIZER;
1230 int error = xf_enumerate_names(wdp_class->type, &names);
1231 svec_move(all_wdps, &names);
1236 wx_open(const struct wdp_class *wdp_class, const char *name, bool create,
1243 ? xfif_create_and_open(name, wdp_class->type, &xfif)
1244 : xfif_open(name, wdp_class->type, &xfif));
1248 wx = xzalloc(sizeof *wx);
1249 list_push_back(&all_wx, &wx->list_node);
1250 wdp_init(&wx->wdp, wdp_class, name, 0, 0);
1252 classifier_init(&wx->cls);
1253 wx->netdev_monitor = netdev_monitor_create();
1254 port_array_init(&wx->ports);
1255 shash_init(&wx->port_by_name);
1256 wx->next_expiration = time_msec() + 1000;
1257 tag_set_init(&wx->revalidate_set);
1261 wx->ofhooks = &default_ofhooks;
1263 wx->ml = mac_learning_create();
1272 wx_close(struct wdp *wdp)
1274 struct wx *wx = wx_cast(wdp);
1277 xfif_close(wx->xfif);
1278 classifier_destroy(&wx->cls);
1279 netdev_monitor_destroy(wx->netdev_monitor);
1280 list_remove(&wx->list_node);
1281 mac_learning_destroy(wx->ml);
1286 wx_get_all_names(const struct wdp *wdp, struct svec *all_names)
1288 struct wx *wx = wx_cast(wdp);
1290 return xfif_get_all_names(wx->xfif, all_names);
1294 wx_destroy(struct wdp *wdp)
1296 struct wx *wx = wx_cast(wdp);
1298 return xfif_delete(wx->xfif);
1302 wx_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
1304 struct wx *wx = wx_cast(wdp);
1305 struct ofp_switch_features *osf;
1307 unsigned int port_no;
1308 struct wdp_port *port;
1310 buf = ofpbuf_new(sizeof *osf);
1311 osf = ofpbuf_put_zeros(buf, sizeof *osf);
1313 osf->capabilities = htonl(OFPC_ARP_MATCH_IP);
1314 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1315 (1u << OFPAT_SET_VLAN_VID) |
1316 (1u << OFPAT_SET_VLAN_PCP) |
1317 (1u << OFPAT_STRIP_VLAN) |
1318 (1u << OFPAT_SET_DL_SRC) |
1319 (1u << OFPAT_SET_DL_DST) |
1320 (1u << OFPAT_SET_NW_SRC) |
1321 (1u << OFPAT_SET_NW_DST) |
1322 (1u << OFPAT_SET_NW_TOS) |
1323 (1u << OFPAT_SET_TP_SRC) |
1324 (1u << OFPAT_SET_TP_DST) |
1325 (1u << OFPAT_ENQUEUE));
1327 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1328 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1336 count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
1338 struct wx_rule *rule = wx_rule_cast(cls_rule);
1339 int *n_subrules = n_subrules_;
1347 wx_get_stats(const struct wdp *wdp, struct wdp_stats *stats)
1349 struct wx *wx = wx_cast(wdp);
1350 struct xflow_stats xflow_stats;
1353 error = xfif_get_xf_stats(wx->xfif, &xflow_stats);
1354 stats->max_ports = xflow_stats.max_ports;
1359 wx_get_table_stats(const struct wdp *wdp, struct ofpbuf *stats)
1361 struct wx *wx = wx_cast(wdp);
1362 struct xflow_stats xflow_stats;
1363 struct ofp_table_stats *exact, *wild;
1366 xfif_get_xf_stats(wx->xfif, &xflow_stats);
1367 /* XXX should pass up errors, but there are no appropriate OpenFlow error
1371 classifier_for_each(&wx->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
1373 exact = ofpbuf_put_zeros(stats, sizeof *exact);
1374 exact->table_id = TABLEID_HASH;
1375 strcpy(exact->name, "exact");
1376 exact->wildcards = htonl(0);
1377 exact->max_entries = htonl(MIN(WX_MAX_EXACT, xflow_stats.max_capacity));
1378 exact->active_count = htonl(classifier_count_exact(&wx->cls) - n_subrules);
1379 exact->lookup_count = htonll(xflow_stats.n_hit + xflow_stats.n_missed);
1380 exact->matched_count = htonll(xflow_stats.n_hit);
1382 wild = ofpbuf_put_zeros(stats, sizeof *exact);
1383 wild->table_id = TABLEID_CLASSIFIER;
1384 strcpy(wild->name, "classifier");
1385 wild->wildcards = htonl(OVSFW_ALL);
1386 wild->max_entries = htonl(WX_MAX_WILD);
1387 wild->active_count = htonl(classifier_count_wild(&wx->cls));
1388 wild->lookup_count = htonll(0); /* XXX */
1389 wild->matched_count = htonll(0); /* XXX */
1395 wx_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
1397 struct wx *wx = wx_cast(wdp);
1399 return xfif_get_drop_frags(wx->xfif, drop_frags);
1403 wx_set_drop_frags(struct wdp *wdp, bool drop_frags)
1405 struct wx *wx = wx_cast(wdp);
1407 return xfif_set_drop_frags(wx->xfif, drop_frags);
1411 wx_port_add(struct wdp *wdp, const char *devname,
1412 bool internal, uint16_t *port_no)
1414 struct wx *wx = wx_cast(wdp);
1415 uint16_t xflow_flags = internal ? XFLOW_PORT_INTERNAL : 0;
1416 return xfif_port_add(wx->xfif, devname, xflow_flags, port_no);
1420 wx_port_del(struct wdp *wdp, uint16_t port_no)
1422 struct wx *wx = wx_cast(wdp);
1424 return xfif_port_del(wx->xfif, port_no);
1428 wx_answer_port_query(const struct wdp_port *port, struct wdp_port *portp)
1431 wdp_port_copy(portp, port);
1439 wx_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
1440 struct wdp_port *portp)
1442 struct wx *wx = wx_cast(wdp);
1443 const struct wdp_port *port;
1445 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1446 return wx_answer_port_query(port, portp);
1450 wx_port_query_by_name(const struct wdp *wdp, const char *devname,
1451 struct wdp_port *portp)
1453 struct wx *wx = wx_cast(wdp);
1455 return wx_answer_port_query(shash_find_data(&wx->port_by_name, devname),
1460 wx_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
1462 struct wx *wx = wx_cast(wdp);
1463 struct wdp_port *port;
1466 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1470 changes = config ^ port->opp.config;
1472 if (changes & OFPPC_PORT_DOWN) {
1474 if (config & OFPPC_PORT_DOWN) {
1475 error = netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
1477 error = netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
1480 port->opp.config ^= OFPPC_PORT_DOWN;
1484 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
1485 if (changes & REVALIDATE_BITS) {
1486 COVERAGE_INC(wx_costly_flags);
1487 port->opp.config ^= changes & REVALIDATE_BITS;
1488 wx->need_revalidate = true;
1490 #undef REVALIDATE_BITS
1492 if (changes & OFPPC_NO_FLOOD) {
1493 port->opp.config ^= OFPPC_NO_FLOOD;
1494 wx_port_refresh_groups(wx);
1497 if (changes & OFPPC_NO_PACKET_IN) {
1498 port->opp.config ^= OFPPC_NO_PACKET_IN;
1505 wx_port_list(const struct wdp *wdp, struct wdp_port **portsp, size_t *n_portsp)
1507 struct wx *wx = wx_cast(wdp);
1508 struct wdp_port *ports, *port;
1509 unsigned int port_no;
1512 *n_portsp = n_ports = port_array_count(&wx->ports);
1513 *portsp = ports = xmalloc(n_ports * sizeof *ports);
1515 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1516 wdp_port_copy(&ports[i++], port);
1518 assert(i == n_ports);
1524 wx_port_poll(struct wdp *wdp, wdp_port_poll_cb_func *cb, void *aux)
1526 struct wx *wx = wx_cast(wdp);
1532 while ((error = xfif_port_poll(wx->xfif, &devname)) != EAGAIN) {
1533 wx_port_process_change(wx, error, devname, cb, aux);
1534 if (error && error != ENOBUFS) {
1538 while ((error = netdev_monitor_poll(wx->netdev_monitor,
1539 &devname)) != EAGAIN) {
1540 wx_port_process_change(wx, error, devname, cb, aux);
1541 if (error && error != ENOBUFS) {
1549 wx_port_poll_wait(const struct wdp *wdp)
1551 struct wx *wx = wx_cast(wdp);
1553 xfif_port_poll_wait(wx->xfif);
1554 netdev_monitor_poll_wait(wx->netdev_monitor);
1558 static struct wdp_rule *
1559 wx_flow_get(const struct wdp *wdp, const flow_t *flow, unsigned int include)
1561 struct wx *wx = wx_cast(wdp);
1562 struct wx_rule *rule;
1565 table_id = flow->wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
1566 if (!(include & (1u << table_id))) {
1570 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, flow));
1571 return rule && !wx_rule_is_hidden(rule) ? &rule->wr : NULL;
1574 static struct wdp_rule *
1575 wx_flow_match(const struct wdp *wdp, const flow_t *flow)
1577 struct wx *wx = wx_cast(wdp);
1578 struct wx_rule *rule;
1580 rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
1582 if (wx_rule_is_hidden(rule)) {
1591 struct wx_for_each_thunk_aux {
1592 wdp_flow_cb_func *client_callback;
1597 wx_for_each_thunk(struct cls_rule *cls_rule, void *aux_)
1599 struct wx_for_each_thunk_aux *aux = aux_;
1600 struct wx_rule *rule = wx_rule_cast(cls_rule);
1602 if (!wx_rule_is_hidden(rule)) {
1603 aux->client_callback(&rule->wr, aux->client_aux);
1608 wx_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
1609 unsigned int include,
1610 wdp_flow_cb_func *client_callback, void *client_aux)
1612 struct wx *wx = wx_cast(wdp);
1613 struct wx_for_each_thunk_aux aux;
1617 if (include & (1u << TABLEID_HASH)) {
1618 cls_include |= CLS_INC_EXACT;
1620 if (include & (1u << TABLEID_CLASSIFIER)) {
1621 cls_include |= CLS_INC_WILD;
1624 aux.client_callback = client_callback;
1625 aux.client_aux = client_aux;
1626 classifier_for_each_match(&wx->cls, target, cls_include,
1627 wx_for_each_thunk, &aux);
1630 /* Obtains statistic counters for 'rule' within 'wx' and stores them into
1631 * '*stats'. If 'rule' is a wildcarded rule, the returned statistic include
1632 * statistics for all of 'rule''s subrules. */
1634 query_stats(struct wx *wx, struct wx_rule *rule, struct wdp_flow_stats *stats)
1636 struct wx_rule *subrule;
1637 struct xflow_flow *xflow_flows;
1638 size_t n_xflow_flows;
1640 /* Start from historical data for 'rule' itself that are no longer tracked
1641 * by the datapath. This counts, for example, subrules that have
1643 stats->n_packets = rule->packet_count;
1644 stats->n_bytes = rule->byte_count;
1645 stats->inserted = rule->wr.created;
1646 stats->used = LLONG_MIN;
1647 stats->tcp_flags = 0;
1650 /* Prepare to ask the datapath for statistics on 'rule', or if it is
1651 * wildcarded then on all of its subrules.
1653 * Also, add any statistics that are not tracked by the datapath for each
1654 * subrule. This includes, for example, statistics for packets that were
1655 * executed "by hand" by ofproto via xfif_execute() but must be accounted
1657 n_xflow_flows = rule->wr.cr.flow.wildcards ? list_size(&rule->list) : 1;
1658 xflow_flows = xzalloc(n_xflow_flows * sizeof *xflow_flows);
1659 if (rule->wr.cr.flow.wildcards) {
1661 LIST_FOR_EACH (subrule, struct wx_rule, list, &rule->list) {
1662 xflow_key_from_flow(&xflow_flows[i++].key, &subrule->wr.cr.flow);
1663 stats->n_packets += subrule->packet_count;
1664 stats->n_bytes += subrule->byte_count;
1667 xflow_key_from_flow(&xflow_flows[0].key, &rule->wr.cr.flow);
1670 /* Fetch up-to-date statistics from the datapath and add them in. */
1671 if (!xfif_flow_get_multiple(wx->xfif, xflow_flows, n_xflow_flows)) {
1673 for (i = 0; i < n_xflow_flows; i++) {
1674 struct xflow_flow *xflow_flow = &xflow_flows[i];
1677 stats->n_packets += xflow_flow->stats.n_packets;
1678 stats->n_bytes += xflow_flow->stats.n_bytes;
1679 used = xflow_flow_stats_to_msec(&xflow_flow->stats);
1680 if (used > stats->used) {
1683 stats->tcp_flags |= xflow_flow->stats.tcp_flags;
1690 wx_flow_get_stats(const struct wdp *wdp,
1691 const struct wdp_rule *wdp_rule,
1692 struct wdp_flow_stats *stats)
1694 struct wx *wx = wx_cast(wdp);
1695 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1697 query_stats(wx, rule, stats);
1702 wx_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
1704 struct wx *wx = wx_cast(wdp);
1706 /* XXX overlap with a subrule? */
1707 return classifier_rule_overlaps(&wx->cls, flow);
1711 wx_flow_put(struct wdp *wdp, const struct wdp_flow_put *put,
1712 struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
1714 struct wx *wx = wx_cast(wdp);
1715 struct wx_rule *rule;
1716 uint8_t ofp_table_id;
1718 ofp_table_id = put->flow->wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
1719 if (put->ofp_table_id != 0xff && put->ofp_table_id != ofp_table_id) {
1723 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, put->flow));
1724 if (rule && wx_rule_is_hidden(rule)) {
1729 if (!(put->flags & WDP_PUT_MODIFY)) {
1733 if (!(put->flags & WDP_PUT_CREATE)) {
1736 if ((put->flow->wildcards
1737 ? classifier_count_wild(&wx->cls) >= WX_MAX_WILD
1738 : classifier_count_exact(&wx->cls) >= WX_MAX_EXACT)) {
1739 /* XXX subrules should not count against exact-match limit */
1744 rule = wx_rule_create(NULL, put->actions, put->n_actions,
1745 put->idle_timeout, put->hard_timeout);
1746 cls_rule_from_flow(put->flow, &rule->wr.cr);
1747 rule->wr.ofp_table_id = ofp_table_id;
1748 wx_rule_insert(wx, rule, NULL, 0);
1752 memset(old_stats, 0, sizeof *old_stats);
1762 wx_flow_delete(struct wdp *wdp, struct wdp_rule *wdp_rule,
1763 struct wdp_flow_stats *final_stats)
1765 struct wx *wx = wx_cast(wdp);
1766 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1768 wx_rule_remove(wx, rule);
1770 memset(final_stats, 0, sizeof *final_stats); /* XXX */
1776 wx_flush_rule(struct cls_rule *cls_rule, void *wx_)
1778 struct wx_rule *rule = wx_rule_cast(cls_rule);
1779 struct wx *wx = wx_;
1781 /* Mark the flow as not installed, even though it might really be
1782 * installed, so that wx_rule_remove() doesn't bother trying to uninstall
1783 * it. There is no point in uninstalling it individually since we are
1784 * about to blow away all the flows with xfif_flow_flush(). */
1785 rule->installed = false;
1787 wx_rule_remove(wx, rule);
1791 wx_flow_flush(struct wdp *wdp)
1793 struct wx *wx = wx_cast(wdp);
1795 COVERAGE_INC(wx_flow_flush);
1796 classifier_for_each(&wx->cls, CLS_INC_ALL, wx_flush_rule, wx);
1797 xfif_flow_flush(wx->xfif);
1802 wx_execute(struct wdp *wdp, uint16_t in_port,
1803 const union ofp_action actions[], int n_actions,
1804 const struct ofpbuf *packet)
1806 struct wx *wx = wx_cast(wdp);
1807 struct xflow_actions xflow_actions;
1811 flow_extract((struct ofpbuf *) packet, 0, in_port, &flow);
1812 error = wx_xlate_actions(wx, actions, n_actions, &flow, packet,
1813 NULL, &xflow_actions, NULL);
1817 xfif_execute(wx->xfif, ofp_port_to_xflow_port(in_port),
1818 xflow_actions.actions, xflow_actions.n_actions, packet);
1823 wx_flow_inject(struct wdp *wdp, struct wdp_rule *wdp_rule,
1824 uint16_t in_port, const struct ofpbuf *packet)
1826 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1829 error = wx_execute(wdp, in_port, rule->wr.actions, rule->wr.n_actions,
1832 rule->packet_count++;
1833 rule->byte_count += packet->size;
1834 rule->used = time_msec();
1840 wx_recv_get_mask(const struct wdp *wdp, int *listen_mask)
1842 struct wx *wx = wx_cast(wdp);
1843 int xflow_listen_mask;
1846 error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
1849 if (xflow_listen_mask & XFLOWL_MISS) {
1850 *listen_mask |= 1 << WDP_CHAN_MISS;
1852 if (xflow_listen_mask & XFLOWL_ACTION) {
1853 *listen_mask |= 1 << WDP_CHAN_ACTION;
1855 if (xflow_listen_mask & XFLOWL_SFLOW) {
1856 *listen_mask |= 1 << WDP_CHAN_SFLOW;
1863 wx_recv_set_mask(struct wdp *wdp, int listen_mask)
1865 struct wx *wx = wx_cast(wdp);
1866 int xflow_listen_mask;
1868 xflow_listen_mask = 0;
1869 if (listen_mask & (1 << WDP_CHAN_MISS)) {
1870 xflow_listen_mask |= XFLOWL_MISS;
1872 if (listen_mask & (1 << WDP_CHAN_ACTION)) {
1873 xflow_listen_mask |= XFLOWL_ACTION;
1875 if (listen_mask & (1 << WDP_CHAN_SFLOW)) {
1876 xflow_listen_mask |= XFLOWL_SFLOW;
1879 return xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
1883 wx_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
1885 struct wx *wx = wx_cast(wdp);
1887 return xfif_get_sflow_probability(wx->xfif, probability);
1891 wx_set_sflow_probability(struct wdp *wdp, uint32_t probability)
1893 struct wx *wx = wx_cast(wdp);
1895 return xfif_set_sflow_probability(wx->xfif, probability);
1899 wx_translate_xflow_msg(struct xflow_msg *msg, struct ofpbuf *payload,
1900 struct wdp_packet *packet)
1902 packet->in_port = xflow_port_to_ofp_port(msg->port);
1903 packet->send_len = 0;
1906 switch (msg->type) {
1907 case _XFLOWL_MISS_NR:
1908 packet->channel = WDP_CHAN_MISS;
1909 packet->payload = payload;
1910 packet->tun_id = msg->arg;
1913 case _XFLOWL_ACTION_NR:
1914 packet->channel = WDP_CHAN_ACTION;
1915 packet->payload = payload;
1916 packet->send_len = msg->arg;
1919 case _XFLOWL_SFLOW_NR:
1921 ofpbuf_delete(payload);
1925 VLOG_WARN_RL(&rl, "received XFLOW message of unexpected type %"PRIu32,
1927 ofpbuf_delete(payload);
1932 static const uint8_t *
1933 get_local_mac(const struct wx *wx)
1935 const struct wdp_port *port = port_array_get(&wx->ports, XFLOWP_LOCAL);
1936 return port ? port->opp.hw_addr : NULL;
1939 /* Returns true if 'packet' is a DHCP reply to the local port. Such a reply
1940 * should be sent to the local port regardless of the flow table.
1942 * We only need this, strictly speaking, when in-band control is turned on. */
1944 wx_is_local_dhcp_reply(const struct wx *wx,
1945 const flow_t *flow, const struct ofpbuf *packet)
1947 if (flow->dl_type == htons(ETH_TYPE_IP)
1948 && flow->nw_proto == IP_TYPE_UDP
1949 && flow->tp_src == htons(DHCP_SERVER_PORT)
1950 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
1953 const uint8_t *local_mac = get_local_mac(wx);
1954 struct dhcp_header *dhcp = ofpbuf_at(
1955 packet, (char *)packet->l7 - (char *)packet->data, sizeof *dhcp);
1956 return dhcp && local_mac && eth_addr_equals(dhcp->chaddr, local_mac);
1963 wx_explode_rule(struct wx *wx, struct xflow_msg *msg, struct ofpbuf *payload)
1965 struct wx_rule *rule;
1968 flow_extract(payload, 0, xflow_port_to_ofp_port(msg->port), &flow);
1970 if (wx_is_local_dhcp_reply(wx, &flow, payload)) {
1971 union xflow_action action;
1973 memset(&action, 0, sizeof(action));
1974 action.output.type = XFLOWAT_OUTPUT;
1975 action.output.port = XFLOWP_LOCAL;
1976 xfif_execute(wx->xfif, msg->port, &action, 1, payload);
1979 rule = wx_rule_lookup_valid(wx, &flow);
1984 if (rule->wr.cr.flow.wildcards) {
1985 rule = wx_rule_create_subrule(wx, rule, &flow);
1986 wx_rule_make_actions(wx, rule, payload);
1988 if (!rule->may_install) {
1989 /* The rule is not installable, that is, we need to process every
1990 * packet, so process the current packet and set its actions into
1992 wx_rule_make_actions(wx, rule, payload);
1994 /* XXX revalidate rule if it needs it */
1998 wx_rule_execute(wx, rule, payload, &flow);
1999 wx_rule_reinstall(wx, rule);
2005 wx_recv(struct wdp *wdp, struct wdp_packet *packet)
2007 struct wx *wx = wx_cast(wdp);
2010 /* XXX need to avoid 50*50 potential cost for caller. */
2011 for (i = 0; i < 50; i++) {
2012 struct xflow_msg *msg;
2016 error = xfif_recv(wx->xfif, &buf);
2021 msg = ofpbuf_pull(buf, sizeof *msg);
2022 if (msg->type != _XFLOWL_MISS_NR || !wx_explode_rule(wx, msg, buf)) {
2023 return wx_translate_xflow_msg(msg, buf, packet);
2031 wx_recv_purge_queue__(struct wx *wx, int max, int xflow_listen_mask,
2036 error = xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
2040 while (max > 0 && (error = xfif_recv(wx->xfif, &buf)) == 0) {
2045 if (error && error != EAGAIN) {
2051 wx_recv_purge(struct wdp *wdp)
2053 struct wx *wx = wx_cast(wdp);
2054 struct xflow_stats xflow_stats;
2055 int xflow_listen_mask;
2058 xfif_get_xf_stats(wx->xfif, &xflow_stats);
2060 error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
2061 if (error || !(xflow_listen_mask & XFLOWL_ALL)) {
2065 if (xflow_listen_mask & XFLOWL_MISS) {
2066 wx_recv_purge_queue__(wx, xflow_stats.max_miss_queue, XFLOWL_MISS,
2069 if (xflow_listen_mask & XFLOWL_ACTION) {
2070 wx_recv_purge_queue__(wx, xflow_stats.max_action_queue, XFLOWL_ACTION,
2073 if (xflow_listen_mask & XFLOWL_SFLOW) {
2074 wx_recv_purge_queue__(wx, xflow_stats.max_sflow_queue, XFLOWL_SFLOW,
2078 retval = xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
2079 return retval ? retval : error;
2084 wx_recv_wait(struct wdp *wdp)
2086 struct wx *wx = wx_cast(wdp);
2088 xfif_recv_wait(wx->xfif);
2092 wx_set_ofhooks(struct wdp *wdp, const struct ofhooks *ofhooks, void *aux)
2094 struct wx *wx = wx_cast(wdp);
2096 if (wx->ofhooks == &default_ofhooks) {
2097 mac_learning_destroy(wx->ml);
2101 wx->ofhooks = ofhooks;
2107 wx_revalidate(struct wdp *wdp, tag_type tag)
2109 struct wx *wx = wx_cast(wdp);
2111 tag_set_add(&wx->revalidate_set, tag);
2115 wx_revalidate_all(struct wdp *wdp)
2117 struct wx *wx = wx_cast(wdp);
2119 wx->revalidate_all = true;
2122 static void wx_port_update(struct wx *, const char *devname,
2123 wdp_port_poll_cb_func *cb, void *aux);
2124 static void wx_port_reinit(struct wx *, wdp_port_poll_cb_func *cb, void *aux);
2127 wx_port_process_change(struct wx *wx, int error, char *devname,
2128 wdp_port_poll_cb_func *cb, void *aux)
2130 if (error == ENOBUFS) {
2131 wx_port_reinit(wx, cb, aux);
2132 } else if (!error) {
2133 wx_port_update(wx, devname, cb, aux);
2139 wx_port_refresh_group(struct wx *wx, unsigned int group)
2143 struct wdp_port *port;
2144 unsigned int port_no;
2146 assert(group == WX_GROUP_ALL || group == WX_GROUP_FLOOD);
2148 ports = xmalloc(port_array_count(&wx->ports) * sizeof *ports);
2150 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
2151 if (group == WX_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
2152 ports[n_ports++] = port_no;
2155 xfif_port_group_set(wx->xfif, group, ports, n_ports);
2162 wx_port_refresh_groups(struct wx *wx)
2164 wx_port_refresh_group(wx, WX_GROUP_FLOOD);
2165 wx_port_refresh_group(wx, WX_GROUP_ALL);
2169 wx_port_reinit(struct wx *wx, wdp_port_poll_cb_func *cb, void *aux)
2171 struct svec devnames;
2172 struct wdp_port *wdp_port;
2173 unsigned int port_no;
2174 struct xflow_port *xflow_ports;
2175 size_t n_xflow_ports;
2178 svec_init(&devnames);
2179 PORT_ARRAY_FOR_EACH (wdp_port, &wx->ports, port_no) {
2180 svec_add (&devnames, (char *) wdp_port->opp.name);
2182 xfif_port_list(wx->xfif, &xflow_ports, &n_xflow_ports);
2183 for (i = 0; i < n_xflow_ports; i++) {
2184 svec_add(&devnames, xflow_ports[i].devname);
2188 svec_sort_unique(&devnames);
2189 for (i = 0; i < devnames.n; i++) {
2190 wx_port_update(wx, devnames.names[i], cb, aux);
2192 svec_destroy(&devnames);
2194 wx_port_refresh_groups(wx);
2197 static struct wdp_port *
2198 make_wdp_port(const struct xflow_port *xflow_port)
2200 struct netdev_options netdev_options;
2201 enum netdev_flags flags;
2202 struct wdp_port *wdp_port;
2203 struct netdev *netdev;
2207 memset(&netdev_options, 0, sizeof netdev_options);
2208 netdev_options.name = xflow_port->devname;
2209 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
2211 error = netdev_open(&netdev_options, &netdev);
2213 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
2214 "cannot be opened (%s)",
2215 xflow_port->devname, xflow_port->port,
2216 xflow_port->devname, strerror(error));
2220 wdp_port = xmalloc(sizeof *wdp_port);
2221 wdp_port->netdev = netdev;
2222 wdp_port->opp.port_no = xflow_port_to_ofp_port(xflow_port->port);
2223 netdev_get_etheraddr(netdev, wdp_port->opp.hw_addr);
2224 strncpy((char *) wdp_port->opp.name, xflow_port->devname,
2225 sizeof wdp_port->opp.name);
2226 wdp_port->opp.name[sizeof wdp_port->opp.name - 1] = '\0';
2228 netdev_get_flags(netdev, &flags);
2229 wdp_port->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
2231 netdev_get_carrier(netdev, &carrier);
2232 wdp_port->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
2234 netdev_get_features(netdev,
2235 &wdp_port->opp.curr, &wdp_port->opp.advertised,
2236 &wdp_port->opp.supported, &wdp_port->opp.peer);
2238 wdp_port->devname = xstrdup(xflow_port->devname);
2239 wdp_port->internal = (xflow_port->flags & XFLOW_PORT_INTERNAL) != 0;
2244 wx_port_conflicts(const struct wx *wx, const struct xflow_port *xflow_port)
2246 if (port_array_get(&wx->ports, xflow_port->port)) {
2247 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
2250 } else if (shash_find(&wx->port_by_name, xflow_port->devname)) {
2251 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
2252 xflow_port->devname);
2260 wdp_port_equal(const struct wdp_port *a_, const struct wdp_port *b_)
2262 const struct ofp_phy_port *a = &a_->opp;
2263 const struct ofp_phy_port *b = &b_->opp;
2265 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
2266 return (a->port_no == b->port_no
2267 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
2268 && !strcmp((char *) a->name, (char *) b->name)
2269 && a->state == b->state
2270 && a->config == b->config
2271 && a->curr == b->curr
2272 && a->advertised == b->advertised
2273 && a->supported == b->supported
2274 && a->peer == b->peer);
2278 wx_port_install(struct wx *wx, struct wdp_port *wdp_port)
2280 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2281 const char *netdev_name = (const char *) wdp_port->opp.name;
2283 netdev_monitor_add(wx->netdev_monitor, wdp_port->netdev);
2284 port_array_set(&wx->ports, xflow_port, wdp_port);
2285 shash_add(&wx->port_by_name, netdev_name, wdp_port);
2289 wx_port_remove(struct wx *wx, struct wdp_port *wdp_port)
2291 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2293 netdev_monitor_remove(wx->netdev_monitor, wdp_port->netdev);
2294 port_array_delete(&wx->ports, xflow_port);
2295 shash_delete(&wx->port_by_name,
2296 shash_find(&wx->port_by_name, (char *) wdp_port->opp.name));
2300 wx_port_free(struct wdp_port *wdp_port)
2303 netdev_close(wdp_port->netdev);
2309 wx_port_update(struct wx *wx, const char *devname,
2310 wdp_port_poll_cb_func *cb, void *aux)
2312 struct xflow_port xflow_port;
2313 struct wdp_port *old_wdp_port;
2314 struct wdp_port *new_wdp_port;
2317 COVERAGE_INC(wx_update_port);
2319 /* Query the datapath for port information. */
2320 error = xfif_port_query_by_name(wx->xfif, devname, &xflow_port);
2322 /* Find the old wdp_port. */
2323 old_wdp_port = shash_find_data(&wx->port_by_name, devname);
2325 if (!old_wdp_port) {
2326 /* There's no port named 'devname' but there might be a port with
2327 * the same port number. This could happen if a port is deleted
2328 * and then a new one added in its place very quickly, or if a port
2329 * is renamed. In the former case we want to send an OFPPR_DELETE
2330 * and an OFPPR_ADD, and in the latter case we want to send a
2331 * single OFPPR_MODIFY. We can distinguish the cases by comparing
2332 * the old port's ifindex against the new port, or perhaps less
2333 * reliably but more portably by comparing the old port's MAC
2334 * against the new port's MAC. However, this code isn't that smart
2335 * and always sends an OFPPR_MODIFY (XXX). */
2336 old_wdp_port = port_array_get(&wx->ports, xflow_port.port);
2338 } else if (error != ENOENT && error != ENODEV) {
2339 VLOG_WARN_RL(&rl, "xfif_port_query_by_name returned unexpected error "
2340 "%s", strerror(error));
2344 /* Create a new wdp_port. */
2345 new_wdp_port = !error ? make_wdp_port(&xflow_port) : NULL;
2347 /* Eliminate a few pathological cases. */
2348 if (!old_wdp_port && !new_wdp_port) {
2350 } else if (old_wdp_port && new_wdp_port) {
2351 /* Most of the 'config' bits are OpenFlow soft state, but
2352 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
2353 * OpenFlow bits from old_wdp_port. (make_wdp_port() only sets
2354 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
2355 new_wdp_port->opp.config |= old_wdp_port->opp.config & ~OFPPC_PORT_DOWN;
2357 if (wdp_port_equal(old_wdp_port, new_wdp_port)) {
2358 /* False alarm--no change. */
2359 wx_port_free(new_wdp_port);
2364 /* Now deal with the normal cases. */
2366 wx_port_remove(wx, old_wdp_port);
2369 wx_port_install(wx, new_wdp_port);
2373 if (!old_wdp_port) {
2374 (*cb)(&new_wdp_port->opp, OFPPR_ADD, aux);
2375 } else if (!new_wdp_port) {
2376 (*cb)(&old_wdp_port->opp, OFPPR_DELETE, aux);
2378 (*cb)(&new_wdp_port->opp, OFPPR_MODIFY, aux);
2381 /* Update port groups. */
2382 wx_port_refresh_groups(wx);
2385 wx_port_free(old_wdp_port);
2389 wx_port_init(struct wx *wx)
2391 struct xflow_port *ports;
2396 error = xfif_port_list(wx->xfif, &ports, &n_ports);
2401 for (i = 0; i < n_ports; i++) {
2402 const struct xflow_port *xflow_port = &ports[i];
2403 if (!wx_port_conflicts(wx, xflow_port)) {
2404 struct wdp_port *wdp_port = make_wdp_port(xflow_port);
2406 wx_port_install(wx, wdp_port);
2411 wx_port_refresh_groups(wx);
2416 wdp_xflow_register(void)
2418 static const struct wdp_class wdp_xflow_class = {
2434 wx_port_query_by_number,
2435 wx_port_query_by_name,
2442 wx_flow_for_each_match,
2452 wx_get_sflow_probability,
2453 wx_set_sflow_probability,
2462 static bool inited = false;
2475 xf_enumerate_types(&types);
2478 SVEC_FOR_EACH (i, type, &types) {
2479 struct wdp_class *class;
2481 class = xmalloc(sizeof *class);
2482 *class = wdp_xflow_class;
2483 class->type = xstrdup(type);
2488 if (!wdp_register_provider(class)) {
2493 svec_destroy(&types);
2497 default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
2498 struct xflow_actions *actions, tag_type *tags,
2499 uint16_t *nf_output_iface, void *wx_)
2501 struct wx *wx = wx_;
2504 /* Drop frames for reserved multicast addresses. */
2505 if (eth_addr_is_reserved(flow->dl_dst)) {
2509 /* Learn source MAC (but don't try to learn from revalidation). */
2510 if (packet != NULL) {
2511 tag_type rev_tag = mac_learning_learn(wx->ml, flow->dl_src,
2513 GRAT_ARP_LOCK_NONE);
2515 /* The log messages here could actually be useful in debugging,
2516 * so keep the rate limit relatively high. */
2517 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2518 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
2519 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
2520 tag_set_add(&wx->revalidate_set, rev_tag);
2524 /* Determine output port. */
2525 out_port = mac_learning_lookup_tag(wx->ml, flow->dl_dst, 0, tags,
2528 add_output_group_action(actions, WX_GROUP_FLOOD, nf_output_iface);
2529 } else if (out_port != flow->in_port) {
2530 xflow_actions_add(actions, XFLOWAT_OUTPUT)->output.port = out_port;
2531 *nf_output_iface = out_port;
2539 static const struct ofhooks default_ofhooks = {
2541 default_normal_ofhook_cb,