nicira-ext: Support matching ARP source and target hardware addresses.
[sliver-openvswitch.git] / datapath / flow.h
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #ifndef FLOW_H
10 #define FLOW_H 1
11
12 #include <linux/kernel.h>
13 #include <linux/netlink.h>
14 #include <linux/spinlock.h>
15 #include <linux/types.h>
16 #include <linux/rcupdate.h>
17 #include <linux/if_ether.h>
18 #include <linux/jiffies.h>
19 #include <linux/time.h>
20
21 #include "openvswitch/datapath-protocol.h"
22 #include "table.h"
23
24 struct sk_buff;
25
26 struct sw_flow_actions {
27         struct rcu_head rcu;
28         u32 actions_len;
29         struct nlattr actions[];
30 };
31
32 struct sw_flow_key {
33         __be64  tun_id;     /* Encapsulating tunnel ID. */
34         __be32  nw_src;     /* IP source address. */
35         __be32  nw_dst;     /* IP destination address. */
36         u16     in_port;    /* Input switch port. */
37         __be16  dl_tci;     /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
38         __be16  dl_type;    /* Ethernet frame type. */
39         __be16  tp_src;     /* TCP/UDP source port. */
40         __be16  tp_dst;     /* TCP/UDP destination port. */
41         u8      dl_src[ETH_ALEN]; /* Ethernet source address. */
42         u8      dl_dst[ETH_ALEN]; /* Ethernet destination address. */
43         u8      nw_proto;   /* IP protocol or lower 8 bits of ARP opcode. */
44         u8      nw_tos;     /* IP ToS (DSCP field, 6 bits). */
45         u8      arp_sha[ETH_ALEN]; /* ARP source hardware address. */
46         u8      arp_tha[ETH_ALEN]; /* ARP target hardware address. */
47 };
48
49 struct sw_flow {
50         struct rcu_head rcu;
51         struct tbl_node tbl_node;
52
53         struct sw_flow_key key;
54         struct sw_flow_actions __rcu *sf_acts;
55
56         atomic_t refcnt;
57         bool dead;
58
59         spinlock_t lock;        /* Lock for values below. */
60         unsigned long used;     /* Last used time (in jiffies). */
61         u64 packet_count;       /* Number of packets matched. */
62         u64 byte_count;         /* Number of bytes matched. */
63         u8 tcp_flags;           /* Union of seen TCP flags. */
64 };
65
66 struct arp_eth_header
67 {
68         __be16      ar_hrd;     /* format of hardware address   */
69         __be16      ar_pro;     /* format of protocol address   */
70         unsigned char   ar_hln; /* length of hardware address   */
71         unsigned char   ar_pln; /* length of protocol address   */
72         __be16      ar_op;      /* ARP opcode (command)     */
73
74         /* Ethernet+IPv4 specific members. */
75         unsigned char       ar_sha[ETH_ALEN];   /* sender hardware address  */
76         unsigned char       ar_sip[4];          /* sender IP address        */
77         unsigned char       ar_tha[ETH_ALEN];   /* target hardware address  */
78         unsigned char       ar_tip[4];          /* target IP address        */
79 } __packed;
80
81 int flow_init(void);
82 void flow_exit(void);
83
84 struct sw_flow *flow_alloc(void);
85 void flow_deferred_free(struct sw_flow *);
86 void flow_free_tbl(struct tbl_node *);
87
88 struct sw_flow_actions *flow_actions_alloc(const struct nlattr *);
89 void flow_deferred_free_acts(struct sw_flow_actions *);
90
91 void flow_hold(struct sw_flow *);
92 void flow_put(struct sw_flow *);
93
94 int flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *, bool *is_frag);
95 void flow_used(struct sw_flow *, struct sk_buff *);
96 u64 flow_used_time(unsigned long flow_jiffies);
97
98 u32 flow_hash(const struct sw_flow_key *);
99 int flow_cmp(const struct tbl_node *, void *target);
100
101 /* By my calculations currently the longest valid nlattr-formatted flow key is
102  * 80 bytes long, so this leaves some safety margin.
103  */
104 #define FLOW_BUFSIZE 96
105
106 int flow_to_nlattrs(const struct sw_flow_key *, struct sk_buff *);
107 int flow_from_nlattrs(struct sw_flow_key *swkey, const struct nlattr *);
108
109 static inline struct sw_flow *flow_cast(const struct tbl_node *node)
110 {
111         return container_of(node, struct sw_flow, tbl_node);
112 }
113
114 #endif /* flow.h */