1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
33 #include <sys/types.h>
36 #include <netinet/in.h>
44 #define THIS_MODULE VLM_flow
47 flow_extract(struct buffer *packet, uint16_t in_port, struct flow *flow)
49 struct buffer b = *packet;
50 struct eth_header *eth;
52 if (b.size < ETH_TOTAL_MIN) {
53 /* This message is not too useful since there are various ways that we
54 * can end up with runt frames, e.g. frames that only ever passed
55 * through virtual network devices and never touched a physical
57 VLOG_DBG("packet length %zu less than minimum size %d",
58 b.size, ETH_TOTAL_MIN);
61 memset(flow, 0, sizeof *flow);
62 flow->in_port = htons(in_port);
68 eth = buffer_at(&b, 0, sizeof *eth);
70 buffer_pull(&b, ETH_HEADER_LEN);
71 if (ntohs(eth->eth_type) >= OFP_DL_TYPE_ETH2_CUTOFF) {
72 /* This is an Ethernet II frame */
73 flow->dl_type = eth->eth_type;
75 /* This is an 802.2 frame */
76 struct llc_snap_header *h = buffer_at(&b, 0, sizeof *h);
80 if (h->llc.llc_dsap == LLC_DSAP_SNAP
81 && h->llc.llc_ssap == LLC_SSAP_SNAP
82 && h->llc.llc_cntl == LLC_CNTL_SNAP
83 && !memcmp(h->snap.snap_org, SNAP_ORG_ETHERNET,
84 sizeof h->snap.snap_org)) {
85 flow->dl_type = h->snap.snap_type;
86 buffer_pull(&b, sizeof *h);
88 flow->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
89 buffer_pull(&b, sizeof(struct llc_header));
93 /* Check for a VLAN tag */
94 if (flow->dl_type != htons(ETH_TYPE_VLAN)) {
95 flow->dl_vlan = htons(OFP_VLAN_NONE);
97 struct vlan_header *vh = buffer_at(&b, 0, sizeof *vh);
98 flow->dl_type = vh->vlan_next_type;
99 flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID);
100 buffer_pull(&b, sizeof *vh);
102 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
103 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
106 if (flow->dl_type == htons(ETH_TYPE_IP)) {
107 const struct ip_header *nh = buffer_at(&b, 0, sizeof *nh);
109 int ip_len = IP_IHL(nh->ip_ihl_ver) * 4;
110 if (ip_len < IP_HEADER_LEN) {
114 flow->nw_src = nh->ip_src;
115 flow->nw_dst = nh->ip_dst;
116 flow->nw_proto = nh->ip_proto;
117 if (flow->nw_proto == IP_TYPE_TCP
118 || flow->nw_proto == IP_TYPE_UDP) {
119 const struct udp_header *th;
120 th = packet->l4 = buffer_at(&b, ip_len, sizeof *th);
122 flow->tp_src = th->udp_src;
123 flow->tp_dst = th->udp_dst;
125 /* Avoid tricking other code into thinking that this
126 * packet has an L4 header. */
136 flow_print(FILE *stream, const struct flow *flow)
139 "port%04x:vlan%04x mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" "
140 "proto%04x ip"IP_FMT"->"IP_FMT" port%d->%d",
141 ntohs(flow->in_port), ntohs(flow->dl_vlan),
142 ETH_ADDR_ARGS(flow->dl_src), ETH_ADDR_ARGS(flow->dl_dst),
143 ntohs(flow->dl_type),
144 IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
145 ntohs(flow->tp_src), ntohs(flow->tp_dst));
149 flow_compare(const struct flow *a, const struct flow *b)
151 return memcmp(a, b, sizeof *a);
155 flow_hash(const struct flow *flow, uint32_t basis)
157 return hash_fnv(flow, sizeof *flow, basis);