]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-flows.c
test: Reverse the order of commands added by ON_EXIT macro
[mirror_ovs.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 #undef NDEBUG
19 #include "flow.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "classifier.h"
25 #include "ofpbuf.h"
26 #include "ofp-print.h"
27 #include "ofp-util.h"
28 #include "openflow/openflow.h"
29 #include "ovstest.h"
30 #include "pcap-file.h"
31 #include "timeval.h"
32 #include "util.h"
33 #include "openvswitch/vlog.h"
34
35 static void
36 test_flows_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
37 {
38 struct ofp10_match expected_match;
39 FILE *flows, *pcap;
40 int retval;
41 int n = 0, errors = 0;
42
43 set_program_name(argv[0]);
44
45 flows = fopen(argv[1], "rb");
46 if (!flows) {
47 ovs_fatal(errno, "failed to open %s", argv[1]);
48 }
49 pcap = fopen(argv[2], "rb");
50 if (!pcap) {
51 ovs_fatal(errno, "failed to open %s", argv[2]);
52 }
53
54 retval = ovs_pcap_read_header(pcap);
55 if (retval) {
56 ovs_fatal(retval > 0 ? retval : 0, "reading pcap header failed");
57 }
58
59 while (fread(&expected_match, sizeof expected_match, 1, flows)) {
60 struct ofpbuf *packet;
61 struct ofp10_match extracted_match;
62 struct match match;
63 struct flow flow;
64 n++;
65
66 retval = ovs_pcap_read(pcap, &packet, NULL);
67 if (retval == EOF) {
68 ovs_fatal(0, "unexpected end of file reading pcap file");
69 } else if (retval) {
70 ovs_fatal(retval, "error reading pcap file");
71 }
72
73 flow_extract(packet, NULL, &flow);
74 flow.in_port.ofp_port = u16_to_ofp(1);
75
76 match_wc_init(&match, &flow);
77 ofputil_match_to_ofp10_match(&match, &extracted_match);
78
79 if (memcmp(&expected_match, &extracted_match, sizeof expected_match)) {
80 char *exp_s = ofp10_match_to_string(&expected_match, 2);
81 char *got_s = ofp10_match_to_string(&extracted_match, 2);
82 errors++;
83 printf("mismatch on packet #%d (1-based).\n", n);
84 printf("Packet:\n");
85 ofp_print_packet(stdout, ofpbuf_data(packet), ofpbuf_size(packet));
86 ovs_hex_dump(stdout, ofpbuf_data(packet), ofpbuf_size(packet), 0, true);
87 match_print(&match);
88 printf("Expected flow:\n%s\n", exp_s);
89 printf("Actually extracted flow:\n%s\n", got_s);
90 ovs_hex_dump(stdout, &expected_match, sizeof expected_match, 0, false);
91 ovs_hex_dump(stdout, &extracted_match, sizeof extracted_match, 0, false);
92 printf("\n");
93 free(exp_s);
94 free(got_s);
95 }
96
97 ofpbuf_delete(packet);
98 }
99 printf("checked %d packets, %d errors\n", n, errors);
100 exit(errors != 0);
101 }
102
103 OVSTEST_REGISTER("test-flows", test_flows_main);