Catalli's threaded switch
[sliver-openvswitch.git] / tests / test-flows.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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 "openflow/openflow.h"
23 #include "timeval.h"
24 #include "ofpbuf.h"
25 #include "ofp-print.h"
26 #include "pcap.h"
27 #include "util.h"
28 #include "vlog.h"
29
30 #undef NDEBUG
31 #include <assert.h>
32
33 int
34 main(int argc OVS_UNUSED, char *argv[])
35 {
36     struct ofp_match expected_match;
37     FILE *flows, *pcap;
38     int retval;
39     int n = 0, errors = 0;
40
41     set_program_name(argv[0]);
42
43     flows = stdin;
44     pcap = fdopen(3, "rb");
45     if (!pcap) {
46         ovs_fatal(errno, "failed to open fd 3 for reading");
47     }
48
49     retval = pcap_read_header(pcap);
50     if (retval) {
51         ovs_fatal(retval > 0 ? retval : 0, "reading pcap header failed");
52     }
53
54     while (fread(&expected_match, sizeof expected_match, 1, flows)) {
55         struct ofpbuf *packet;
56         struct ofp_match extracted_match;
57         flow_t flow;
58
59         n++;
60
61         retval = pcap_read(pcap, &packet);
62         if (retval == EOF) {
63             ovs_fatal(0, "unexpected end of file reading pcap file");
64         } else if (retval) {
65             ovs_fatal(retval, "error reading pcap file");
66         }
67
68         flow_extract(packet, 0, 1, &flow);
69         flow_to_match(&flow, 0, false, &extracted_match);
70
71         if (memcmp(&expected_match, &extracted_match, sizeof expected_match)) {
72             char *exp_s = ofp_match_to_string(&expected_match, 2);
73             char *got_s = ofp_match_to_string(&extracted_match, 2);
74             errors++;
75             printf("mismatch on packet #%d (1-based).\n", n);
76             printf("Packet:\n");
77             ofp_print_packet(stdout, packet->data, packet->size, packet->size);
78             ovs_hex_dump(stdout, packet->data, packet->size, 0, true);
79             printf("Expected flow:\n%s\n", exp_s);
80             printf("Actually extracted flow:\n%s\n", got_s);
81             printf("\n");
82             free(exp_s);
83             free(got_s);
84         }
85
86         ofpbuf_delete(packet);
87     }
88     printf("checked %d packets, %d errors\n", n, errors);
89     return errors != 0;
90 }
91