datapath.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 DATAPATH_H
  19. #define DATAPATH_H 1
  20. #include <asm/page.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mutex.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/u64_stats_sync.h>
  26. #include <net/ip_tunnels.h>
  27. #include "conntrack.h"
  28. #include "flow.h"
  29. #include "flow_table.h"
  30. #include "meter.h"
  31. #include "vport-internal_dev.h"
  32. #define DP_MAX_PORTS USHRT_MAX
  33. #define DP_VPORT_HASH_BUCKETS 1024
  34. /**
  35. * struct dp_stats_percpu - per-cpu packet processing statistics for a given
  36. * datapath.
  37. * @n_hit: Number of received packets for which a matching flow was found in
  38. * the flow table.
  39. * @n_miss: Number of received packets that had no matching flow in the flow
  40. * table. The sum of @n_hit and @n_miss is the number of packets that have
  41. * been received by the datapath.
  42. * @n_lost: Number of received packets that had no matching flow in the flow
  43. * table that could not be sent to userspace (normally due to an overflow in
  44. * one of the datapath's queues).
  45. * @n_mask_hit: Number of masks looked up for flow match.
  46. * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked
  47. * up per packet.
  48. */
  49. struct dp_stats_percpu {
  50. u64 n_hit;
  51. u64 n_missed;
  52. u64 n_lost;
  53. u64 n_mask_hit;
  54. struct u64_stats_sync syncp;
  55. };
  56. /**
  57. * struct datapath - datapath for flow-based packet switching
  58. * @rcu: RCU callback head for deferred destruction.
  59. * @list_node: Element in global 'dps' list.
  60. * @table: flow table.
  61. * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
  62. * ovs_mutex and RCU.
  63. * @stats_percpu: Per-CPU datapath statistics.
  64. * @net: Reference to net namespace.
  65. * @max_headroom: the maximum headroom of all vports in this datapath; it will
  66. * be used by all the internal vports in this dp.
  67. *
  68. * Context: See the comment on locking at the top of datapath.c for additional
  69. * locking information.
  70. */
  71. struct datapath {
  72. struct rcu_head rcu;
  73. struct list_head list_node;
  74. /* Flow table. */
  75. struct flow_table table;
  76. /* Switch ports. */
  77. struct hlist_head *ports;
  78. /* Stats. */
  79. struct dp_stats_percpu __percpu *stats_percpu;
  80. /* Network namespace ref. */
  81. possible_net_t net;
  82. u32 user_features;
  83. u32 max_headroom;
  84. /* Switch meters. */
  85. struct hlist_head *meters;
  86. };
  87. /**
  88. * struct ovs_skb_cb - OVS data in skb CB
  89. * @input_vport: The original vport packet came in on. This value is cached
  90. * when a packet is received by OVS.
  91. * @mru: The maximum received fragement size; 0 if the packet is not
  92. * fragmented.
  93. * @acts_origlen: The netlink size of the flow actions applied to this skb.
  94. * @cutlen: The number of bytes from the packet end to be removed.
  95. */
  96. struct ovs_skb_cb {
  97. struct vport *input_vport;
  98. u16 mru;
  99. u16 acts_origlen;
  100. u32 cutlen;
  101. };
  102. #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
  103. /**
  104. * struct dp_upcall - metadata to include with a packet to send to userspace
  105. * @cmd: One of %OVS_PACKET_CMD_*.
  106. * @userdata: If nonnull, its variable-length value is passed to userspace as
  107. * %OVS_PACKET_ATTR_USERDATA.
  108. * @portid: Netlink portid to which packet should be sent. If @portid is 0
  109. * then no packet is sent and the packet is accounted in the datapath's @n_lost
  110. * counter.
  111. * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
  112. * @mru: If not zero, Maximum received IP fragment size.
  113. */
  114. struct dp_upcall_info {
  115. struct ip_tunnel_info *egress_tun_info;
  116. const struct nlattr *userdata;
  117. const struct nlattr *actions;
  118. int actions_len;
  119. u32 portid;
  120. u8 cmd;
  121. u16 mru;
  122. };
  123. /**
  124. * struct ovs_net - Per net-namespace data for ovs.
  125. * @dps: List of datapaths to enable dumping them all out.
  126. * Protected by genl_mutex.
  127. */
  128. struct ovs_net {
  129. struct list_head dps;
  130. struct work_struct dp_notify_work;
  131. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  132. struct ovs_ct_limit_info *ct_limit_info;
  133. #endif
  134. /* Module reference for configuring conntrack. */
  135. bool xt_label;
  136. };
  137. extern unsigned int ovs_net_id;
  138. void ovs_lock(void);
  139. void ovs_unlock(void);
  140. #ifdef CONFIG_LOCKDEP
  141. int lockdep_ovsl_is_held(void);
  142. #else
  143. #define lockdep_ovsl_is_held() 1
  144. #endif
  145. #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held())
  146. #define ovsl_dereference(p) \
  147. rcu_dereference_protected(p, lockdep_ovsl_is_held())
  148. #define rcu_dereference_ovsl(p) \
  149. rcu_dereference_check(p, lockdep_ovsl_is_held())
  150. static inline struct net *ovs_dp_get_net(const struct datapath *dp)
  151. {
  152. return read_pnet(&dp->net);
  153. }
  154. static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
  155. {
  156. write_pnet(&dp->net, net);
  157. }
  158. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
  159. static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
  160. {
  161. WARN_ON_ONCE(!rcu_read_lock_held());
  162. return ovs_lookup_vport(dp, port_no);
  163. }
  164. static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
  165. {
  166. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  167. return ovs_lookup_vport(dp, port_no);
  168. }
  169. static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
  170. {
  171. ASSERT_OVSL();
  172. return ovs_lookup_vport(dp, port_no);
  173. }
  174. /* Must be called with rcu_read_lock. */
  175. static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  176. {
  177. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  178. if (dev) {
  179. struct vport *vport = ovs_internal_dev_get_vport(dev);
  180. if (vport)
  181. return vport->dp;
  182. }
  183. return NULL;
  184. }
  185. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  186. * returned dp pointer valid.
  187. */
  188. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  189. {
  190. struct datapath *dp;
  191. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  192. rcu_read_lock();
  193. dp = get_dp_rcu(net, dp_ifindex);
  194. rcu_read_unlock();
  195. return dp;
  196. }
  197. extern struct notifier_block ovs_dp_device_notifier;
  198. extern struct genl_family dp_vport_genl_family;
  199. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
  200. void ovs_dp_detach_port(struct vport *);
  201. int ovs_dp_upcall(struct datapath *, struct sk_buff *,
  202. const struct sw_flow_key *, const struct dp_upcall_info *,
  203. uint32_t cutlen);
  204. const char *ovs_dp_name(const struct datapath *dp);
  205. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
  206. u32 portid, u32 seq, u8 cmd);
  207. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  208. const struct sw_flow_actions *, struct sw_flow_key *);
  209. void ovs_dp_notify_wq(struct work_struct *work);
  210. int action_fifos_init(void);
  211. void action_fifos_exit(void);
  212. /* 'KEY' must not have any bits set outside of the 'MASK' */
  213. #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
  214. #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
  215. #define OVS_NLERR(logging_allowed, fmt, ...) \
  216. do { \
  217. if (logging_allowed && net_ratelimit()) \
  218. pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \
  219. } while (0)
  220. #endif /* datapath.h */