Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / tests / test-flows.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "flow.h"
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "classifier.h"
23 #include "openflow/openflow.h"
24 #include "timeval.h"
25 #include "ofpbuf.h"
26 #include "ofp-print.h"
27 #include "ofp-util.h"
28 #include "pcap-file.h"
29 #include "util.h"
30 #include "vlog.h"
31 #include "ovstest.h"
32
33 #undef NDEBUG
34 #include <assert.h>
35
36 static void
37 test_flows_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
38 {
39     struct ofp10_match expected_match;
40     FILE *flows, *pcap;
41     int retval;
42     int n = 0, errors = 0;
43
44     set_program_name(argv[0]);
45
46     flows = stdin;
47     pcap = fdopen(3, "rb");
48     if (!pcap) {
49         ovs_fatal(errno, "failed to open fd 3 for reading");
50     }
51
52     retval = ovs_pcap_read_header(pcap);
53     if (retval) {
54         ovs_fatal(retval > 0 ? retval : 0, "reading pcap header failed");
55     }
56
57     while (fread(&expected_match, sizeof expected_match, 1, flows)) {
58         struct ofpbuf *packet;
59         struct ofp10_match extracted_match;
60         struct match match;
61         struct flow flow;
62         n++;
63
64         retval = ovs_pcap_read(pcap, &packet, NULL);
65         if (retval == EOF) {
66             ovs_fatal(0, "unexpected end of file reading pcap file");
67         } else if (retval) {
68             ovs_fatal(retval, "error reading pcap file");
69         }
70
71         flow_extract(packet, NULL, &flow);
72         flow.in_port.ofp_port = u16_to_ofp(1);
73
74         match_wc_init(&match, &flow);
75         ofputil_match_to_ofp10_match(&match, &extracted_match);
76
77         if (memcmp(&expected_match, &extracted_match, sizeof expected_match)) {
78             char *exp_s = ofp10_match_to_string(&expected_match, 2);
79             char *got_s = ofp10_match_to_string(&extracted_match, 2);
80             errors++;
81             printf("mismatch on packet #%d (1-based).\n", n);
82             printf("Packet:\n");
83             ofp_print_packet(stdout, ofpbuf_data(packet), ofpbuf_size(packet));
84             ovs_hex_dump(stdout, ofpbuf_data(packet), ofpbuf_size(packet), 0, true);
85             match_print(&match);
86             printf("Expected flow:\n%s\n", exp_s);
87             printf("Actually extracted flow:\n%s\n", got_s);
88             ovs_hex_dump(stdout, &expected_match, sizeof expected_match, 0, false);
89             ovs_hex_dump(stdout, &extracted_match, sizeof extracted_match, 0, false);
90             printf("\n");
91             free(exp_s);
92             free(got_s);
93         }
94
95         ofpbuf_delete(packet);
96     }
97     printf("checked %d packets, %d errors\n", n, errors);
98     exit(errors != 0);
99 }
100
101 OVSTEST_REGISTER("test-flows", test_flows_main);