Implement OpenFlow statistics in switches and in dpctl.
[sliver-openvswitch.git] / lib / dpif.c
index 6bd6fef..63611e9 100644 (file)
@@ -1,22 +1,34 @@
-/* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
+/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
+ * Junior University
+ * 
+ * We are making the OpenFlow specification and associated documentation
+ * (Software) available for public use and benefit with the expectation
+ * that others will use, modify and enhance the Software and contribute
+ * those enhancements back to the community. However, since we would
+ * like to make the Software available for broadest use, with as few
+ * restrictions as possible permission is hereby granted, free of
+ * charge, to any person obtaining a copy of this Software to deal in
+ * the Software under the copyrights without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ * 
+ * The name and trademarks of copyright holder(s) may NOT be used in
+ * advertising or publicity pertaining to the Software or any
+ * derivatives without specific, written prior permission.
  */
 
 #include "dpif.h"
 #include <string.h>
 
 #include "buffer.h"
-#include "mac.h"
 #include "netlink.h"
 #include "ofp-print.h"
 #include "openflow-netlink.h"
 #include "openflow.h"
+#include "packets.h"
 #include "util.h"
 #include "xtoxll.h"
 
@@ -246,193 +258,6 @@ dpif_del_port(struct dpif *dp, const char *netdev)
     return send_mgmt_command(dp, DP_GENL_C_DEL_PORT, netdev);
 }
 
