flow.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2007-2013 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #ifndef FLOW_H
  19. #define FLOW_H 1
  20. #include <linux/kernel.h>
  21. #include <linux/netlink.h>
  22. #include <linux/openvswitch.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/types.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/in6.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/time.h>
  30. #include <linux/flex_array.h>
  31. #include <net/inet_ecn.h>
  32. struct sk_buff;
  33. struct sw_flow_mask;
  34. struct flow_table;
  35. struct sw_flow_actions {
  36. struct rcu_head rcu;
  37. u32 actions_len;
  38. struct nlattr actions[];
  39. };
  40. /* Used to memset ovs_key_ipv4_tunnel padding. */
  41. #define OVS_TUNNEL_KEY_SIZE \
  42. (offsetof(struct ovs_key_ipv4_tunnel, ipv4_ttl) + \
  43. FIELD_SIZEOF(struct ovs_key_ipv4_tunnel, ipv4_ttl))
  44. struct ovs_key_ipv4_tunnel {
  45. __be64 tun_id;
  46. __be32 ipv4_src;
  47. __be32 ipv4_dst;
  48. __be16 tun_flags;
  49. u8 ipv4_tos;
  50. u8 ipv4_ttl;
  51. };
  52. static inline void ovs_flow_tun_key_init(struct ovs_key_ipv4_tunnel *tun_key,
  53. const struct iphdr *iph, __be64 tun_id,
  54. __be16 tun_flags)
  55. {
  56. tun_key->tun_id = tun_id;
  57. tun_key->ipv4_src = iph->saddr;
  58. tun_key->ipv4_dst = iph->daddr;
  59. tun_key->ipv4_tos = iph->tos;
  60. tun_key->ipv4_ttl = iph->ttl;
  61. tun_key->tun_flags = tun_flags;
  62. /* clear struct padding. */
  63. memset((unsigned char *) tun_key + OVS_TUNNEL_KEY_SIZE, 0,
  64. sizeof(*tun_key) - OVS_TUNNEL_KEY_SIZE);
  65. }
  66. struct sw_flow_key {
  67. struct ovs_key_ipv4_tunnel tun_key; /* Encapsulating tunnel key. */
  68. struct {
  69. u32 priority; /* Packet QoS priority. */
  70. u32 skb_mark; /* SKB mark. */
  71. u16 in_port; /* Input switch port (or DP_MAX_PORTS). */
  72. } phy;
  73. struct {
  74. u8 src[ETH_ALEN]; /* Ethernet source address. */
  75. u8 dst[ETH_ALEN]; /* Ethernet destination address. */
  76. __be16 tci; /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
  77. __be16 type; /* Ethernet frame type. */
  78. } eth;
  79. struct {
  80. u8 proto; /* IP protocol or lower 8 bits of ARP opcode. */
  81. u8 tos; /* IP ToS. */
  82. u8 ttl; /* IP TTL/hop limit. */
  83. u8 frag; /* One of OVS_FRAG_TYPE_*. */
  84. } ip;
  85. union {
  86. struct {
  87. struct {
  88. __be32 src; /* IP source address. */
  89. __be32 dst; /* IP destination address. */
  90. } addr;
  91. union {
  92. struct {
  93. __be16 src; /* TCP/UDP source port. */
  94. __be16 dst; /* TCP/UDP destination port. */
  95. } tp;
  96. struct {
  97. u8 sha[ETH_ALEN]; /* ARP source hardware address. */
  98. u8 tha[ETH_ALEN]; /* ARP target hardware address. */
  99. } arp;
  100. };
  101. } ipv4;
  102. struct {
  103. struct {
  104. struct in6_addr src; /* IPv6 source address. */
  105. struct in6_addr dst; /* IPv6 destination address. */
  106. } addr;
  107. __be32 label; /* IPv6 flow label. */
  108. struct {
  109. __be16 src; /* TCP/UDP source port. */
  110. __be16 dst; /* TCP/UDP destination port. */
  111. } tp;
  112. struct {
  113. struct in6_addr target; /* ND target address. */
  114. u8 sll[ETH_ALEN]; /* ND source link layer address. */
  115. u8 tll[ETH_ALEN]; /* ND target link layer address. */
  116. } nd;
  117. } ipv6;
  118. };
  119. };
  120. struct sw_flow {
  121. struct rcu_head rcu;
  122. struct hlist_node hash_node[2];
  123. u32 hash;
  124. struct sw_flow_key key;
  125. struct sw_flow_key unmasked_key;
  126. struct sw_flow_mask *mask;
  127. struct sw_flow_actions __rcu *sf_acts;
  128. spinlock_t lock; /* Lock for values below. */
  129. unsigned long used; /* Last used time (in jiffies). */
  130. u64 packet_count; /* Number of packets matched. */
  131. u64 byte_count; /* Number of bytes matched. */
  132. u8 tcp_flags; /* Union of seen TCP flags. */
  133. };
  134. struct sw_flow_key_range {
  135. size_t start;
  136. size_t end;
  137. };
  138. static inline u16 ovs_sw_flow_key_range_actual_size(const struct sw_flow_key_range *range)
  139. {
  140. return range->end - range->start;
  141. }
  142. struct sw_flow_match {
  143. struct sw_flow_key *key;
  144. struct sw_flow_key_range range;
  145. struct sw_flow_mask *mask;
  146. };
  147. void ovs_match_init(struct sw_flow_match *match,
  148. struct sw_flow_key *key, struct sw_flow_mask *mask);
  149. struct arp_eth_header {
  150. __be16 ar_hrd; /* format of hardware address */
  151. __be16 ar_pro; /* format of protocol address */
  152. unsigned char ar_hln; /* length of hardware address */
  153. unsigned char ar_pln; /* length of protocol address */
  154. __be16 ar_op; /* ARP opcode (command) */
  155. /* Ethernet+IPv4 specific members. */
  156. unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
  157. unsigned char ar_sip[4]; /* sender IP address */
  158. unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
  159. unsigned char ar_tip[4]; /* target IP address */
  160. } __packed;
  161. int ovs_flow_init(void);
  162. void ovs_flow_exit(void);
  163. struct sw_flow *ovs_flow_alloc(void);
  164. void ovs_flow_deferred_free(struct sw_flow *);
  165. void ovs_flow_free(struct sw_flow *, bool deferred);
  166. struct sw_flow_actions *ovs_flow_actions_alloc(int actions_len);
  167. void ovs_flow_deferred_free_acts(struct sw_flow_actions *);
  168. int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);
  169. void ovs_flow_used(struct sw_flow *, struct sk_buff *);
  170. u64 ovs_flow_used_time(unsigned long flow_jiffies);
  171. int ovs_flow_to_nlattrs(const struct sw_flow_key *,
  172. const struct sw_flow_key *, struct sk_buff *);
  173. int ovs_match_from_nlattrs(struct sw_flow_match *match,
  174. const struct nlattr *,
  175. const struct nlattr *);
  176. int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
  177. const struct nlattr *attr);
  178. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  179. #define TBL_MIN_BUCKETS 1024
  180. struct flow_table {
  181. struct flex_array *buckets;
  182. unsigned int count, n_buckets;
  183. struct rcu_head rcu;
  184. struct list_head *mask_list;
  185. int node_ver;
  186. u32 hash_seed;
  187. bool keep_flows;
  188. };
  189. static inline int ovs_flow_tbl_count(struct flow_table *table)
  190. {
  191. return table->count;
  192. }
  193. static inline int ovs_flow_tbl_need_to_expand(struct flow_table *table)
  194. {
  195. return (table->count > table->n_buckets);
  196. }
  197. struct sw_flow *ovs_flow_lookup(struct flow_table *,
  198. const struct sw_flow_key *);
  199. struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
  200. struct sw_flow_match *match);
  201. void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred);
  202. struct flow_table *ovs_flow_tbl_alloc(int new_size);
  203. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table);
  204. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table);
  205. void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow);
  206. void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow);
  207. struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *idx);
  208. extern const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1];
  209. int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
  210. struct sw_flow_match *match, bool is_mask);
  211. int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
  212. const struct ovs_key_ipv4_tunnel *tun_key,
  213. const struct ovs_key_ipv4_tunnel *output);
  214. bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  215. const struct sw_flow_key *key, int key_len);
  216. struct sw_flow_mask {
  217. int ref_count;
  218. struct rcu_head rcu;
  219. struct list_head list;
  220. struct sw_flow_key_range range;
  221. struct sw_flow_key key;
  222. };
  223. static inline u16
  224. ovs_sw_flow_mask_actual_size(const struct sw_flow_mask *mask)
  225. {
  226. return ovs_sw_flow_key_range_actual_size(&mask->range);
  227. }
  228. static inline u16
  229. ovs_sw_flow_mask_size_roundup(const struct sw_flow_mask *mask)
  230. {
  231. return roundup(ovs_sw_flow_mask_actual_size(mask), sizeof(u32));
  232. }
  233. struct sw_flow_mask *ovs_sw_flow_mask_alloc(void);
  234. void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *);
  235. void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *, bool deferred);
  236. void ovs_sw_flow_mask_insert(struct flow_table *, struct sw_flow_mask *);
  237. struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *,
  238. const struct sw_flow_mask *);
  239. void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
  240. const struct sw_flow_mask *mask);
  241. #endif /* flow.h */