X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fdpif-netdev.c;h=5f7ce8ff2a87714a1dce75c450a3a07e2fe7838b;hb=5cc2c261d33f091ea45ff3de22737cf4deb724a2;hp=d7c3da028f2d44776845eb89ab5436cbdf98f6c1;hpb=89625d1efb32461071837b5e81c1238b4ca63387;p=openvswitch diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index d7c3da02..5f7ce8ff 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks. + * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ #include "poll-loop.h" #include "random.h" #include "shash.h" +#include "sset.h" #include "timeval.h" #include "util.h" #include "vlog.h" @@ -100,7 +101,7 @@ struct dp_netdev_port { int port_no; /* Index into dp_netdev's 'ports'. */ struct list node; /* Element in dp_netdev's 'port_list'. */ struct netdev *netdev; - bool internal; /* Internal port? */ + char *type; /* Port type as requested by user. */ }; /* A flow in dp_netdev's 'flow_table'. */ @@ -112,7 +113,7 @@ struct dp_netdev_flow { long long int used; /* Last used time, in monotonic msecs. */ long long int packet_count; /* Number of packets matched. */ long long int byte_count; /* Number of bytes matched. */ - ovs_be16 tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */ + uint8_t tcp_flags; /* Bitwise-OR of seen tcp_flags values. */ /* Actions. */ struct nlattr *actions; @@ -151,8 +152,6 @@ static void dp_netdev_execute_actions(struct dp_netdev *, const struct nlattr *actions, size_t actions_len); -static struct dpif_class dpif_dummy_class; - static struct dpif_netdev * dpif_netdev_cast(const struct dpif *dpif) { @@ -166,6 +165,17 @@ get_dp_netdev(const struct dpif *dpif) return dpif_netdev_cast(dpif)->dp; } +static int +dpif_netdev_enumerate(struct sset *all_dps) +{ + struct shash_node *node; + + SHASH_FOR_EACH(node, &dp_netdevs) { + sset_add(all_dps, node->name); + } + return 0; +} + static struct dpif * create_dpif_netdev(struct dp_netdev *dp) { @@ -309,28 +319,17 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type, { struct dp_netdev_port *port; struct netdev *netdev; - bool internal; + const char *open_type; int mtu; int error; /* XXX reject devices already in some dp_netdev. */ - if (type[0] == '\0' || !strcmp(type, "system") || !strcmp(type, "dummy")) { - internal = false; - } else if (!strcmp(type, "internal")) { - internal = true; - } else { - VLOG_WARN("%s: unsupported port type %s", devname, type); - return EINVAL; - } /* Open and validate network device. */ - if (dp->class == &dpif_dummy_class) { - type = "dummy"; - } else if (internal) { - type = "tap"; - } - - error = netdev_open(devname, type, &netdev); + open_type = (strcmp(type, "internal") ? type + : dp->class != &dpif_netdev_class ? "dummy" + : "tap"); + error = netdev_open(devname, open_type, &netdev); if (error) { return error; } @@ -354,7 +353,7 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type, port = xmalloc(sizeof *port); port->port_no = port_no; port->netdev = netdev; - port->internal = internal; + port->type = xstrdup(type); error = netdev_get_mtu(netdev, &mtu); if (!error) { @@ -374,7 +373,7 @@ choose_port(struct dpif *dpif, struct netdev *netdev) struct dp_netdev *dp = get_dp_netdev(dpif); int port_no; - if (dpif->dpif_class == &dpif_dummy_class) { + if (dpif->dpif_class != &dpif_netdev_class) { /* If the port name contains a number, try to assign that port number. * This can make writing unit tests easier because port numbers are * predictable. */ @@ -476,6 +475,7 @@ do_del_port(struct dp_netdev *dp, uint16_t port_no) name = xstrdup(netdev_get_name(port->netdev)); netdev_close(port->netdev); + free(port->type); free(name); free(port); @@ -488,8 +488,7 @@ answer_port_query(const struct dp_netdev_port *port, struct dpif_port *dpif_port) { dpif_port->name = xstrdup(netdev_get_name(port->netdev)); - dpif_port->type = xstrdup(port->internal ? "internal" - : netdev_get_type(port->netdev)); + dpif_port->type = xstrdup(port->type); dpif_port->port_no = port->port_no; } @@ -581,8 +580,7 @@ dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_, free(state->name); state->name = xstrdup(netdev_get_name(port->netdev)); dpif_port->name = state->name; - dpif_port->type = (char *) (port->internal ? "internal" - : netdev_get_type(port->netdev)); + dpif_port->type = port->type; dpif_port->port_no = port->port_no; state->port_no = port_no + 1; return 0; @@ -640,7 +638,7 @@ get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats) stats->n_packets = flow->packet_count; stats->n_bytes = flow->byte_count; stats->used = flow->used; - stats->tcp_flags = TCP_FLAGS(flow->tcp_ctl); + stats->tcp_flags = flow->tcp_flags; } static int @@ -715,10 +713,9 @@ set_flow_actions(struct dp_netdev_flow *flow, } static int -add_flow(struct dpif *dpif, const struct flow *key, - const struct nlattr *actions, size_t actions_len) +dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key, + const struct nlattr *actions, size_t actions_len) { - struct dp_netdev *dp = get_dp_netdev(dpif); struct dp_netdev_flow *flow; int error; @@ -741,7 +738,7 @@ clear_stats(struct dp_netdev_flow *flow) flow->used = 0; flow->packet_count = 0; flow->byte_count = 0; - flow->tcp_ctl = 0; + flow->tcp_flags = 0; } static int @@ -764,7 +761,8 @@ dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put) if (put->stats) { memset(put->stats, 0, sizeof *put->stats); } - return add_flow(dpif, &key, put->actions, put->actions_len); + return dp_netdev_flow_add(dp, &key, put->actions, + put->actions_len); } else { return EFBIG; } @@ -790,24 +788,22 @@ dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put) } static int -dpif_netdev_flow_del(struct dpif *dpif, - const struct nlattr *nl_key, size_t nl_key_len, - struct dpif_flow_stats *stats) +dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del) { struct dp_netdev *dp = get_dp_netdev(dpif); struct dp_netdev_flow *flow; struct flow key; int error; - error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key); + error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key); if (error) { return error; } flow = dp_netdev_lookup_flow(dp, &key); if (flow) { - if (stats) { - get_dpif_flow_stats(flow, stats); + if (del->stats) { + get_dpif_flow_stats(flow, del->stats); } dp_netdev_free_flow(dp, flow); return 0; @@ -950,7 +946,8 @@ find_nonempty_queue(struct dpif *dpif) } static int -dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall) +dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall, + struct ofpbuf *buf) { struct dp_netdev_queue *q = find_nonempty_queue(dpif); if (q) { @@ -958,6 +955,9 @@ dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall) *upcall = *u; free(u); + ofpbuf_uninit(buf); + *buf = *upcall->packet; + return 0; } else { return EAGAIN; @@ -989,10 +989,7 @@ dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key, flow->used = time_msec(); flow->packet_count++; flow->byte_count += packet->size; - if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) { - struct tcp_header *th = packet->l4; - flow->tcp_ctl |= th->tcp_ctl; - } + flow->tcp_flags |= packet_get_tcp_flags(packet, key); } static void @@ -1255,7 +1252,7 @@ dp_netdev_execute_actions(struct dp_netdev *dp, const struct dpif_class dpif_netdev_class = { "netdev", - NULL, /* enumerate */ + dpif_netdev_enumerate, dpif_netdev_open, dpif_netdev_close, dpif_netdev_destroy, @@ -1289,12 +1286,33 @@ const struct dpif_class dpif_netdev_class = { dpif_netdev_recv_purge, }; +static void +dpif_dummy_register__(const char *type) +{ + struct dpif_class *class; + + class = xmalloc(sizeof *class); + *class = dpif_netdev_class; + class->type = xstrdup(type); + dp_register_provider(class); +} + void -dpif_dummy_register(void) +dpif_dummy_register(bool override) { - if (!dpif_dummy_class.type) { - dpif_dummy_class = dpif_netdev_class; - dpif_dummy_class.type = "dummy"; - dp_register_provider(&dpif_dummy_class); + if (override) { + struct sset types; + const char *type; + + sset_init(&types); + dp_enumerate_types(&types); + SSET_FOR_EACH (type, &types) { + if (!dp_unregister_provider(type)) { + dpif_dummy_register__(type); + } + } + sset_destroy(&types); } + + dpif_dummy_register__("dummy"); }