-/* Prints a description of 'dp' to stdout.  Returns 0 if successful, otherwise
- * a positive errno value. */
-int
-dpif_show(struct dpif *dp) 
-{
-    static const struct nl_policy show_policy[] = {
-        [DP_GENL_A_DP_INFO] = { .type = NL_A_UNSPEC,
-                                .min_len = sizeof(struct ofp_data_hello),
-                                .max_len = SIZE_MAX },
-    };
-
-    struct buffer request, *reply;
-    struct nlattr *attrs[ARRAY_SIZE(show_policy)];
-    struct ofp_data_hello *odh;
-    int retval;
-    size_t len;
-
-    buffer_init(&request, 0);
-    nl_msg_put_genlmsghdr(&request, dp->sock, 0, openflow_family,
-                          NLM_F_REQUEST, DP_GENL_C_SHOW_DP, 1);
-    nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp->dp_idx);
-    retval = nl_sock_transact(dp->sock, &request, &reply);
-    buffer_uninit(&request);
-    if (retval) {
-        return retval;
-    }
-    if (!nl_policy_parse(reply, show_policy, attrs,
-                         ARRAY_SIZE(show_policy))) {
-        buffer_delete(reply);
-        return EPROTO;
-    }
-
-    odh = (void *) nl_attr_get(attrs[DP_GENL_A_DP_INFO]);
-    if (odh->header.version != OFP_VERSION
-        || odh->header.type != OFPT_DATA_HELLO) {
-        VLOG_ERR("bad show query response (%"PRIu8",%"PRIu8")",
-                 odh->header.version, odh->header.type);
-        buffer_delete(reply);
-        return EPROTO;
-    }
-
-    len = nl_attr_get_size(attrs[DP_GENL_A_DP_INFO]);
-    ofp_print_data_hello(stdout, odh, len, 1);
-
-    return retval;
-}
-
-static const struct nl_policy table_policy[] = {
-    [DP_GENL_A_NUMTABLES] = { .type = NL_A_U32 },
-    [DP_GENL_A_TABLE] = { .type = NL_A_UNSPEC },
-};
-
-/* Writes a description of 'dp''s tables to stdout.  Returns 0 if successful,
- * otherwise a positive errno value. */
-int
-dpif_dump_tables(struct dpif *dp) 
-{
-    struct buffer request, *reply;
-    struct nlattr *attrs[ARRAY_SIZE(table_policy)];
-    const struct ofp_table *tables;
-    int n_tables;
-    int i;
-    int retval;
-
-    buffer_init(&request, 0);
-    nl_msg_put_genlmsghdr(&request, dp->sock, 0, openflow_family,
-                          NLM_F_REQUEST, DP_GENL_C_QUERY_TABLE, 1);
-    nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp->dp_idx);
-    retval = nl_sock_transact(dp->sock, &request, &reply);
-    buffer_uninit(&request);
-    if (retval) {
-        return retval;
-    }
-    if (!nl_policy_parse(reply, table_policy, attrs,
-                         ARRAY_SIZE(table_policy))) {
-        buffer_delete(reply);
-        return EPROTO;
-    }
-
-    tables = nl_attr_get(attrs[DP_GENL_A_TABLE]);
-    n_tables = (nl_attr_get_size(attrs[DP_GENL_A_TABLE])
-                / sizeof(struct ofp_table));
-    n_tables = MIN(n_tables, nl_attr_get_u32(attrs[DP_GENL_A_NUMTABLES]));
-    for (i = 0; i < n_tables; i++) {
-        const struct ofp_table *ot = &tables[i];
-        if (ot->header.version != 1 || ot->header.type != OFPT_TABLE) {
-            VLOG_DBG("bad table query response (%"PRIu8",%"PRIu8")",
-                     ot->header.version, ot->header.type);
-            retval = EPROTO;
-            break;
-        }
-
-        ofp_print_table(stdout, ot);
-        fprintf(stdout,"\n");
-    }
-    buffer_delete(reply);
-
-    return retval;
-}
-
-static const struct nl_policy flow_policy[] = {
-    [DP_GENL_A_TABLEIDX] = { .type = NL_A_U16 },
-    [DP_GENL_A_NUMFLOWS] = { .type = NL_A_U32 },
-    [DP_GENL_A_FLOW] = { .type = NL_A_UNSPEC },
-};
-
-struct _dump_ofp_flow_mod
-{
-    struct ofp_flow_mod ofm;
-    struct ofp_action   oa;
-};
-
-/* Writes a description of flows in the given 'table' in 'dp' to stdout.  If
- * 'match' is null, all flows in the table are written; otherwise, only
- * matching flows are written.  Returns 0 if successful, otherwise a positive
- * errno value. */
-int
-dpif_dump_flows(struct dpif *dp, int table, struct ofp_match *match)
-{
-    struct buffer request, *reply;
-    struct ofp_flow_mod *ofm;
-    int retval;
-
-    buffer_init(&request, 0);
-    nl_msg_put_genlmsghdr(&request, dp->sock, 0, openflow_family, NLM_F_REQUEST,
-                          DP_GENL_C_QUERY_FLOW, 1);
-    nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp->dp_idx);
-    nl_msg_put_u16(&request, DP_GENL_A_TABLEIDX, table);
-    ofm = nl_msg_put_unspec_uninit(&request, DP_GENL_A_FLOW, sizeof *ofm);
-    memset(ofm, 0, sizeof *ofm);
-    ofm->header.version = 1;
-    ofm->header.type = OFPT_FLOW_MOD;
-    ofm->header.length = htons(sizeof ofm);
-    if (match) {
-        ofm->match = *match;
-    } else {
-        ofm->match.wildcards = htons(OFPFW_ALL);
-    }
-    retval = nl_sock_transact(dp->sock, &request, &reply);
-    buffer_uninit(&request);
-    if (retval) {
-        return retval;
-    }
-
-    for (;;) {
-        struct nlattr *attrs[ARRAY_SIZE(flow_policy)];
-        const struct _dump_ofp_flow_mod *flows, *ofm;
-        int n_flows;
-
-        if (!nl_policy_parse(reply, flow_policy, attrs,
-                             ARRAY_SIZE(flow_policy))) {
-            buffer_delete(reply);
-            return EPROTO;
-        }
-        n_flows = (nl_attr_get_size(attrs[DP_GENL_A_FLOW])
-                   / sizeof(struct ofp_flow_mod));
-        n_flows = MIN(n_flows, nl_attr_get_u32(attrs[DP_GENL_A_NUMFLOWS]));
-        if (n_flows <= 0) {
-            break;
-        }
-
-        flows = nl_attr_get(attrs[DP_GENL_A_FLOW]);
-        for (ofm = flows; ofm < &flows[n_flows]; ofm++) {
-            if (ofm->ofm.header.version != 1){
-                VLOG_DBG("recv_dp_flow incorrect version");
-                buffer_delete(reply);
-                return EPROTO;
-            } else if (ofm->ofm.header.type != OFPT_FLOW_MOD) {
-                VLOG_DBG("recv_fp_flow bad return message type");
-                buffer_delete(reply);
-                return EPROTO;
-            }
-
-            ofp_print_flow_mod(stdout, &ofm->ofm, 
-                    sizeof(struct ofp_flow_mod), 1);
-            putc('\n', stdout);
-        }
-
-        buffer_delete(reply);
-        retval = nl_sock_recv(dp->sock, &reply, true);
-        if (retval) {
-            return retval;
-        }
-    }
-    return 0;
-}
-
 /* Tells dp to send num_packets up through netlink for benchmarking*/
 int
 dpif_benchmark_nl(struct dpif *dp, uint32_t num_packets, uint32_t packet_size)