common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License version 2
  4. * as published by the Free Software Foundation; or, when distributed
  5. * separately from the Linux kernel or incorporated into other
  6. * software packages, subject to the following license:
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this source file (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  12. * and to permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #ifndef __XEN_NETBACK__COMMON_H__
  27. #define __XEN_NETBACK__COMMON_H__
  28. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  29. #include <linux/module.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/slab.h>
  32. #include <linux/ip.h>
  33. #include <linux/in.h>
  34. #include <linux/io.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/wait.h>
  38. #include <linux/sched.h>
  39. #include <xen/interface/io/netif.h>
  40. #include <xen/interface/grant_table.h>
  41. #include <xen/grant_table.h>
  42. #include <xen/xenbus.h>
  43. #include <xen/page.h>
  44. #include <linux/debugfs.h>
  45. typedef unsigned int pending_ring_idx_t;
  46. struct pending_tx_info {
  47. struct xen_netif_tx_request req; /* tx request */
  48. unsigned int extra_count;
  49. /* Callback data for released SKBs. The callback is always
  50. * xenvif_zerocopy_callback, desc contains the pending_idx, which is
  51. * also an index in pending_tx_info array. It is initialized in
  52. * xenvif_alloc and it never changes.
  53. * skb_shinfo(skb)->destructor_arg points to the first mapped slot's
  54. * callback_struct in this array of struct pending_tx_info's, then ctx
  55. * to the next, or NULL if there is no more slot for this skb.
  56. * ubuf_to_vif is a helper which finds the struct xenvif from a pointer
  57. * to this field.
  58. */
  59. struct ubuf_info_msgzc callback_struct;
  60. };
  61. #define XEN_NETIF_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, XEN_PAGE_SIZE)
  62. #define XEN_NETIF_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, XEN_PAGE_SIZE)
  63. struct xenvif_rx_meta {
  64. int id;
  65. int size;
  66. int gso_type;
  67. int gso_size;
  68. };
  69. #define GSO_BIT(type) \
  70. (1 << XEN_NETIF_GSO_TYPE_ ## type)
  71. /* Discriminate from any valid pending_idx value. */
  72. #define INVALID_PENDING_IDX 0xFFFF
  73. #define MAX_PENDING_REQS XEN_NETIF_TX_RING_SIZE
  74. /* The maximum number of frags is derived from the size of a grant (same
  75. * as a Xen page size for now).
  76. */
  77. #define MAX_XEN_SKB_FRAGS (65536 / XEN_PAGE_SIZE + 1)
  78. #define NETBACK_INVALID_HANDLE -1
  79. /* To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
  80. * the maximum slots a valid packet can use. Now this value is defined
  81. * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
  82. * all backend.
  83. */
  84. #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
  85. /* Queue name is interface name with "-qNNN" appended */
  86. #define QUEUE_NAME_SIZE (IFNAMSIZ + 5)
  87. /* IRQ name is queue name with "-tx" or "-rx" appended */
  88. #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
  89. struct xenvif;
  90. struct xenvif_stats {
  91. /* Stats fields to be updated per-queue.
  92. * A subset of struct net_device_stats that contains only the
  93. * fields that are updated in netback.c for each queue.
  94. */
  95. u64 rx_bytes;
  96. u64 rx_packets;
  97. u64 tx_bytes;
  98. u64 tx_packets;
  99. /* Additional stats used by xenvif */
  100. unsigned long rx_gso_checksum_fixup;
  101. unsigned long tx_zerocopy_sent;
  102. unsigned long tx_zerocopy_success;
  103. unsigned long tx_zerocopy_fail;
  104. unsigned long tx_frag_overflow;
  105. };
  106. #define COPY_BATCH_SIZE 64
  107. struct xenvif_copy_state {
  108. struct gnttab_copy op[COPY_BATCH_SIZE];
  109. RING_IDX idx[COPY_BATCH_SIZE];
  110. unsigned int num;
  111. struct sk_buff_head *completed;
  112. };
  113. struct xenvif_queue { /* Per-queue data for xenvif */
  114. unsigned int id; /* Queue ID, 0-based */
  115. char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
  116. struct xenvif *vif; /* Parent VIF */
  117. /*
  118. * TX/RX common EOI handling.
  119. * When feature-split-event-channels = 0, interrupt handler sets
  120. * NETBK_COMMON_EOI, otherwise NETBK_RX_EOI and NETBK_TX_EOI are set
  121. * by the RX and TX interrupt handlers.
  122. * RX and TX handler threads will issue an EOI when either
  123. * NETBK_COMMON_EOI or their specific bits (NETBK_RX_EOI or
  124. * NETBK_TX_EOI) are set and they will reset those bits.
  125. */
  126. atomic_t eoi_pending;
  127. #define NETBK_RX_EOI 0x01
  128. #define NETBK_TX_EOI 0x02
  129. #define NETBK_COMMON_EOI 0x04
  130. /* Use NAPI for guest TX */
  131. struct napi_struct napi;
  132. /* When feature-split-event-channels = 0, tx_irq = rx_irq. */
  133. unsigned int tx_irq;
  134. /* Only used when feature-split-event-channels = 1 */
  135. char tx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-tx */
  136. struct xen_netif_tx_back_ring tx;
  137. struct sk_buff_head tx_queue;
  138. struct page *mmap_pages[MAX_PENDING_REQS];
  139. pending_ring_idx_t pending_prod;
  140. pending_ring_idx_t pending_cons;
  141. u16 pending_ring[MAX_PENDING_REQS];
  142. struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
  143. grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
  144. struct gnttab_copy tx_copy_ops[2 * MAX_PENDING_REQS];
  145. struct gnttab_map_grant_ref tx_map_ops[MAX_PENDING_REQS];
  146. struct gnttab_unmap_grant_ref tx_unmap_ops[MAX_PENDING_REQS];
  147. /* passed to gnttab_[un]map_refs with pages under (un)mapping */
  148. struct page *pages_to_map[MAX_PENDING_REQS];
  149. struct page *pages_to_unmap[MAX_PENDING_REQS];
  150. /* This prevents zerocopy callbacks to race over dealloc_ring */
  151. spinlock_t callback_lock;
  152. /* This prevents dealloc thread and NAPI instance to race over response
  153. * creation and pending_ring in xenvif_idx_release. In xenvif_tx_err
  154. * it only protect response creation
  155. */
  156. spinlock_t response_lock;
  157. pending_ring_idx_t dealloc_prod;
  158. pending_ring_idx_t dealloc_cons;
  159. u16 dealloc_ring[MAX_PENDING_REQS];
  160. struct task_struct *dealloc_task;
  161. wait_queue_head_t dealloc_wq;
  162. atomic_t inflight_packets;
  163. /* Use kthread for guest RX */
  164. struct task_struct *task;
  165. wait_queue_head_t wq;
  166. /* When feature-split-event-channels = 0, tx_irq = rx_irq. */
  167. unsigned int rx_irq;
  168. /* Only used when feature-split-event-channels = 1 */
  169. char rx_irq_name[IRQ_NAME_SIZE]; /* DEVNAME-qN-rx */
  170. struct xen_netif_rx_back_ring rx;
  171. struct sk_buff_head rx_queue;
  172. unsigned int rx_queue_max;
  173. unsigned int rx_queue_len;
  174. unsigned long last_rx_time;
  175. unsigned int rx_slots_needed;
  176. bool stalled;
  177. struct xenvif_copy_state rx_copy;
  178. /* Transmit shaping: allow 'credit_bytes' every 'credit_usec'. */
  179. unsigned long credit_bytes;
  180. unsigned long credit_usec;
  181. unsigned long remaining_credit;
  182. struct timer_list credit_timeout;
  183. u64 credit_window_start;
  184. bool rate_limited;
  185. /* Statistics */
  186. struct xenvif_stats stats;
  187. };
  188. enum state_bit_shift {
  189. /* This bit marks that the vif is connected */
  190. VIF_STATUS_CONNECTED,
  191. };
  192. struct xenvif_mcast_addr {
  193. struct list_head entry;
  194. struct rcu_head rcu;
  195. u8 addr[6];
  196. };
  197. #define XEN_NETBK_MCAST_MAX 64
  198. #define XEN_NETBK_MAX_HASH_KEY_SIZE 40
  199. #define XEN_NETBK_MAX_HASH_MAPPING_SIZE 128
  200. #define XEN_NETBK_HASH_TAG_SIZE 40
  201. struct xenvif_hash_cache_entry {
  202. struct list_head link;
  203. struct rcu_head rcu;
  204. u8 tag[XEN_NETBK_HASH_TAG_SIZE];
  205. unsigned int len;
  206. u32 val;
  207. int seq;
  208. };
  209. struct xenvif_hash_cache {
  210. spinlock_t lock;
  211. struct list_head list;
  212. unsigned int count;
  213. atomic_t seq;
  214. };
  215. struct xenvif_hash {
  216. unsigned int alg;
  217. u32 flags;
  218. bool mapping_sel;
  219. u8 key[XEN_NETBK_MAX_HASH_KEY_SIZE];
  220. u32 mapping[2][XEN_NETBK_MAX_HASH_MAPPING_SIZE];
  221. unsigned int size;
  222. struct xenvif_hash_cache cache;
  223. };
  224. struct backend_info {
  225. struct xenbus_device *dev;
  226. struct xenvif *vif;
  227. /* This is the state that will be reflected in xenstore when any
  228. * active hotplug script completes.
  229. */
  230. enum xenbus_state state;
  231. enum xenbus_state frontend_state;
  232. struct xenbus_watch hotplug_status_watch;
  233. u8 have_hotplug_status_watch:1;
  234. const char *hotplug_script;
  235. };
  236. struct xenvif {
  237. /* Unique identifier for this interface. */
  238. domid_t domid;
  239. unsigned int handle;
  240. u8 fe_dev_addr[6];
  241. struct list_head fe_mcast_addr;
  242. unsigned int fe_mcast_count;
  243. /* Frontend feature information. */
  244. int gso_mask;
  245. u8 can_sg:1;
  246. u8 ip_csum:1;
  247. u8 ipv6_csum:1;
  248. u8 multicast_control:1;
  249. /* headroom requested by xen-netfront */
  250. u16 xdp_headroom;
  251. /* Is this interface disabled? True when backend discovers
  252. * frontend is rogue.
  253. */
  254. bool disabled;
  255. unsigned long status;
  256. unsigned long drain_timeout;
  257. unsigned long stall_timeout;
  258. /* Queues */
  259. struct xenvif_queue *queues;
  260. unsigned int num_queues; /* active queues, resource allocated */
  261. unsigned int stalled_queues;
  262. struct xenvif_hash hash;
  263. struct xenbus_watch credit_watch;
  264. struct xenbus_watch mcast_ctrl_watch;
  265. struct backend_info *be;
  266. spinlock_t lock;
  267. #ifdef CONFIG_DEBUG_FS
  268. struct dentry *xenvif_dbg_root;
  269. #endif
  270. struct xen_netif_ctrl_back_ring ctrl;
  271. unsigned int ctrl_irq;
  272. /* Miscellaneous private stuff. */
  273. struct net_device *dev;
  274. };
  275. struct xenvif_rx_cb {
  276. unsigned long expires;
  277. int meta_slots_used;
  278. };
  279. #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
  280. static inline struct xenbus_device *xenvif_to_xenbus_device(struct xenvif *vif)
  281. {
  282. return to_xenbus_device(vif->dev->dev.parent);
  283. }
  284. void xenvif_tx_credit_callback(struct timer_list *t);
  285. struct xenvif *xenvif_alloc(struct device *parent,
  286. domid_t domid,
  287. unsigned int handle);
  288. int xenvif_init_queue(struct xenvif_queue *queue);
  289. void xenvif_deinit_queue(struct xenvif_queue *queue);
  290. int xenvif_connect_data(struct xenvif_queue *queue,
  291. unsigned long tx_ring_ref,
  292. unsigned long rx_ring_ref,
  293. unsigned int tx_evtchn,
  294. unsigned int rx_evtchn);
  295. void xenvif_disconnect_data(struct xenvif *vif);
  296. int xenvif_connect_ctrl(struct xenvif *vif, grant_ref_t ring_ref,
  297. unsigned int evtchn);
  298. void xenvif_disconnect_ctrl(struct xenvif *vif);
  299. void xenvif_free(struct xenvif *vif);
  300. int xenvif_xenbus_init(void);
  301. void xenvif_xenbus_fini(void);
  302. /* (Un)Map communication rings. */
  303. void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue);
  304. int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
  305. grant_ref_t tx_ring_ref,
  306. grant_ref_t rx_ring_ref);
  307. /* Check for SKBs from frontend and schedule backend processing */
  308. void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue);
  309. /* Prevent the device from generating any further traffic. */
  310. void xenvif_carrier_off(struct xenvif *vif);
  311. int xenvif_tx_action(struct xenvif_queue *queue, int budget);
  312. int xenvif_kthread_guest_rx(void *data);
  313. void xenvif_kick_thread(struct xenvif_queue *queue);
  314. int xenvif_dealloc_kthread(void *data);
  315. irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data);
  316. bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread);
  317. bool xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb);
  318. void xenvif_carrier_on(struct xenvif *vif);
  319. /* Callback from stack when TX packet can be released */
  320. void xenvif_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *ubuf,
  321. bool zerocopy_success);
  322. static inline pending_ring_idx_t nr_pending_reqs(struct xenvif_queue *queue)
  323. {
  324. return MAX_PENDING_REQS -
  325. queue->pending_prod + queue->pending_cons;
  326. }
  327. irqreturn_t xenvif_interrupt(int irq, void *dev_id);
  328. extern bool separate_tx_rx_irq;
  329. extern bool provides_xdp_headroom;
  330. extern unsigned int rx_drain_timeout_msecs;
  331. extern unsigned int rx_stall_timeout_msecs;
  332. extern unsigned int xenvif_max_queues;
  333. extern unsigned int xenvif_hash_cache_size;
  334. #ifdef CONFIG_DEBUG_FS
  335. extern struct dentry *xen_netback_dbg_root;
  336. #endif
  337. void xenvif_skb_zerocopy_prepare(struct xenvif_queue *queue,
  338. struct sk_buff *skb);
  339. void xenvif_skb_zerocopy_complete(struct xenvif_queue *queue);
  340. /* Multicast control */
  341. bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr);
  342. void xenvif_mcast_addr_list_free(struct xenvif *vif);
  343. /* Hash */
  344. void xenvif_init_hash(struct xenvif *vif);
  345. void xenvif_deinit_hash(struct xenvif *vif);
  346. u32 xenvif_set_hash_alg(struct xenvif *vif, u32 alg);
  347. u32 xenvif_get_hash_flags(struct xenvif *vif, u32 *flags);
  348. u32 xenvif_set_hash_flags(struct xenvif *vif, u32 flags);
  349. u32 xenvif_set_hash_key(struct xenvif *vif, u32 gref, u32 len);
  350. u32 xenvif_set_hash_mapping_size(struct xenvif *vif, u32 size);
  351. u32 xenvif_set_hash_mapping(struct xenvif *vif, u32 gref, u32 len,
  352. u32 off);
  353. void xenvif_set_skb_hash(struct xenvif *vif, struct sk_buff *skb);
  354. #ifdef CONFIG_DEBUG_FS
  355. void xenvif_dump_hash_info(struct xenvif *vif, struct seq_file *m);
  356. #endif
  357. #endif /* __XEN_NETBACK__COMMON_H__ */