2 * Copyright (c) 2009, 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.
25 #include <netinet/in.h>
26 #include <sys/socket.h>
30 #include <sys/ioctl.h>
39 #include "xflow-util.h"
40 #include "ofp-print.h"
43 #include "poll-loop.h"
48 #include "xfif-provider.h"
50 VLOG_DEFINE_THIS_MODULE(xfif_netdev)
52 /* Configuration parameters. */
53 enum { N_QUEUES = 2 }; /* Number of queues for xfif_recv(). */
54 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
55 enum { N_GROUPS = 16 }; /* Number of port groups. */
56 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
57 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
59 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
60 * headers to be aligned on a 4-byte boundary. */
61 enum { XF_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
63 /* Datapath based on the network device interface from netdev.h. */
70 bool drop_frags; /* Drop all IP fragments, if true. */
71 struct ovs_queue queues[N_QUEUES]; /* Messages queued for xfif_recv(). */
72 struct hmap flow_table; /* Flow table. */
73 struct xflow_port_group groups[N_GROUPS];
76 long long int n_frags; /* Number of dropped IP fragments. */
77 long long int n_hit; /* Number of flow table matches. */
78 long long int n_missed; /* Number of flow table misses. */
79 long long int n_lost; /* Number of misses not passed to client. */
83 struct xf_netdev_port *ports[MAX_PORTS];
84 struct list port_list;
88 /* A port in a netdev-based datapath. */
89 struct xf_netdev_port {
90 int port_no; /* Index into xf_netdev's 'ports'. */
91 struct list node; /* Element in xf_netdev's 'port_list'. */
92 struct netdev *netdev;
93 bool internal; /* Internal port (as XFLOW_PORT_INTERNAL)? */
96 /* A flow in xf_netdev's 'flow_table'. */
97 struct xf_netdev_flow {
98 struct hmap_node node; /* Element in xf_netdev's 'flow_table'. */
102 struct timespec used; /* Last used time. */
103 long long int packet_count; /* Number of packets matched. */
104 long long int byte_count; /* Number of bytes matched. */
105 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
108 union xflow_action *actions;
109 unsigned int n_actions;
112 /* Interface to netdev-based datapath. */
115 struct xf_netdev *xf;
117 unsigned int xf_serial;
120 /* All netdev-based datapaths. */
121 static struct xf_netdev *xf_netdevs[256];
122 struct list xf_netdev_list = LIST_INITIALIZER(&xf_netdev_list);
123 enum { N_XF_NETDEVS = ARRAY_SIZE(xf_netdevs) };
125 /* Maximum port MTU seen so far. */
126 static int max_mtu = ETH_PAYLOAD_MAX;
128 static int get_port_by_number(struct xf_netdev *, uint16_t port_no,
129 struct xf_netdev_port **portp);
130 static int get_port_by_name(struct xf_netdev *, const char *devname,
131 struct xf_netdev_port **portp);
132 static void xf_netdev_free(struct xf_netdev *);
133 static void xf_netdev_flow_flush(struct xf_netdev *);
134 static int do_add_port(struct xf_netdev *, const char *devname, uint16_t flags,
136 static int do_del_port(struct xf_netdev *, uint16_t port_no);
137 static int xf_netdev_output_control(struct xf_netdev *, const struct ofpbuf *,
138 int queue_no, int port_no, uint32_t arg);
139 static int xf_netdev_execute_actions(struct xf_netdev *,
140 struct ofpbuf *, struct xflow_key *,
141 const union xflow_action *, int n);
143 static struct xfif_netdev *
144 xfif_netdev_cast(const struct xfif *xfif)
146 xfif_assert_class(xfif, &xfif_netdev_class);
147 return CONTAINER_OF(xfif, struct xfif_netdev, xfif);
150 static struct xf_netdev *
151 get_xf_netdev(const struct xfif *xfif)
153 return xfif_netdev_cast(xfif)->xf;
157 name_to_xf_idx(const char *name)
159 if (!strncmp(name, "xf", 2) && isdigit((unsigned char)name[2])) {
160 int xf_idx = atoi(name + 2);
161 if (xf_idx >= 0 && xf_idx < N_XF_NETDEVS) {
168 static struct xf_netdev *
169 find_xf_netdev(const char *name)
174 xf_idx = name_to_xf_idx(name);
176 return xf_netdevs[xf_idx];
179 for (i = 0; i < N_XF_NETDEVS; i++) {
180 struct xf_netdev *xf = xf_netdevs[i];
182 struct xf_netdev_port *port;
183 if (!get_port_by_name(xf, name, &port)) {
192 create_xfif_netdev(struct xf_netdev *xf)
194 struct xfif_netdev *xfif;
199 xfname = xasprintf("xf%d", xf->xf_idx);
200 xfif = xmalloc(sizeof *xfif);
201 xfif_init(&xfif->xfif, &xfif_netdev_class, xfname, xf->xf_idx, xf->xf_idx);
203 xfif->listen_mask = 0;
204 xfif->xf_serial = xf->serial;
211 create_xf_netdev(const char *name, int xf_idx, struct xfif **xfifp)
213 struct xf_netdev *xf;
217 if (xf_netdevs[xf_idx]) {
221 /* Create datapath. */
222 xf_netdevs[xf_idx] = xf = xzalloc(sizeof *xf);
223 list_push_back(&xf_netdev_list, &xf->node);
226 xf->drop_frags = false;
227 for (i = 0; i < N_QUEUES; i++) {
228 queue_init(&xf->queues[i]);
230 hmap_init(&xf->flow_table);
231 for (i = 0; i < N_GROUPS; i++) {
232 xf->groups[i].ports = NULL;
233 xf->groups[i].n_ports = 0;
234 xf->groups[i].group = i;
236 list_init(&xf->port_list);
237 error = do_add_port(xf, name, XFLOW_PORT_INTERNAL, XFLOWP_LOCAL);
243 *xfifp = create_xfif_netdev(xf);
248 xfif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
252 if (find_xf_netdev(name)) {
255 int xf_idx = name_to_xf_idx(name);
257 return create_xf_netdev(name, xf_idx, xfifp);
259 /* Scan for unused xf_idx number. */
260 for (xf_idx = 0; xf_idx < N_XF_NETDEVS; xf_idx++) {
261 int error = create_xf_netdev(name, xf_idx, xfifp);
262 if (error != EBUSY) {
267 /* All datapath numbers in use. */
272 struct xf_netdev *xf = find_xf_netdev(name);
274 *xfifp = create_xfif_netdev(xf);
283 xf_netdev_free(struct xf_netdev *xf)
287 xf_netdev_flow_flush(xf);
288 while (xf->n_ports > 0) {
289 struct xf_netdev_port *port = CONTAINER_OF(
290 xf->port_list.next, struct xf_netdev_port, node);
291 do_del_port(xf, port->port_no);
293 for (i = 0; i < N_QUEUES; i++) {
294 queue_destroy(&xf->queues[i]);
296 hmap_destroy(&xf->flow_table);
297 for (i = 0; i < N_GROUPS; i++) {
298 free(xf->groups[i].ports);
300 xf_netdevs[xf->xf_idx] = NULL;
301 list_remove(&xf->node);
306 xfif_netdev_close(struct xfif *xfif)
308 struct xf_netdev *xf = get_xf_netdev(xfif);
309 assert(xf->open_cnt > 0);
310 if (--xf->open_cnt == 0 && xf->destroyed) {
317 xfif_netdev_destroy(struct xfif *xfif)
319 struct xf_netdev *xf = get_xf_netdev(xfif);
320 xf->destroyed = true;
325 xfif_netdev_get_stats(const struct xfif *xfif, struct xflow_stats *stats)
327 struct xf_netdev *xf = get_xf_netdev(xfif);
328 memset(stats, 0, sizeof *stats);
329 stats->n_flows = hmap_count(&xf->flow_table);
330 stats->cur_capacity = hmap_capacity(&xf->flow_table);
331 stats->max_capacity = MAX_FLOWS;
332 stats->n_ports = xf->n_ports;
333 stats->max_ports = MAX_PORTS;
334 stats->max_groups = N_GROUPS;
335 stats->n_frags = xf->n_frags;
336 stats->n_hit = xf->n_hit;
337 stats->n_missed = xf->n_missed;
338 stats->n_lost = xf->n_lost;
339 stats->max_miss_queue = MAX_QUEUE_LEN;
340 stats->max_action_queue = MAX_QUEUE_LEN;
345 xfif_netdev_get_drop_frags(const struct xfif *xfif, bool *drop_fragsp)
347 struct xf_netdev *xf = get_xf_netdev(xfif);
348 *drop_fragsp = xf->drop_frags;
353 xfif_netdev_set_drop_frags(struct xfif *xfif, bool drop_frags)
355 struct xf_netdev *xf = get_xf_netdev(xfif);
356 xf->drop_frags = drop_frags;
361 do_add_port(struct xf_netdev *xf, const char *devname, uint16_t flags,
364 bool internal = (flags & XFLOW_PORT_INTERNAL) != 0;
365 struct xf_netdev_port *port;
366 struct netdev_options netdev_options;
367 struct netdev *netdev;
371 /* XXX reject devices already in some xf_netdev. */
373 /* Open and validate network device. */
374 memset(&netdev_options, 0, sizeof netdev_options);
375 netdev_options.name = devname;
376 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
378 netdev_options.type = "tap";
381 error = netdev_open(&netdev_options, &netdev);
385 /* XXX reject loopback devices */
386 /* XXX reject non-Ethernet devices */
388 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
390 netdev_close(netdev);
394 port = xmalloc(sizeof *port);
395 port->port_no = port_no;
396 port->netdev = netdev;
397 port->internal = internal;
399 netdev_get_mtu(netdev, &mtu);
404 list_push_back(&xf->port_list, &port->node);
405 xf->ports[port_no] = port;
413 xfif_netdev_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
416 struct xf_netdev *xf = get_xf_netdev(xfif);
419 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
420 if (!xf->ports[port_no]) {
422 return do_add_port(xf, devname, flags, port_no);
429 xfif_netdev_port_del(struct xfif *xfif, uint16_t port_no)
431 struct xf_netdev *xf = get_xf_netdev(xfif);
432 return port_no == XFLOWP_LOCAL ? EINVAL : do_del_port(xf, port_no);
436 is_valid_port_number(uint16_t port_no)
438 return port_no < MAX_PORTS;
442 get_port_by_number(struct xf_netdev *xf,
443 uint16_t port_no, struct xf_netdev_port **portp)
445 if (!is_valid_port_number(port_no)) {
449 *portp = xf->ports[port_no];
450 return *portp ? 0 : ENOENT;
455 get_port_by_name(struct xf_netdev *xf,
456 const char *devname, struct xf_netdev_port **portp)
458 struct xf_netdev_port *port;
460 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
461 if (!strcmp(netdev_get_name(port->netdev), devname)) {
470 do_del_port(struct xf_netdev *xf, uint16_t port_no)
472 struct xf_netdev_port *port;
476 error = get_port_by_number(xf, port_no, &port);
481 list_remove(&port->node);
482 xf->ports[port->port_no] = NULL;
486 name = xstrdup(netdev_get_name(port->netdev));
487 netdev_close(port->netdev);
496 answer_port_query(const struct xf_netdev_port *port, struct xflow_port *xflow_port)
498 memset(xflow_port, 0, sizeof *xflow_port);
499 ovs_strlcpy(xflow_port->devname, netdev_get_name(port->netdev),
500 sizeof xflow_port->devname);
501 xflow_port->port = port->port_no;
502 xflow_port->flags = port->internal ? XFLOW_PORT_INTERNAL : 0;
506 xfif_netdev_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
507 struct xflow_port *xflow_port)
509 struct xf_netdev *xf = get_xf_netdev(xfif);
510 struct xf_netdev_port *port;
513 error = get_port_by_number(xf, port_no, &port);
515 answer_port_query(port, xflow_port);
521 xfif_netdev_port_query_by_name(const struct xfif *xfif, const char *devname,
522 struct xflow_port *xflow_port)
524 struct xf_netdev *xf = get_xf_netdev(xfif);
525 struct xf_netdev_port *port;
528 error = get_port_by_name(xf, devname, &port);
530 answer_port_query(port, xflow_port);
536 xf_netdev_free_flow(struct xf_netdev *xf, struct xf_netdev_flow *flow)
538 hmap_remove(&xf->flow_table, &flow->node);
544 xf_netdev_flow_flush(struct xf_netdev *xf)
546 struct xf_netdev_flow *flow, *next;
548 HMAP_FOR_EACH_SAFE (flow, next, struct xf_netdev_flow, node,
550 xf_netdev_free_flow(xf, flow);
555 xfif_netdev_flow_flush(struct xfif *xfif)
557 struct xf_netdev *xf = get_xf_netdev(xfif);
558 xf_netdev_flow_flush(xf);
563 xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
565 struct xf_netdev *xf = get_xf_netdev(xfif);
566 struct xf_netdev_port *port;
570 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
571 struct xflow_port *xflow_port = &ports[i];
575 answer_port_query(port, xflow_port);
582 xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
584 struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
585 if (xfif->xf_serial != xfif->xf->serial) {
586 xfif->xf_serial = xfif->xf->serial;
594 xfif_netdev_port_poll_wait(const struct xfif *xfif_)
596 struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
597 if (xfif->xf_serial != xfif->xf->serial) {
598 poll_immediate_wake();
603 get_port_group(const struct xfif *xfif, int group_no,
604 struct xflow_port_group **groupp)
606 struct xf_netdev *xf = get_xf_netdev(xfif);
608 if (group_no >= 0 && group_no < N_GROUPS) {
609 *groupp = &xf->groups[group_no];
618 xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
619 uint16_t ports[], int n)
621 struct xflow_port_group *group;
628 error = get_port_group(xfif, group_no, &group);
630 memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
631 return group->n_ports;
638 xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
639 const uint16_t ports[], int n)
641 struct xflow_port_group *group;
644 if (n < 0 || n > MAX_PORTS) {
648 error = get_port_group(xfif, group_no, &group);
651 group->ports = xmemdup(ports, n * sizeof *group->ports);
653 group->group = group_no;
658 static struct xf_netdev_flow *
659 xf_netdev_lookup_flow(const struct xf_netdev *xf,
660 const struct xflow_key *key)
662 struct xf_netdev_flow *flow;
664 HMAP_FOR_EACH_WITH_HASH (flow, struct xf_netdev_flow, node,
665 xflow_key_hash(key, 0), &xf->flow_table) {
666 if (xflow_key_equal(&flow->key, key)) {
674 answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
675 struct xflow_flow *xflow_flow)
678 xflow_flow->key = flow->key;
679 xflow_flow->stats.n_packets = flow->packet_count;
680 xflow_flow->stats.n_bytes = flow->byte_count;
681 xflow_flow->stats.used_sec = flow->used.tv_sec;
682 xflow_flow->stats.used_nsec = flow->used.tv_nsec;
683 xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
684 xflow_flow->stats.reserved = 0;
685 xflow_flow->stats.error = 0;
686 if (xflow_flow->n_actions > 0) {
687 unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
688 memcpy(xflow_flow->actions, flow->actions,
689 n * sizeof *xflow_flow->actions);
690 xflow_flow->n_actions = flow->n_actions;
693 if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
698 xflow_flow->stats.error = ENOENT;
703 xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
705 struct xf_netdev *xf = get_xf_netdev(xfif);
708 for (i = 0; i < n; i++) {
709 struct xflow_flow *xflow_flow = &flows[i];
710 answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
711 xflow_flow->flags, xflow_flow);
717 xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
723 for (i = 0; i < n_actions; i++) {
724 const union xflow_action *a = &actions[i];
727 if (a->output.port >= MAX_PORTS) {
732 case XFLOWAT_OUTPUT_GROUP:
734 if (a->output_group.group >= N_GROUPS) {
739 case XFLOWAT_CONTROLLER:
742 case XFLOWAT_SET_DL_TCI:
744 if (a->dl_tci.mask != htons(VLAN_VID_MASK)
745 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
746 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
749 if (a->dl_tci.tci & ~a->dl_tci.mask){
754 case XFLOWAT_SET_NW_TOS:
756 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
761 case XFLOWAT_STRIP_VLAN:
762 case XFLOWAT_SET_DL_SRC:
763 case XFLOWAT_SET_DL_DST:
764 case XFLOWAT_SET_NW_SRC:
765 case XFLOWAT_SET_NW_DST:
766 case XFLOWAT_SET_TP_SRC:
767 case XFLOWAT_SET_TP_DST:
779 set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
785 if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
788 error = xfif_netdev_validate_actions(xflow_flow->actions,
789 xflow_flow->n_actions, &mutates);
794 n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
795 flow->actions = xrealloc(flow->actions, n_bytes);
796 flow->n_actions = xflow_flow->n_actions;
797 memcpy(flow->actions, xflow_flow->actions, n_bytes);
802 add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
804 struct xf_netdev *xf = get_xf_netdev(xfif);
805 struct xf_netdev_flow *flow;
808 flow = xzalloc(sizeof *flow);
809 flow->key = xflow_flow->key;
811 error = set_flow_actions(flow, xflow_flow);
817 hmap_insert(&xf->flow_table, &flow->node,
818 xflow_key_hash(&flow->key, 0));
823 clear_stats(struct xf_netdev_flow *flow)
825 flow->used.tv_sec = 0;
826 flow->used.tv_nsec = 0;
827 flow->packet_count = 0;
828 flow->byte_count = 0;
833 xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
835 struct xf_netdev *xf = get_xf_netdev(xfif);
836 struct xf_netdev_flow *flow;
838 flow = xf_netdev_lookup_flow(xf, &put->flow.key);
840 if (put->flags & XFLOWPF_CREATE) {
841 if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
842 return add_flow(xfif, &put->flow);
850 if (put->flags & XFLOWPF_MODIFY) {
851 int error = set_flow_actions(flow, &put->flow);
852 if (!error && put->flags & XFLOWPF_ZERO_STATS) {
864 xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
866 struct xf_netdev *xf = get_xf_netdev(xfif);
867 struct xf_netdev_flow *flow;
869 flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
871 answer_flow_query(flow, 0, xflow_flow);
872 xf_netdev_free_flow(xf, flow);
880 xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
882 struct xf_netdev *xf = get_xf_netdev(xfif);
883 struct xf_netdev_flow *flow;
887 HMAP_FOR_EACH (flow, struct xf_netdev_flow, node, &xf->flow_table) {
891 answer_flow_query(flow, 0, &flows[i++]);
893 return hmap_count(&xf->flow_table);
897 xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
898 const union xflow_action actions[], int n_actions,
899 const struct ofpbuf *packet)
901 struct xf_netdev *xf = get_xf_netdev(xfif);
904 struct xflow_key key;
908 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
912 error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
918 /* We need a deep copy of 'packet' since we're going to modify its
920 ofpbuf_init(©, XF_NETDEV_HEADROOM + packet->size);
921 copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
922 ofpbuf_put(©, packet->data, packet->size);
924 /* We still need a shallow copy of 'packet', even though we won't
925 * modify its data, because flow_extract() modifies packet->l2, etc.
926 * We could probably get away with modifying those but it's more polite
930 flow_extract(©, 0, in_port, &flow);
931 xflow_key_from_flow(&key, &flow);
932 error = xf_netdev_execute_actions(xf, ©, &key, actions, n_actions);
934 ofpbuf_uninit(©);
940 xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
942 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
943 *listen_mask = xfif_netdev->listen_mask;
948 xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
950 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
951 if (!(listen_mask & ~XFLOWL_ALL)) {
952 xfif_netdev->listen_mask = listen_mask;
959 static struct ovs_queue *
960 find_nonempty_queue(struct xfif *xfif)
962 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
963 struct xf_netdev *xf = get_xf_netdev(xfif);
964 int mask = xfif_netdev->listen_mask;
967 for (i = 0; i < N_QUEUES; i++) {
968 struct ovs_queue *q = &xf->queues[i];
969 if (q->n && mask & (1u << i)) {
977 xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
979 struct ovs_queue *q = find_nonempty_queue(xfif);
981 *bufp = queue_pop_head(q);
989 xfif_netdev_recv_wait(struct xfif *xfif)
991 struct ovs_queue *q = find_nonempty_queue(xfif);
993 poll_immediate_wake();
995 /* No messages ready to be received, and xf_wait() will ensure that we
996 * wake up to queue new messages, so there is nothing to do. */
1001 xf_netdev_flow_used(struct xf_netdev_flow *flow,
1002 const struct xflow_key *key,
1003 const struct ofpbuf *packet)
1005 time_timespec(&flow->used);
1006 flow->packet_count++;
1007 flow->byte_count += packet->size;
1008 if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
1009 struct tcp_header *th = packet->l4;
1010 flow->tcp_ctl |= th->tcp_ctl;
1015 xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
1016 struct ofpbuf *packet)
1018 struct xf_netdev_flow *flow;
1019 struct xflow_key key;
1022 if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
1026 xflow_key_from_flow(&key, &f);
1028 flow = xf_netdev_lookup_flow(xf, &key);
1030 xf_netdev_flow_used(flow, &key, packet);
1031 xf_netdev_execute_actions(xf, packet, &key,
1032 flow->actions, flow->n_actions);
1036 xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
1043 struct ofpbuf packet;
1044 struct xf_netdev *xf;
1046 ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
1047 LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1048 struct xf_netdev_port *port;
1050 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1053 /* Reset packet contents. */
1054 packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
1057 error = netdev_recv(port->netdev, &packet);
1059 xf_netdev_port_input(xf, port, &packet);
1060 } else if (error != EAGAIN) {
1061 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1062 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1063 netdev_get_name(port->netdev), strerror(error));
1067 ofpbuf_uninit(&packet);
1071 xf_netdev_wait(void)
1073 struct xf_netdev *xf;
1075 LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1076 struct xf_netdev_port *port;
1077 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1078 netdev_recv_wait(port->netdev);
1084 /* Modify or add a 802.1Q header in 'packet' according to 'a'. */
1086 xf_netdev_set_dl_tci(struct ofpbuf *packet, struct xflow_key *key,
1087 const struct xflow_action_dl_tci *a)
1089 struct vlan_eth_header *veh;
1093 veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1095 /* Insert new 802.1Q header. */
1096 struct eth_header *eh = packet->l2;
1097 struct vlan_eth_header tmp;
1098 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1099 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1100 tmp.veth_type = htons(ETH_TYPE_VLAN);
1101 tmp.veth_tci = htons(a->tci);
1102 tmp.veth_next_type = eh->eth_type;
1104 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1105 memcpy(veh, &tmp, sizeof tmp);
1106 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1109 key->dl_tci = veh->veth_tci | htons(XFLOW_TCI_PRESENT);
1113 xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
1115 struct vlan_eth_header *veh = packet->l2;
1116 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1117 struct eth_header tmp;
1119 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1120 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1121 tmp.eth_type = veh->veth_next_type;
1123 packet->size -= VLAN_HEADER_LEN;
1124 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1125 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1126 memcpy(packet->data, &tmp, sizeof tmp);
1128 key->dl_tci = htons(0);
1133 xf_netdev_set_dl_src(struct ofpbuf *packet,
1134 const uint8_t dl_addr[ETH_ADDR_LEN])
1136 struct eth_header *eh = packet->l2;
1137 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1141 xf_netdev_set_dl_dst(struct ofpbuf *packet,
1142 const uint8_t dl_addr[ETH_ADDR_LEN])
1144 struct eth_header *eh = packet->l2;
1145 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1149 xf_netdev_set_nw_addr(struct ofpbuf *packet, const struct xflow_key *key,
1150 const struct xflow_action_nw_addr *a)
1152 if (key->dl_type == htons(ETH_TYPE_IP)) {
1153 struct ip_header *nh = packet->l3;
1156 field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1157 if (key->nw_proto == IP_TYPE_TCP) {
1158 struct tcp_header *th = packet->l4;
1159 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1160 } else if (key->nw_proto == IP_TYPE_UDP) {
1161 struct udp_header *uh = packet->l4;
1163 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1164 if (!uh->udp_csum) {
1165 uh->udp_csum = 0xffff;
1169 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1170 *field = a->nw_addr;
1175 xf_netdev_set_nw_tos(struct ofpbuf *packet, const struct xflow_key *key,
1176 const struct xflow_action_nw_tos *a)
1178 if (key->dl_type == htons(ETH_TYPE_IP)) {
1179 struct ip_header *nh = packet->l3;
1180 uint8_t *field = &nh->ip_tos;
1182 /* Set the DSCP bits and preserve the ECN bits. */
1183 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1185 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1186 htons((uint16_t)a->nw_tos));
1192 xf_netdev_set_tp_port(struct ofpbuf *packet, const struct xflow_key *key,
1193 const struct xflow_action_tp_port *a)
1195 if (key->dl_type == htons(ETH_TYPE_IP)) {
1197 if (key->nw_proto == IPPROTO_TCP) {
1198 struct tcp_header *th = packet->l4;
1199 field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1200 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1201 *field = a->tp_port;
1202 } else if (key->nw_proto == IPPROTO_UDP) {
1203 struct udp_header *uh = packet->l4;
1204 field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1205 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1206 *field = a->tp_port;
1214 xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
1217 struct xf_netdev_port *p = xf->ports[out_port];
1219 netdev_send(p->netdev, packet);
1224 xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
1225 struct ofpbuf *packet)
1227 struct xflow_port_group *g = &xf->groups[group];
1230 for (i = 0; i < g->n_ports; i++) {
1231 uint16_t out_port = g->ports[i];
1232 if (out_port != in_port) {
1233 xf_netdev_output_port(xf, packet, out_port);
1239 xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
1240 int queue_no, int port_no, uint32_t arg)
1242 struct ovs_queue *q = &xf->queues[queue_no];
1243 struct xflow_msg *header;
1247 if (q->n >= MAX_QUEUE_LEN) {
1252 msg_size = sizeof *header + packet->size;
1253 msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
1254 header = ofpbuf_put_uninit(msg, sizeof *header);
1255 ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
1256 header->type = queue_no;
1257 header->length = msg_size;
1258 header->port = port_no;
1260 ofpbuf_put(msg, packet->data, packet->size);
1261 queue_push_tail(q, msg);
1267 xf_netdev_execute_actions(struct xf_netdev *xf,
1268 struct ofpbuf *packet, struct xflow_key *key,
1269 const union xflow_action *actions, int n_actions)
1272 for (i = 0; i < n_actions; i++) {
1273 const union xflow_action *a = &actions[i];
1276 case XFLOWAT_OUTPUT:
1277 xf_netdev_output_port(xf, packet, a->output.port);
1280 case XFLOWAT_OUTPUT_GROUP:
1281 xf_netdev_output_group(xf, a->output_group.group, key->in_port,
1285 case XFLOWAT_CONTROLLER:
1286 xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
1287 key->in_port, a->controller.arg);
1290 case XFLOWAT_SET_DL_TCI:
1291 xf_netdev_set_dl_tci(packet, key, &a->dl_tci);
1294 case XFLOWAT_STRIP_VLAN:
1295 xf_netdev_strip_vlan(packet, key);
1298 case XFLOWAT_SET_DL_SRC:
1299 xf_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1302 case XFLOWAT_SET_DL_DST:
1303 xf_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1306 case XFLOWAT_SET_NW_SRC:
1307 case XFLOWAT_SET_NW_DST:
1308 xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
1311 case XFLOWAT_SET_NW_TOS:
1312 xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
1315 case XFLOWAT_SET_TP_SRC:
1316 case XFLOWAT_SET_TP_DST:
1317 xf_netdev_set_tp_port(packet, key, &a->tp_port);
1324 const struct xfif_class xfif_netdev_class = {
1328 NULL, /* enumerate */
1331 NULL, /* get_all_names */
1332 xfif_netdev_destroy,
1333 xfif_netdev_get_stats,
1334 xfif_netdev_get_drop_frags,
1335 xfif_netdev_set_drop_frags,
1336 xfif_netdev_port_add,
1337 xfif_netdev_port_del,
1338 xfif_netdev_port_query_by_number,
1339 xfif_netdev_port_query_by_name,
1340 xfif_netdev_port_list,
1341 xfif_netdev_port_poll,
1342 xfif_netdev_port_poll_wait,
1343 xfif_netdev_port_group_get,
1344 xfif_netdev_port_group_set,
1345 xfif_netdev_flow_get,
1346 xfif_netdev_flow_put,
1347 xfif_netdev_flow_del,
1348 xfif_netdev_flow_flush,
1349 xfif_netdev_flow_list,
1350 xfif_netdev_execute,
1351 xfif_netdev_recv_get_mask,
1352 xfif_netdev_recv_set_mask,
1353 NULL, /* get_sflow_probability */
1354 NULL, /* set_sflow_probability */
1355 NULL, /* queue_to_priority */
1357 xfif_netdev_recv_wait,