X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tests%2Ftest-classifier.c;h=e1f93a09d95afb21d056f15bcfb6cce5cbf2f689;hb=36956a7d33c9ee204fcb184484a5aaacbd9ecef8;hp=6cecef75f3932ffd1b5d555fd07289b63420346e;hpb=cd10ed7fb4fb5b1e3ed8e3fae1fd115c7ae96e77;p=openvswitch diff --git a/tests/test-classifier.c b/tests/test-classifier.c index 6cecef75..e1f93a09 100644 --- a/tests/test-classifier.c +++ b/tests/test-classifier.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Nicira Networks. + * Copyright (c) 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ #include "byte-order.h" #include "command-line.h" #include "flow.h" +#include "ofp-util.h" #include "packets.h" #include "unaligned.h" @@ -39,24 +40,22 @@ #include /* Fields in a rule. */ -#define CLS_FIELDS \ - /* struct flow all-caps */ \ - /* wildcard bit(s) member name name */ \ - /* ----------------- ----------- -------- */ \ - CLS_FIELD(NXFW_TUN_ID, tun_id, TUN_ID) \ - CLS_FIELD(OFPFW_NW_SRC_MASK, nw_src, NW_SRC) \ - CLS_FIELD(OFPFW_NW_DST_MASK, nw_dst, NW_DST) \ - CLS_FIELD(OFPFW_IN_PORT, in_port, IN_PORT) \ - CLS_FIELD(OFPFW_DL_VLAN, dl_vlan, DL_VLAN) \ - CLS_FIELD(OFPFW_DL_TYPE, dl_type, DL_TYPE) \ - CLS_FIELD(OFPFW_TP_SRC, tp_src, TP_SRC) \ - CLS_FIELD(OFPFW_TP_DST, tp_dst, TP_DST) \ - CLS_FIELD(OFPFW_DL_SRC, dl_src, DL_SRC) \ - CLS_FIELD(OFPFW_DL_DST | FWW_ETH_MCAST, \ - dl_dst, DL_DST) \ - CLS_FIELD(OFPFW_NW_PROTO, nw_proto, NW_PROTO) \ - CLS_FIELD(OFPFW_DL_VLAN_PCP, dl_vlan_pcp, DL_VLAN_PCP) \ - CLS_FIELD(OFPFW_NW_TOS, nw_tos, NW_TOS) +#define CLS_FIELDS \ + /* struct flow all-caps */ \ + /* FWW_* bit(s) member name name */ \ + /* -------------------------- ----------- -------- */ \ + CLS_FIELD(0, tun_id, TUN_ID) \ + CLS_FIELD(0, nw_src, NW_SRC) \ + CLS_FIELD(0, nw_dst, NW_DST) \ + CLS_FIELD(FWW_IN_PORT, in_port, IN_PORT) \ + CLS_FIELD(0, vlan_tci, VLAN_TCI) \ + CLS_FIELD(FWW_DL_TYPE, dl_type, DL_TYPE) \ + CLS_FIELD(FWW_TP_SRC, tp_src, TP_SRC) \ + CLS_FIELD(FWW_TP_DST, tp_dst, TP_DST) \ + CLS_FIELD(FWW_DL_SRC, dl_src, DL_SRC) \ + CLS_FIELD(FWW_DL_DST | FWW_ETH_MCAST, dl_dst, DL_DST) \ + CLS_FIELD(FWW_NW_PROTO, nw_proto, NW_PROTO) \ + CLS_FIELD(FWW_NW_TOS, nw_tos, NW_TOS) /* Field indexes. * @@ -72,7 +71,7 @@ enum { struct cls_field { int ofs; /* Offset in struct flow. */ int len; /* Length in bytes. */ - uint32_t wildcards; /* OFPFW_* bit or bits for this field. */ + flow_wildcards_t wildcards; /* FWW_* bit or bits for this field. */ const char *name; /* Name (for debugging). */ }; @@ -136,14 +135,12 @@ tcls_insert(struct tcls *tcls, const struct test_rule *rule) { size_t i; - assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX); + assert(!flow_wildcards_is_exact(&rule->cls_rule.wc) + || rule->cls_rule.priority == UINT_MAX); for (i = 0; i < tcls->n_rules; i++) { const struct cls_rule *pos = &tcls->rules[i]->cls_rule; - if (pos->priority == rule->cls_rule.priority - && pos->wc.wildcards == rule->cls_rule.wc.wildcards - && flow_equal(&pos->flow, &rule->cls_rule.flow)) { - /* Exact match. - * XXX flow_equal should ignore wildcarded fields */ + if (cls_rule_equal(pos, &rule->cls_rule)) { + /* Exact match. */ free(tcls->rules[i]); tcls->rules[i] = xmemdup(rule, sizeof *rule); return tcls->rules[i]; @@ -190,27 +187,29 @@ match(const struct cls_rule *wild, const struct flow *fixed) for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) { const struct cls_field *f = &cls_fields[f_idx]; - void *wild_field = (char *) &wild->flow + f->ofs; - void *fixed_field = (char *) fixed + f->ofs; - - if ((wild->wc.wildcards & f->wildcards) == f->wildcards || - !memcmp(wild_field, fixed_field, f->len)) { - /* Definite match. */ - continue; + bool eq; + + if (f->wildcards) { + void *wild_field = (char *) &wild->flow + f->ofs; + void *fixed_field = (char *) fixed + f->ofs; + eq = ((wild->wc.wildcards & f->wildcards) == f->wildcards + || !memcmp(wild_field, fixed_field, f->len)); + } else if (f_idx == CLS_F_IDX_NW_SRC) { + eq = !((fixed->nw_src ^ wild->flow.nw_src) & wild->wc.nw_src_mask); + } else if (f_idx == CLS_F_IDX_NW_DST) { + eq = !((fixed->nw_dst ^ wild->flow.nw_dst) & wild->wc.nw_dst_mask); + } else if (f_idx == CLS_F_IDX_VLAN_TCI) { + eq = !((fixed->vlan_tci ^ wild->flow.vlan_tci) + & wild->wc.vlan_tci_mask); + } else if (f_idx == CLS_F_IDX_TUN_ID) { + eq = !((fixed->tun_id ^ wild->flow.tun_id) & wild->wc.tun_id_mask); + } else { + NOT_REACHED(); } - if (wild->wc.wildcards & f->wildcards) { - uint32_t test = get_unaligned_u32(wild_field); - uint32_t ip = get_unaligned_u32(fixed_field); - int shift = (f_idx == CLS_F_IDX_NW_SRC - ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT); - uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift); - if (!((test ^ ip) & mask)) { - continue; - } + if (!eq) { + return false; } - - return false; } return true; } @@ -249,10 +248,11 @@ static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001), CONSTANT_HTONL(0xc0a04455) }; static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002), CONSTANT_HTONL(0xc0a04455) }; -static ovs_be32 tun_id_values[] = { 0, 0xffff0000 }; +static ovs_be64 tun_id_values[] = { + 0, + CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) }; static uint16_t in_port_values[] = { 1, ODPP_LOCAL }; -static ovs_be16 dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) }; -static uint8_t dl_vlan_pcp_values[] = { 7, 0 }; +static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) }; static ovs_be16 dl_type_values[] = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) }; static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362), @@ -276,11 +276,8 @@ init_values(void) values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0]; values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1]; - values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0]; - values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1]; - - values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0]; - values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1]; + values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0]; + values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1]; values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0]; values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1]; @@ -314,8 +311,7 @@ init_values(void) #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values) #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values) #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values) -#define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values) -#define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values) +#define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values) #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values) #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values) #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values) @@ -328,8 +324,7 @@ init_values(void) N_NW_DST_VALUES * \ N_TUN_ID_VALUES * \ N_IN_PORT_VALUES * \ - N_DL_VLAN_VALUES * \ - N_DL_VLAN_PCP_VALUES * \ + N_VLAN_TCI_VALUES * \ N_DL_TYPE_VALUES * \ N_TP_SRC_VALUES * \ N_TP_DST_VALUES * \ @@ -363,9 +358,7 @@ compare_classifiers(struct classifier *cls, struct tcls *tcls) flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)]; flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)]; flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)]; - flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)]; - flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x, - N_DL_VLAN_PCP_VALUES)]; + flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)]; flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)]; flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)]; flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)]; @@ -383,27 +376,23 @@ compare_classifiers(struct classifier *cls, struct tcls *tcls) const struct test_rule *tr0 = test_rule_from_cls_rule(cr0); const struct test_rule *tr1 = test_rule_from_cls_rule(cr1); - assert(flow_equal(&cr0->flow, &cr1->flow)); - assert(cr0->wc.wildcards == cr1->wc.wildcards); - assert(cr0->priority == cr1->priority); - /* Skip nw_src_mask and nw_dst_mask, because they are derived - * members whose values are used only for optimization. */ + assert(cls_rule_equal(cr0, cr1)); assert(tr0->aux == tr1->aux); } } } -static void -free_rule(struct cls_rule *cls_rule, void *cls) -{ - classifier_remove(cls, cls_rule); - free(test_rule_from_cls_rule(cls_rule)); -} - static void destroy_classifier(struct classifier *cls) { - classifier_for_each(cls, free_rule, cls); + struct test_rule *rule, *next_rule; + struct cls_cursor cursor; + + cls_cursor_init(&cursor, cls, NULL); + CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) { + classifier_remove(cls, &rule->cls_rule); + free(rule); + } classifier_destroy(cls); } @@ -413,9 +402,12 @@ check_tables(const struct classifier *cls, { const struct cls_table *table; struct flow_wildcards exact_wc; + struct test_rule *test_rule; + struct cls_cursor cursor; int found_tables = 0; int found_rules = 0; int found_dups = 0; + int found_rules2 = 0; flow_wildcards_init_exact(&exact_wc); HMAP_FOR_EACH (table, hmap_node, &cls->tables) { @@ -443,33 +435,42 @@ check_tables(const struct classifier *cls, assert(n_tables == -1 || n_tables == hmap_count(&cls->tables)); assert(n_rules == -1 || found_rules == n_rules); assert(n_dups == -1 || found_dups == n_dups); + + cls_cursor_init(&cursor, cls, NULL); + CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) { + found_rules2++; + } + assert(found_rules == found_rules2); } static struct test_rule * make_rule(int wc_fields, unsigned int priority, int value_pat) { const struct cls_field *f; - struct flow_wildcards wc; struct test_rule *rule; - uint32_t wildcards; - struct flow flow; - wildcards = 0; - memset(&flow, 0, sizeof flow); + rule = xzalloc(sizeof *rule); + cls_rule_init_catchall(&rule->cls_rule, wc_fields ? priority : UINT_MAX); for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) { int f_idx = f - cls_fields; - if (wc_fields & (1u << f_idx)) { - wildcards |= f->wildcards; + int value_idx = (value_pat & (1u << f_idx)) != 0; + memcpy((char *) &rule->cls_rule.flow + f->ofs, + values[f_idx][value_idx], f->len); + + if (f->wildcards) { + rule->cls_rule.wc.wildcards &= ~f->wildcards; + } else if (f_idx == CLS_F_IDX_NW_SRC) { + rule->cls_rule.wc.nw_src_mask = htonl(UINT32_MAX); + } else if (f_idx == CLS_F_IDX_NW_DST) { + rule->cls_rule.wc.nw_dst_mask = htonl(UINT32_MAX); + } else if (f_idx == CLS_F_IDX_VLAN_TCI) { + rule->cls_rule.wc.vlan_tci_mask = htons(UINT16_MAX); + } else if (f_idx == CLS_F_IDX_TUN_ID) { + rule->cls_rule.wc.tun_id_mask = htonll(UINT64_MAX); } else { - int value_idx = (value_pat & (1u << f_idx)) != 0; - memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len); + NOT_REACHED(); } } - - rule = xzalloc(sizeof *rule); - flow_wildcards_init(&wc, wildcards); - cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority, - &rule->cls_rule); return rule; } @@ -844,13 +845,22 @@ test_many_rules_in_n_tables(int n_tables) } while (!classifier_is_empty(&cls)) { - struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules], - sizeof(struct test_rule)); - classifier_for_each_match(&cls, &rule->cls_rule, free_rule, &cls); - tcls_delete_matches(&tcls, &rule->cls_rule); + struct test_rule *rule, *next_rule; + struct test_rule *target; + struct cls_cursor cursor; + + target = xmemdup(tcls.rules[rand() % tcls.n_rules], + sizeof(struct test_rule)); + + cls_cursor_init(&cursor, &cls, &target->cls_rule); + CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) { + classifier_remove(&cls, &rule->cls_rule); + free(rule); + } + tcls_delete_matches(&tcls, &target->cls_rule); compare_classifiers(&cls, &tcls); check_tables(&cls, -1, -1, -1); - free(rule); + free(target); } destroy_classifier(&cls);