datapath.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "flow.h"
  27. #include "flow_table.h"
  28. #include "vport.h"
  29. #define DP_MAX_PORTS USHRT_MAX
  30. #define DP_VPORT_HASH_BUCKETS 1024
  31. #define SAMPLE_ACTION_DEPTH 3
  32. /**
  33. * struct dp_stats_percpu - per-cpu packet processing statistics for a given
  34. * datapath.
  35. * @n_hit: Number of received packets for which a matching flow was found in
  36. * the flow table.
  37. * @n_miss: Number of received packets that had no matching flow in the flow
  38. * table. The sum of @n_hit and @n_miss is the number of packets that have
  39. * been received by the datapath.
  40. * @n_lost: Number of received packets that had no matching flow in the flow
  41. * table that could not be sent to userspace (normally due to an overflow in
  42. * one of the datapath's queues).
  43. * @n_mask_hit: Number of masks looked up for flow match.
  44. * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked
  45. * up per packet.
  46. */
  47. struct dp_stats_percpu {
  48. u64 n_hit;
  49. u64 n_missed;
  50. u64 n_lost;
  51. u64 n_mask_hit;
  52. struct u64_stats_sync syncp;
  53. };
  54. /**
  55. * struct datapath - datapath for flow-based packet switching
  56. * @rcu: RCU callback head for deferred destruction.
  57. * @list_node: Element in global 'dps' list.
  58. * @table: flow table.
  59. * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
  60. * ovs_mutex and RCU.
  61. * @stats_percpu: Per-CPU datapath statistics.
  62. * @net: Reference to net namespace.
  63. *
  64. * Context: See the comment on locking at the top of datapath.c for additional
  65. * locking information.
  66. */
  67. struct datapath {
  68. struct rcu_head rcu;
  69. struct list_head list_node;
  70. /* Flow table. */
  71. struct flow_table table;
  72. /* Switch ports. */
  73. struct hlist_head *ports;
  74. /* Stats. */
  75. struct dp_stats_percpu __percpu *stats_percpu;
  76. /* Network namespace ref. */
  77. possible_net_t net;
  78. u32 user_features;
  79. };
  80. /**
  81. * struct ovs_skb_cb - OVS data in skb CB
  82. * @egress_tun_key: Tunnel information about this packet on egress path.
  83. * NULL if the packet is not being tunneled.
  84. * @input_vport: The original vport packet came in on. This value is cached
  85. * when a packet is received by OVS.
  86. */
  87. struct ovs_skb_cb {
  88. struct ovs_tunnel_info *egress_tun_info;
  89. struct vport *input_vport;
  90. };
  91. #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
  92. /**
  93. * struct dp_upcall - metadata to include with a packet to send to userspace
  94. * @cmd: One of %OVS_PACKET_CMD_*.
  95. * @userdata: If nonnull, its variable-length value is passed to userspace as
  96. * %OVS_PACKET_ATTR_USERDATA.
  97. * @portid: Netlink portid to which packet should be sent. If @portid is 0
  98. * then no packet is sent and the packet is accounted in the datapath's @n_lost
  99. * counter.
  100. * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
  101. */
  102. struct dp_upcall_info {
  103. const struct ovs_tunnel_info *egress_tun_info;
  104. const struct nlattr *userdata;
  105. const struct nlattr *actions;
  106. int actions_len;
  107. u32 portid;
  108. u8 cmd;
  109. };
  110. /**
  111. * struct ovs_net - Per net-namespace data for ovs.
  112. * @dps: List of datapaths to enable dumping them all out.
  113. * Protected by genl_mutex.
  114. */
  115. struct ovs_net {
  116. struct list_head dps;
  117. struct work_struct dp_notify_work;
  118. struct vport_net vport_net;
  119. };
  120. extern int ovs_net_id;
  121. void ovs_lock(void);
  122. void ovs_unlock(void);
  123. #ifdef CONFIG_LOCKDEP
  124. int lockdep_ovsl_is_held(void);
  125. #else
  126. #define lockdep_ovsl_is_held() 1
  127. #endif
  128. #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held())
  129. #define ovsl_dereference(p) \
  130. rcu_dereference_protected(p, lockdep_ovsl_is_held())
  131. #define rcu_dereference_ovsl(p) \
  132. rcu_dereference_check(p, lockdep_ovsl_is_held())
  133. static inline struct net *ovs_dp_get_net(const struct datapath *dp)
  134. {
  135. return read_pnet(&dp->net);
  136. }
  137. static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
  138. {
  139. write_pnet(&dp->net, net);
  140. }
  141. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
  142. static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
  143. {
  144. WARN_ON_ONCE(!rcu_read_lock_held());
  145. return ovs_lookup_vport(dp, port_no);
  146. }
  147. static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
  148. {
  149. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  150. return ovs_lookup_vport(dp, port_no);
  151. }
  152. static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
  153. {
  154. ASSERT_OVSL();
  155. return ovs_lookup_vport(dp, port_no);
  156. }
  157. extern struct notifier_block ovs_dp_device_notifier;
  158. extern struct genl_family dp_vport_genl_family;
  159. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
  160. void ovs_dp_detach_port(struct vport *);
  161. int ovs_dp_upcall(struct datapath *, struct sk_buff *,
  162. const struct sw_flow_key *, const struct dp_upcall_info *);
  163. const char *ovs_dp_name(const struct datapath *dp);
  164. struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
  165. u8 cmd);
  166. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  167. const struct sw_flow_actions *, struct sw_flow_key *);
  168. void ovs_dp_notify_wq(struct work_struct *work);
  169. int action_fifos_init(void);
  170. void action_fifos_exit(void);
  171. #define OVS_NLERR(logging_allowed, fmt, ...) \
  172. do { \
  173. if (logging_allowed && net_ratelimit()) \
  174. pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \
  175. } while (0)
  176. #endif /* datapath.h */