datapath: Use "OVS_*" as opposed to "ODP_*" for user<->kernel interactions.
[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/in6.h>
19 #include <linux/jiffies.h>
20 #include <linux/time.h>
21
22 #include "openvswitch/datapath-protocol.h"
23 #include "table.h"
24
25 struct sk_buff;
26
27 struct sw_flow_actions {
28         struct rcu_head rcu;
29         u32 actions_len;
30         struct nlattr actions[];
31 };
32
33 struct sw_flow_key {
34         struct {
35                 __be64 tun_id;          /* Encapsulating tunnel ID. */
36                 u16    in_port;         /* Input switch port. */
37                 u8     src[ETH_ALEN];   /* Ethernet source address. */
38                 u8     dst[ETH_ALEN];   /* Ethernet destination address. */
39                 __be16 tci;             /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
40                 __be16 type;            /* Ethernet frame type. */
41         } eth;
42         struct {
43                 u8     proto;           /* IP protocol or lower 8 bits of ARP opcode. */
44                 u8     tos;             /* IP ToS (DSCP field, 6 bits). */
45         } ip;
46         union {
47                 struct {
48                         struct {
49                                 __be32 src;     /* IP source address. */
50                                 __be32 dst;     /* IP destination address. */
51                         } addr;
52                         union {
53                                 struct {
54                                         __be16 src;             /* TCP/UDP source port. */
55                                         __be16 dst;             /* TCP/UDP destination port. */
56                                 } tp;
57                                 struct {
58                                         u8 sha[ETH_ALEN];       /* ARP source hardware address. */
59                                         u8 tha[ETH_ALEN];       /* ARP target hardware address. */
60                                 } arp;
61                         };
62                 } ipv4;
63                 struct {
64                         struct {
65                                 struct in6_addr src;    /* IPv6 source address. */
66                                 struct in6_addr dst;    /* IPv6 destination address. */
67                         } addr;
68                         struct {
69                                 __be16 src;             /* TCP/UDP source port. */
70                                 __be16 dst;             /* TCP/UDP destination port. */
71                         } tp;
72                         struct {
73                                 struct in6_addr target; /* ND target address. */
74                                 u8 sll[ETH_ALEN];       /* ND source link layer address. */
75                                 u8 tll[ETH_ALEN];       /* ND target link layer address. */
76                         } nd;
77                 } ipv6;
78         };
79 };
80
81 struct sw_flow {
82         struct rcu_head rcu;
83         struct tbl_node tbl_node;
84
85         struct sw_flow_key key;
86         struct sw_flow_actions __rcu *sf_acts;
87
88         atomic_t refcnt;
89         bool dead;
90
91         spinlock_t lock;        /* Lock for values below. */
92         unsigned long used;     /* Last used time (in jiffies). */
93         u64 packet_count;       /* Number of packets matched. */
94         u64 byte_count;         /* Number of bytes matched. */
95         u8 tcp_flags;           /* Union of seen TCP flags. */
96 };
97
98 struct arp_eth_header
99 {
100         __be16      ar_hrd;     /* format of hardware address   */
101         __be16      ar_pro;     /* format of protocol address   */
102         unsigned char   ar_hln; /* length of hardware address   */
103         unsigned char   ar_pln; /* length of protocol address   */
104         __be16      ar_op;      /* ARP opcode (command)     */
105
106         /* Ethernet+IPv4 specific members. */
107         unsigned char       ar_sha[ETH_ALEN];   /* sender hardware address  */
108         unsigned char       ar_sip[4];          /* sender IP address        */
109         unsigned char       ar_tha[ETH_ALEN];   /* target hardware address  */
110         unsigned char       ar_tip[4];          /* target IP address        */
111 } __packed;
112
113 int flow_init(void);
114 void flow_exit(void);
115
116 struct sw_flow *flow_alloc(void);
117 void flow_deferred_free(struct sw_flow *);
118 void flow_free_tbl(struct tbl_node *);
119
120 struct sw_flow_actions *flow_actions_alloc(const struct nlattr *);
121 void flow_deferred_free_acts(struct sw_flow_actions *);
122
123 void flow_hold(struct sw_flow *);
124 void flow_put(struct sw_flow *);
125
126 int flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *,
127                  int *key_lenp, bool *is_frag);
128 void flow_used(struct sw_flow *, struct sk_buff *);
129 u64 flow_used_time(unsigned long flow_jiffies);
130
131 u32 flow_hash(const struct sw_flow_key *, int key_lenp);
132 int flow_cmp(const struct tbl_node *, void *target, int len);
133
134 /* Upper bound on the length of a nlattr-formatted flow key.  The longest
135  * nlattr-formatted flow key would be:
136  *
137  *                         struct  pad  nl hdr  total
138  *                         ------  ---  ------  -----
139  *  OVS_KEY_ATTR_TUN_ID        8    --     4     12
140  *  OVS_KEY_ATTR_IN_PORT       4    --     4      8
141  *  OVS_KEY_ATTR_ETHERNET     12    --     4     16
142  *  OVS_KEY_ATTR_8021Q         4    --     4      8
143  *  OVS_KEY_ATTR_ETHERTYPE     2     2     4      8
144  *  OVS_KEY_ATTR_IPV6         34     2     4     40
145  *  OVS_KEY_ATTR_ICMPV6        2     2     4      8
146  *  OVS_KEY_ATTR_ND           28    --     4     32
147  *  -------------------------------------------------
148  *  total                                       132
149  */
150 #define FLOW_BUFSIZE 132
151
152 int flow_to_nlattrs(const struct sw_flow_key *, struct sk_buff *);
153 int flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
154                       const struct nlattr *);
155 int flow_metadata_from_nlattrs(u16 *in_port, __be64 *tun_id,
156                                const struct nlattr *);
157
158 static inline struct sw_flow *flow_cast(const struct tbl_node *node)
159 {
160         return container_of(node, struct sw_flow, tbl_node);
161 }
162
163 #endif /* flow.h */