X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tests%2Ftest-classifier.c;h=f279bda1e7d8c75eb81eef02b52544cff8145acc;hb=81a76618be9ea195a1e4a881ba9591728891d10b;hp=45700ed9ee2db43cbed44691cc3a2d7da20d41b2;hpb=193eb87453463d45eb10a3d3adbffedc0a762d1f;p=openvswitch diff --git a/tests/test-classifier.c b/tests/test-classifier.c index 45700ed9..f279bda1 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, 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. @@ -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,30 +40,29 @@ #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 */ \ + /* member name name */ \ + /* ----------- -------- */ \ + CLS_FIELD(tun_id, TUN_ID) \ + CLS_FIELD(metadata, METADATA) \ + CLS_FIELD(nw_src, NW_SRC) \ + CLS_FIELD(nw_dst, NW_DST) \ + CLS_FIELD(in_port, IN_PORT) \ + CLS_FIELD(vlan_tci, VLAN_TCI) \ + CLS_FIELD(dl_type, DL_TYPE) \ + CLS_FIELD(tp_src, TP_SRC) \ + CLS_FIELD(tp_dst, TP_DST) \ + CLS_FIELD(dl_src, DL_SRC) \ + CLS_FIELD(dl_dst, DL_DST) \ + CLS_FIELD(nw_proto, NW_PROTO) \ + CLS_FIELD(nw_tos, NW_DSCP) /* Field indexes. * * (These are also indexed into struct classifier's 'tables' array.) */ enum { -#define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME, +#define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME, CLS_FIELDS #undef CLS_FIELD CLS_N_FIELDS @@ -72,15 +72,13 @@ 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. */ const char *name; /* Name (for debugging). */ }; static const struct cls_field cls_fields[CLS_N_FIELDS] = { -#define CLS_FIELD(WILDCARDS, MEMBER, NAME) \ +#define CLS_FIELD(MEMBER, NAME) \ { offsetof(struct flow, MEMBER), \ sizeof ((struct flow *)0)->MEMBER, \ - WILDCARDS, \ #NAME }, CLS_FIELDS #undef CLS_FIELD @@ -136,7 +134,6 @@ 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); for (i = 0; i < tcls->n_rules; i++) { const struct cls_rule *pos = &tcls->rules[i]->cls_rule; if (cls_rule_equal(pos, &rule->cls_rule)) { @@ -186,28 +183,54 @@ match(const struct cls_rule *wild, const struct flow *fixed) int f_idx; 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_idx == CLS_F_IDX_NW_SRC) { + eq = !((fixed->nw_src ^ wild->match.flow.nw_src) + & wild->match.wc.masks.nw_src); + } else if (f_idx == CLS_F_IDX_NW_DST) { + eq = !((fixed->nw_dst ^ wild->match.flow.nw_dst) + & wild->match.wc.masks.nw_dst); + } else if (f_idx == CLS_F_IDX_TP_SRC) { + eq = !((fixed->tp_src ^ wild->match.flow.tp_src) + & wild->match.wc.masks.tp_src); + } else if (f_idx == CLS_F_IDX_TP_DST) { + eq = !((fixed->tp_dst ^ wild->match.flow.tp_dst) + & wild->match.wc.masks.tp_dst); + } else if (f_idx == CLS_F_IDX_DL_SRC) { + eq = eth_addr_equal_except(fixed->dl_src, wild->match.flow.dl_src, + wild->match.wc.masks.dl_src); + } else if (f_idx == CLS_F_IDX_DL_DST) { + eq = eth_addr_equal_except(fixed->dl_dst, wild->match.flow.dl_dst, + wild->match.wc.masks.dl_dst); + } else if (f_idx == CLS_F_IDX_VLAN_TCI) { + eq = !((fixed->vlan_tci ^ wild->match.flow.vlan_tci) + & wild->match.wc.masks.vlan_tci); + } else if (f_idx == CLS_F_IDX_TUN_ID) { + eq = !((fixed->tun_id ^ wild->match.flow.tun_id) + & wild->match.wc.masks.tun_id); + } else if (f_idx == CLS_F_IDX_METADATA) { + eq = !((fixed->metadata ^ wild->match.flow.metadata) + & wild->match.wc.masks.metadata); + } else if (f_idx == CLS_F_IDX_NW_DSCP) { + eq = !((fixed->nw_tos ^ wild->match.flow.nw_tos) & + (wild->match.wc.masks.nw_tos & IP_DSCP_MASK)); + } else if (f_idx == CLS_F_IDX_NW_PROTO) { + eq = !((fixed->nw_proto ^ wild->match.flow.nw_proto) + & wild->match.wc.masks.nw_proto); + } else if (f_idx == CLS_F_IDX_DL_TYPE) { + eq = !((fixed->dl_type ^ wild->match.flow.dl_type) + & wild->match.wc.masks.dl_type); + } else if (f_idx == CLS_F_IDX_IN_PORT) { + eq = !((fixed->in_port ^ wild->match.flow.in_port) + & wild->match.wc.masks.in_port); + } 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; } @@ -233,8 +256,9 @@ tcls_delete_matches(struct tcls *cls, const struct cls_rule *target) for (i = 0; i < cls->n_rules; ) { struct test_rule *pos = cls->rules[i]; - if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc) - && match(target, &pos->cls_rule.flow)) { + if (!flow_wildcards_has_extra(&pos->cls_rule.match.wc, + &target->match.wc) + && match(target, &pos->cls_rule.match.flow)) { tcls_remove(cls, pos); } else { i++; @@ -246,10 +270,14 @@ 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 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_be64 tun_id_values[] = { + 0, + CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) }; +static ovs_be64 metadata_values[] = { + 0, + CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) }; +static uint16_t in_port_values[] = { 1, OFPP_LOCAL }; +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), @@ -259,8 +287,8 @@ static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 }, { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } }; static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; -static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP }; -static uint8_t nw_tos_values[] = { 49, 0 }; +static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP }; +static uint8_t nw_dscp_values[] = { 48, 0 }; static void *values[CLS_N_FIELDS][2]; @@ -270,14 +298,14 @@ init_values(void) values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0]; values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1]; + values[CLS_F_IDX_METADATA][0] = &metadata_values[0]; + values[CLS_F_IDX_METADATA][1] = &metadata_values[1]; + 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]; @@ -297,8 +325,8 @@ init_values(void) values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0]; values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1]; - values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0]; - values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1]; + values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0]; + values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1]; values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0]; values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1]; @@ -310,30 +338,29 @@ init_values(void) #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values) #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values) #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values) +#define N_METADATA_VALUES ARRAY_SIZE(metadata_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) #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values) #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values) #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values) -#define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values) +#define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values) #define N_FLOW_VALUES (N_NW_SRC_VALUES * \ 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 * \ N_DL_SRC_VALUES * \ N_DL_DST_VALUES * \ N_NW_PROTO_VALUES * \ - N_NW_TOS_VALUES) + N_NW_DSCP_VALUES) static unsigned int get_value(unsigned int *x, unsigned n_values) @@ -356,13 +383,13 @@ compare_classifiers(struct classifier *cls, struct tcls *tcls) unsigned int x; x = rand () % N_FLOW_VALUES; + memset(&flow, 0, sizeof flow); flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)]; 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.metadata = metadata_values[get_value(&x, N_METADATA_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)]; @@ -371,7 +398,7 @@ compare_classifiers(struct classifier *cls, struct tcls *tcls) memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)], ETH_ADDR_LEN); flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)]; - flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)]; + flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)]; cr0 = classifier_lookup(cls, &flow); cr1 = tcls_lookup(tcls, &flow); @@ -405,7 +432,6 @@ check_tables(const struct classifier *cls, int n_tables, int n_rules, int n_dups) { const struct cls_table *table; - struct flow_wildcards exact_wc; struct test_rule *test_rule; struct cls_cursor cursor; int found_tables = 0; @@ -413,7 +439,6 @@ check_tables(const struct classifier *cls, int found_dups = 0; int found_rules2 = 0; - flow_wildcards_init_exact(&exact_wc); HMAP_FOR_EACH (table, hmap_node, &cls->tables) { const struct cls_rule *head; @@ -451,27 +476,49 @@ 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; + struct match match; - wildcards = 0; - memset(&flow, 0, sizeof flow); + match_init_catchall(&match); 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 *) &match.flow + f->ofs, + values[f_idx][value_idx], f->len); + + if (f_idx == CLS_F_IDX_NW_SRC) { + match.wc.masks.nw_src = htonl(UINT32_MAX); + } else if (f_idx == CLS_F_IDX_NW_DST) { + match.wc.masks.nw_dst = htonl(UINT32_MAX); + } else if (f_idx == CLS_F_IDX_TP_SRC) { + match.wc.masks.tp_src = htons(UINT16_MAX); + } else if (f_idx == CLS_F_IDX_TP_DST) { + match.wc.masks.tp_dst = htons(UINT16_MAX); + } else if (f_idx == CLS_F_IDX_DL_SRC) { + memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN); + } else if (f_idx == CLS_F_IDX_DL_DST) { + memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN); + } else if (f_idx == CLS_F_IDX_VLAN_TCI) { + match.wc.masks.vlan_tci = htons(UINT16_MAX); + } else if (f_idx == CLS_F_IDX_TUN_ID) { + match.wc.masks.tun_id = htonll(UINT64_MAX); + } else if (f_idx == CLS_F_IDX_METADATA) { + match.wc.masks.metadata = htonll(UINT64_MAX); + } else if (f_idx == CLS_F_IDX_NW_DSCP) { + match.wc.masks.nw_tos |= IP_DSCP_MASK; + } else if (f_idx == CLS_F_IDX_NW_PROTO) { + match.wc.masks.nw_proto = UINT8_MAX; + } else if (f_idx == CLS_F_IDX_DL_TYPE) { + match.wc.masks.dl_type = htons(UINT16_MAX); + } else if (f_idx == CLS_F_IDX_IN_PORT) { + match.wc.masks.in_port = UINT16_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); + cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX); return rule; } @@ -527,7 +574,7 @@ test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) tcls_init(&tcls); tcls_rule = tcls_insert(&tcls, rule); - assert(!classifier_insert(&cls, &rule->cls_rule)); + classifier_insert(&cls, &rule->cls_rule); check_tables(&cls, 1, 1, 0); compare_classifiers(&cls, &tcls); @@ -563,7 +610,7 @@ test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) classifier_init(&cls); tcls_init(&tcls); tcls_insert(&tcls, rule1); - assert(!classifier_insert(&cls, &rule1->cls_rule)); + classifier_insert(&cls, &rule1->cls_rule); check_tables(&cls, 1, 1, 0); compare_classifiers(&cls, &tcls); tcls_destroy(&tcls); @@ -571,7 +618,7 @@ test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) tcls_init(&tcls); tcls_insert(&tcls, rule2); assert(test_rule_from_cls_rule( - classifier_insert(&cls, &rule2->cls_rule)) == rule1); + classifier_replace(&cls, &rule2->cls_rule)) == rule1); free(rule1); check_tables(&cls, 1, 1, 0); compare_classifiers(&cls, &tcls); @@ -682,7 +729,7 @@ test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED) tcls_rules[j] = tcls_insert(&tcls, rules[j]); displaced_rule = test_rule_from_cls_rule( - classifier_insert(&cls, &rules[j]->cls_rule)); + classifier_replace(&cls, &rules[j]->cls_rule)); if (pri_rules[pris[j]] >= 0) { int k = pri_rules[pris[j]]; assert(displaced_rule != NULL); @@ -726,7 +773,7 @@ count_ones(unsigned long int x) int n = 0; while (x) { - x &= x - 1; + x = zero_rightmost_1bit(x); n++; } @@ -782,7 +829,7 @@ test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) rules[i] = make_rule(wcf, priority, value_pats[i]); tcls_rules[i] = tcls_insert(&tcls, rules[i]); - assert(!classifier_insert(&cls, &rules[i]->cls_rule)); + classifier_insert(&cls, &rules[i]->cls_rule); check_tables(&cls, 1, i + 1, 0); compare_classifiers(&cls, &tcls); @@ -840,7 +887,7 @@ test_many_rules_in_n_tables(int n_tables) int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1); rule = make_rule(wcf, priority, value_pat); tcls_insert(&tcls, rule); - assert(!classifier_insert(&cls, &rule->cls_rule)); + classifier_insert(&cls, &rule->cls_rule); check_tables(&cls, -1, i + 1, -1); compare_classifiers(&cls, &tcls); }