~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to tests/test-classifier.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
 
2
 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
34
34
#include "flow.h"
35
35
#include "ofp-util.h"
36
36
#include "packets.h"
 
37
#include "random.h"
37
38
#include "unaligned.h"
38
39
 
39
40
#undef NDEBUG
240
241
            eq = !((fixed->dl_type ^ wild.flow.dl_type)
241
242
                   & wild.wc.masks.dl_type);
242
243
        } else if (f_idx == CLS_F_IDX_IN_PORT) {
243
 
            eq = !((fixed->in_port ^ wild.flow.in_port)
244
 
                   & wild.wc.masks.in_port);
 
244
            eq = !((fixed->in_port.ofp_port
 
245
                    ^ wild.flow.in_port.ofp_port)
 
246
                   & wild.wc.masks.in_port.ofp_port);
245
247
        } else {
246
248
            NOT_REACHED();
247
249
        }
298
300
static ovs_be64 metadata_values[] = {
299
301
    0,
300
302
    CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
301
 
static uint16_t in_port_values[] = { 1, OFPP_LOCAL };
 
303
static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
302
304
static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
303
305
static ovs_be16 dl_type_values[]
304
306
            = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
404
406
        struct flow flow;
405
407
        unsigned int x;
406
408
 
407
 
        x = rand () % N_FLOW_VALUES;
 
409
        x = random_range(N_FLOW_VALUES);
408
410
        memset(&flow, 0, sizeof flow);
409
411
        flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
410
412
        flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
411
413
        flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
412
414
        flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
413
 
        flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
 
415
        flow.in_port.ofp_port = in_port_values[get_value(&x,
 
416
                                                   N_IN_PORT_VALUES)];
414
417
        flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
415
418
        flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
416
419
        flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
422
425
        flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
423
426
        flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
424
427
 
425
 
        cr0 = classifier_lookup(cls, &flow);
 
428
        cr0 = classifier_lookup(cls, &flow, NULL);
426
429
        cr1 = tcls_lookup(tcls, &flow);
427
430
        assert((cr0 == NULL) == (cr1 == NULL));
428
431
        if (cr0 != NULL) {
463
466
 
464
467
    HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
465
468
        const struct cls_rule *head;
 
469
        unsigned int max_priority = 0;
 
470
        unsigned int max_count = 0;
466
471
 
467
472
        assert(!hmap_is_empty(&table->rules));
468
473
 
471
476
            unsigned int prev_priority = UINT_MAX;
472
477
            const struct cls_rule *rule;
473
478
 
 
479
            if (head->priority > max_priority) {
 
480
                max_priority = head->priority;
 
481
                max_count = 1;
 
482
            } else if (head->priority == max_priority) {
 
483
                ++max_count;
 
484
            }
 
485
 
474
486
            found_rules++;
475
487
            LIST_FOR_EACH (rule, list, &head->list) {
476
488
                assert(rule->priority < prev_priority);
 
489
                assert(rule->priority <= table->max_priority);
 
490
 
477
491
                prev_priority = rule->priority;
478
492
                found_rules++;
479
493
                found_dups++;
480
494
                assert(classifier_find_rule_exactly(cls, rule) == rule);
481
495
            }
482
496
        }
 
497
        assert(table->max_priority == max_priority);
 
498
        assert(table->max_count == max_count);
483
499
    }
484
500
 
485
501
    assert(found_tables == hmap_count(&cls->tables));
533
549
        } else if (f_idx == CLS_F_IDX_DL_TYPE) {
534
550
            match.wc.masks.dl_type = htons(UINT16_MAX);
535
551
        } else if (f_idx == CLS_F_IDX_IN_PORT) {
536
 
            match.wc.masks.in_port = UINT16_MAX;
 
552
            match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
537
553
        } else {
538
554
            NOT_REACHED();
539
555
        }
566
582
shuffle(unsigned int *p, size_t n)
567
583
{
568
584
    for (; n > 1; n--, p++) {
569
 
        unsigned int *q = &p[rand() % n];
 
585
        unsigned int *q = &p[random_range(n)];
570
586
        unsigned int tmp = *p;
571
587
        *p = *q;
572
588
        *q = tmp;
577
593
shuffle_u32s(uint32_t *p, size_t n)
578
594
{
579
595
    for (; n > 1; n--, p++) {
580
 
        uint32_t *q = &p[rand() % n];
 
596
        uint32_t *q = &p[random_range(n)];
581
597
        uint32_t tmp = *p;
582
598
        *p = *q;
583
599
        *q = tmp;
866
882
        int i;
867
883
 
868
884
        do {
869
 
            wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
 
885
            wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
870
886
            value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
871
887
        } while ((1 << count_ones(value_mask)) < N_RULES);
872
888
 
874
890
        tcls_init(&tcls);
875
891
 
876
892
        for (i = 0; i < N_RULES; i++) {
877
 
            unsigned int priority = rand();
 
893
            unsigned int priority = random_uint32();
878
894
 
879
895
            do {
880
 
                value_pats[i] = rand() & value_mask;
 
896
                value_pats[i] = random_uint32() & value_mask;
881
897
            } while (array_contains(value_pats, i, value_pats[i]));
882
898
 
883
899
            rules[i] = make_rule(wcf, priority, value_pats[i]);
915
931
    assert(n_tables < 10);
916
932
    for (i = 0; i < n_tables; i++) {
917
933
        do {
918
 
            wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
 
934
            wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
919
935
        } while (array_contains(wcfs, i, wcfs[i]));
920
936
    }
921
937
 
924
940
        struct classifier cls;
925
941
        struct tcls tcls;
926
942
 
927
 
        srand(iteration);
 
943
        random_set_seed(iteration + 1);
928
944
        for (i = 0; i < MAX_RULES; i++) {
929
945
            priorities[i] = i * 129;
930
946
        }
936
952
        for (i = 0; i < MAX_RULES; i++) {
937
953
            struct test_rule *rule;
938
954
            unsigned int priority = priorities[i];
939
 
            int wcf = wcfs[rand() % n_tables];
940
 
            int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
 
955
            int wcf = wcfs[random_range(n_tables)];
 
956
            int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
941
957
            rule = make_rule(wcf, priority, value_pat);
942
958
            tcls_insert(&tcls, rule);
943
959
            classifier_insert(&cls, &rule->cls_rule);
950
966
            struct test_rule *target;
951
967
            struct cls_cursor cursor;
952
968
 
953
 
            target = clone_rule(tcls.rules[rand() % tcls.n_rules]);
 
969
            target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
954
970
 
955
971
            cls_cursor_init(&cursor, &cls, &target->cls_rule);
956
972
            CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
989
1005
        { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
990
1006
          0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
991
1007
 
992
 
    return values[random_uint32() % ARRAY_SIZE(values)];
 
1008
    return values[random_range(ARRAY_SIZE(values))];
993
1009
}
994
1010
 
995
1011
static bool
1266
1282
        minimask_init(&minimask2, &mask2);
1267
1283
 
1268
1284
        minimask_combine(&minicombined, &minimask, &minimask2, storage);
1269
 
        flow_wildcards_combine(&combined, &mask, &mask2);
 
1285
        flow_wildcards_and(&combined, &mask, &mask2);
1270
1286
        minimask_expand(&minicombined, &combined2);
1271
1287
        assert(flow_wildcards_equal(&combined, &combined2));
1272
1288