flow.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2007-2014 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/cache.h>
  21. #include <linux/kernel.h>
  22. #include <linux/netlink.h>
  23. #include <linux/openvswitch.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/types.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/in6.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/time.h>
  31. #include <linux/flex_array.h>
  32. #include <net/inet_ecn.h>
  33. struct sk_buff;
  34. /* Used to memset ovs_key_ipv4_tunnel padding. */
  35. #define OVS_TUNNEL_KEY_SIZE \
  36. (offsetof(struct ovs_key_ipv4_tunnel, tp_dst) + \
  37. FIELD_SIZEOF(struct ovs_key_ipv4_tunnel, tp_dst))
  38. struct ovs_key_ipv4_tunnel {
  39. __be64 tun_id;
  40. __be32 ipv4_src;
  41. __be32 ipv4_dst;
  42. __be16 tun_flags;
  43. u8 ipv4_tos;
  44. u8 ipv4_ttl;
  45. __be16 tp_src;
  46. __be16 tp_dst;
  47. } __packed __aligned(4); /* Minimize padding. */
  48. struct ovs_tunnel_info {
  49. struct ovs_key_ipv4_tunnel tunnel;
  50. const void *options;
  51. u8 options_len;
  52. };
  53. /* Store options at the end of the array if they are less than the
  54. * maximum size. This allows us to get the benefits of variable length
  55. * matching for small options.
  56. */
  57. #define TUN_METADATA_OFFSET(opt_len) \
  58. (FIELD_SIZEOF(struct sw_flow_key, tun_opts) - opt_len)
  59. #define TUN_METADATA_OPTS(flow_key, opt_len) \
  60. ((void *)((flow_key)->tun_opts + TUN_METADATA_OFFSET(opt_len)))
  61. static inline void __ovs_flow_tun_info_init(struct ovs_tunnel_info *tun_info,
  62. __be32 saddr, __be32 daddr,
  63. u8 tos, u8 ttl,
  64. __be16 tp_src,
  65. __be16 tp_dst,
  66. __be64 tun_id,
  67. __be16 tun_flags,
  68. const void *opts,
  69. u8 opts_len)
  70. {
  71. tun_info->tunnel.tun_id = tun_id;
  72. tun_info->tunnel.ipv4_src = saddr;
  73. tun_info->tunnel.ipv4_dst = daddr;
  74. tun_info->tunnel.ipv4_tos = tos;
  75. tun_info->tunnel.ipv4_ttl = ttl;
  76. tun_info->tunnel.tun_flags = tun_flags;
  77. /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
  78. * the upper tunnel are used.
  79. * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
  80. */
  81. tun_info->tunnel.tp_src = tp_src;
  82. tun_info->tunnel.tp_dst = tp_dst;
  83. /* Clear struct padding. */
  84. if (sizeof(tun_info->tunnel) != OVS_TUNNEL_KEY_SIZE)
  85. memset((unsigned char *)&tun_info->tunnel + OVS_TUNNEL_KEY_SIZE,
  86. 0, sizeof(tun_info->tunnel) - OVS_TUNNEL_KEY_SIZE);
  87. tun_info->options = opts;
  88. tun_info->options_len = opts_len;
  89. }
  90. static inline void ovs_flow_tun_info_init(struct ovs_tunnel_info *tun_info,
  91. const struct iphdr *iph,
  92. __be16 tp_src,
  93. __be16 tp_dst,
  94. __be64 tun_id,
  95. __be16 tun_flags,
  96. const void *opts,
  97. u8 opts_len)
  98. {
  99. __ovs_flow_tun_info_init(tun_info, iph->saddr, iph->daddr,
  100. iph->tos, iph->ttl,
  101. tp_src, tp_dst,
  102. tun_id, tun_flags,
  103. opts, opts_len);
  104. }
  105. #define OVS_SW_FLOW_KEY_METADATA_SIZE \
  106. (offsetof(struct sw_flow_key, recirc_id) + \
  107. FIELD_SIZEOF(struct sw_flow_key, recirc_id))
  108. struct sw_flow_key {
  109. u8 tun_opts[255];
  110. u8 tun_opts_len;
  111. struct ovs_key_ipv4_tunnel tun_key; /* Encapsulating tunnel key. */
  112. struct {
  113. u32 priority; /* Packet QoS priority. */
  114. u32 skb_mark; /* SKB mark. */
  115. u16 in_port; /* Input switch port (or DP_MAX_PORTS). */
  116. } __packed phy; /* Safe when right after 'tun_key'. */
  117. u32 ovs_flow_hash; /* Datapath computed hash value. */
  118. u32 recirc_id; /* Recirculation ID. */
  119. struct {
  120. u8 src[ETH_ALEN]; /* Ethernet source address. */
  121. u8 dst[ETH_ALEN]; /* Ethernet destination address. */
  122. __be16 tci; /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
  123. __be16 type; /* Ethernet frame type. */
  124. } eth;
  125. union {
  126. struct {
  127. __be32 top_lse; /* top label stack entry */
  128. } mpls;
  129. struct {
  130. u8 proto; /* IP protocol or lower 8 bits of ARP opcode. */
  131. u8 tos; /* IP ToS. */
  132. u8 ttl; /* IP TTL/hop limit. */
  133. u8 frag; /* One of OVS_FRAG_TYPE_*. */
  134. } ip;
  135. };
  136. struct {
  137. __be16 src; /* TCP/UDP/SCTP source port. */
  138. __be16 dst; /* TCP/UDP/SCTP destination port. */
  139. __be16 flags; /* TCP flags. */
  140. } tp;
  141. union {
  142. struct {
  143. struct {
  144. __be32 src; /* IP source address. */
  145. __be32 dst; /* IP destination address. */
  146. } addr;
  147. struct {
  148. u8 sha[ETH_ALEN]; /* ARP source hardware address. */
  149. u8 tha[ETH_ALEN]; /* ARP target hardware address. */
  150. } arp;
  151. } ipv4;
  152. struct {
  153. struct {
  154. struct in6_addr src; /* IPv6 source address. */
  155. struct in6_addr dst; /* IPv6 destination address. */
  156. } addr;
  157. __be32 label; /* IPv6 flow label. */
  158. struct {
  159. struct in6_addr target; /* ND target address. */
  160. u8 sll[ETH_ALEN]; /* ND source link layer address. */
  161. u8 tll[ETH_ALEN]; /* ND target link layer address. */
  162. } nd;
  163. } ipv6;
  164. };
  165. } __aligned(BITS_PER_LONG/8); /* Ensure that we can do comparisons as longs. */
  166. struct sw_flow_key_range {
  167. unsigned short int start;
  168. unsigned short int end;
  169. };
  170. struct sw_flow_mask {
  171. int ref_count;
  172. struct rcu_head rcu;
  173. struct list_head list;
  174. struct sw_flow_key_range range;
  175. struct sw_flow_key key;
  176. };
  177. struct sw_flow_match {
  178. struct sw_flow_key *key;
  179. struct sw_flow_key_range range;
  180. struct sw_flow_mask *mask;
  181. };
  182. #define MAX_UFID_LENGTH 16 /* 128 bits */
  183. struct sw_flow_id {
  184. u32 ufid_len;
  185. union {
  186. u32 ufid[MAX_UFID_LENGTH / 4];
  187. struct sw_flow_key *unmasked_key;
  188. };
  189. };
  190. struct sw_flow_actions {
  191. struct rcu_head rcu;
  192. u32 actions_len;
  193. struct nlattr actions[];
  194. };
  195. struct flow_stats {
  196. u64 packet_count; /* Number of packets matched. */
  197. u64 byte_count; /* Number of bytes matched. */
  198. unsigned long used; /* Last used time (in jiffies). */
  199. spinlock_t lock; /* Lock for atomic stats update. */
  200. __be16 tcp_flags; /* Union of seen TCP flags. */
  201. };
  202. struct sw_flow {
  203. struct rcu_head rcu;
  204. struct {
  205. struct hlist_node node[2];
  206. u32 hash;
  207. } flow_table, ufid_table;
  208. int stats_last_writer; /* NUMA-node id of the last writer on
  209. * 'stats[0]'.
  210. */
  211. struct sw_flow_key key;
  212. struct sw_flow_id id;
  213. struct sw_flow_mask *mask;
  214. struct sw_flow_actions __rcu *sf_acts;
  215. struct flow_stats __rcu *stats[]; /* One for each NUMA node. First one
  216. * is allocated at flow creation time,
  217. * the rest are allocated on demand
  218. * while holding the 'stats[0].lock'.
  219. */
  220. };
  221. struct arp_eth_header {
  222. __be16 ar_hrd; /* format of hardware address */
  223. __be16 ar_pro; /* format of protocol address */
  224. unsigned char ar_hln; /* length of hardware address */
  225. unsigned char ar_pln; /* length of protocol address */
  226. __be16 ar_op; /* ARP opcode (command) */
  227. /* Ethernet+IPv4 specific members. */
  228. unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
  229. unsigned char ar_sip[4]; /* sender IP address */
  230. unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
  231. unsigned char ar_tip[4]; /* target IP address */
  232. } __packed;
  233. static inline bool ovs_identifier_is_ufid(const struct sw_flow_id *sfid)
  234. {
  235. return sfid->ufid_len;
  236. }
  237. static inline bool ovs_identifier_is_key(const struct sw_flow_id *sfid)
  238. {
  239. return !ovs_identifier_is_ufid(sfid);
  240. }
  241. void ovs_flow_stats_update(struct sw_flow *, __be16 tcp_flags,
  242. const struct sk_buff *);
  243. void ovs_flow_stats_get(const struct sw_flow *, struct ovs_flow_stats *,
  244. unsigned long *used, __be16 *tcp_flags);
  245. void ovs_flow_stats_clear(struct sw_flow *);
  246. u64 ovs_flow_used_time(unsigned long flow_jiffies);
  247. int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key);
  248. int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
  249. struct sk_buff *skb,
  250. struct sw_flow_key *key);
  251. /* Extract key from packet coming from userspace. */
  252. int ovs_flow_key_extract_userspace(const struct nlattr *attr,
  253. struct sk_buff *skb,
  254. struct sw_flow_key *key, bool log);
  255. #endif /* flow.h */