flow.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/SCTP source port. */
  94. __be16 dst; /* TCP/UDP/SCTP 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/SCTP source port. */
  110. __be16 dst; /* TCP/UDP/SCTP 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. } __aligned(BITS_PER_LONG/8); /* Ensure that we can do comparisons as longs. */
  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. struct sw_flow_match {
  139. struct sw_flow_key *key;
  140. struct sw_flow_key_range range;
  141. struct sw_flow_mask *mask;
  142. };
  143. void ovs_match_init(struct sw_flow_match *match,
  144. struct sw_flow_key *key, struct sw_flow_mask *mask);
  145. struct arp_eth_header {
  146. __be16 ar_hrd; /* format of hardware address */
  147. __be16 ar_pro; /* format of protocol address */
  148. unsigned char ar_hln; /* length of hardware address */
  149. unsigned char ar_pln; /* length of protocol address */
  150. __be16 ar_op; /* ARP opcode (command) */
  151. /* Ethernet+IPv4 specific members. */
  152. unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
  153. unsigned char ar_sip[4]; /* sender IP address */
  154. unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
  155. unsigned char ar_tip[4]; /* target IP address */
  156. } __packed;
  157. int ovs_flow_init(void);
  158. void ovs_flow_exit(void);
  159. struct sw_flow *ovs_flow_alloc(void);
  160. void ovs_flow_deferred_free(struct sw_flow *);
  161. void ovs_flow_free(struct sw_flow *, bool deferred);
  162. struct sw_flow_actions *ovs_flow_actions_alloc(int actions_len);
  163. void ovs_flow_deferred_free_acts(struct sw_flow_actions *);
  164. int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);
  165. void ovs_flow_used(struct sw_flow *, struct sk_buff *);
  166. u64 ovs_flow_used_time(unsigned long flow_jiffies);
  167. int ovs_flow_to_nlattrs(const struct sw_flow_key *,
  168. const struct sw_flow_key *, struct sk_buff *);
  169. int ovs_match_from_nlattrs(struct sw_flow_match *match,
  170. const struct nlattr *,
  171. const struct nlattr *);
  172. int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
  173. const struct nlattr *attr);
  174. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  175. #define TBL_MIN_BUCKETS 1024
  176. struct flow_table {
  177. struct flex_array *buckets;
  178. unsigned int count, n_buckets;
  179. struct rcu_head rcu;
  180. struct list_head *mask_list;
  181. int node_ver;
  182. u32 hash_seed;
  183. bool keep_flows;
  184. };
  185. static inline int ovs_flow_tbl_count(struct flow_table *table)
  186. {
  187. return table->count;
  188. }
  189. static inline int ovs_flow_tbl_need_to_expand(struct flow_table *table)
  190. {
  191. return (table->count > table->n_buckets);
  192. }
  193. struct sw_flow *ovs_flow_lookup(struct flow_table *,
  194. const struct sw_flow_key *);
  195. struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
  196. struct sw_flow_match *match);
  197. void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred);
  198. struct flow_table *ovs_flow_tbl_alloc(int new_size);
  199. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table);
  200. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table);
  201. void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow);
  202. void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow);
  203. struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *idx);
  204. extern const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1];
  205. int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
  206. struct sw_flow_match *match, bool is_mask);
  207. int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
  208. const struct ovs_key_ipv4_tunnel *tun_key,
  209. const struct ovs_key_ipv4_tunnel *output);
  210. bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  211. const struct sw_flow_key *key, int key_end);
  212. struct sw_flow_mask {
  213. int ref_count;
  214. struct rcu_head rcu;
  215. struct list_head list;
  216. struct sw_flow_key_range range;
  217. struct sw_flow_key key;
  218. };
  219. struct sw_flow_mask *ovs_sw_flow_mask_alloc(void);
  220. void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *);
  221. void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *, bool deferred);
  222. void ovs_sw_flow_mask_insert(struct flow_table *, struct sw_flow_mask *);
  223. struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *,
  224. const struct sw_flow_mask *);
  225. void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
  226. const struct sw_flow_mask *mask);
  227. #endif /* flow.h */