xsk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* XDP sockets
  3. *
  4. * AF_XDP sockets allows a channel between XDP programs and userspace
  5. * applications.
  6. * Copyright(c) 2018 Intel Corporation.
  7. *
  8. * Author(s): Björn Töpel <bjorn.topel@intel.com>
  9. * Magnus Karlsson <magnus.karlsson@intel.com>
  10. */
  11. #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
  12. #include <linux/if_xdp.h>
  13. #include <linux/init.h>
  14. #include <linux/sched/mm.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/socket.h>
  18. #include <linux/file.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/net.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/rculist.h>
  23. #include <net/xdp_sock.h>
  24. #include <net/xdp.h>
  25. #include "xsk_queue.h"
  26. #include "xdp_umem.h"
  27. #define TX_BATCH_SIZE 16
  28. static struct xdp_sock *xdp_sk(struct sock *sk)
  29. {
  30. return (struct xdp_sock *)sk;
  31. }
  32. bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
  33. {
  34. return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) &&
  35. READ_ONCE(xs->umem->fq);
  36. }
  37. u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
  38. {
  39. return xskq_peek_addr(umem->fq, addr);
  40. }
  41. EXPORT_SYMBOL(xsk_umem_peek_addr);
  42. void xsk_umem_discard_addr(struct xdp_umem *umem)
  43. {
  44. xskq_discard_addr(umem->fq);
  45. }
  46. EXPORT_SYMBOL(xsk_umem_discard_addr);
  47. static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
  48. {
  49. void *buffer;
  50. u64 addr;
  51. int err;
  52. if (!xskq_peek_addr(xs->umem->fq, &addr) ||
  53. len > xs->umem->chunk_size_nohr) {
  54. xs->rx_dropped++;
  55. return -ENOSPC;
  56. }
  57. addr += xs->umem->headroom;
  58. buffer = xdp_umem_get_data(xs->umem, addr);
  59. memcpy(buffer, xdp->data, len);
  60. err = xskq_produce_batch_desc(xs->rx, addr, len);
  61. if (!err) {
  62. xskq_discard_addr(xs->umem->fq);
  63. xdp_return_buff(xdp);
  64. return 0;
  65. }
  66. xs->rx_dropped++;
  67. return err;
  68. }
  69. static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
  70. {
  71. int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);
  72. if (err)
  73. xs->rx_dropped++;
  74. return err;
  75. }
  76. int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
  77. {
  78. u32 len;
  79. if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
  80. return -EINVAL;
  81. len = xdp->data_end - xdp->data;
  82. return (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY) ?
  83. __xsk_rcv_zc(xs, xdp, len) : __xsk_rcv(xs, xdp, len);
  84. }
  85. void xsk_flush(struct xdp_sock *xs)
  86. {
  87. xskq_produce_flush_desc(xs->rx);
  88. xs->sk.sk_data_ready(&xs->sk);
  89. }
  90. int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
  91. {
  92. u32 len = xdp->data_end - xdp->data;
  93. void *buffer;
  94. u64 addr;
  95. int err;
  96. if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
  97. return -EINVAL;
  98. if (!xskq_peek_addr(xs->umem->fq, &addr) ||
  99. len > xs->umem->chunk_size_nohr) {
  100. xs->rx_dropped++;
  101. return -ENOSPC;
  102. }
  103. addr += xs->umem->headroom;
  104. buffer = xdp_umem_get_data(xs->umem, addr);
  105. memcpy(buffer, xdp->data, len);
  106. err = xskq_produce_batch_desc(xs->rx, addr, len);
  107. if (!err) {
  108. xskq_discard_addr(xs->umem->fq);
  109. xsk_flush(xs);
  110. return 0;
  111. }
  112. xs->rx_dropped++;
  113. return err;
  114. }
  115. void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
  116. {
  117. xskq_produce_flush_addr_n(umem->cq, nb_entries);
  118. }
  119. EXPORT_SYMBOL(xsk_umem_complete_tx);
  120. void xsk_umem_consume_tx_done(struct xdp_umem *umem)
  121. {
  122. struct xdp_sock *xs;
  123. rcu_read_lock();
  124. list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
  125. xs->sk.sk_write_space(&xs->sk);
  126. }
  127. rcu_read_unlock();
  128. }
  129. EXPORT_SYMBOL(xsk_umem_consume_tx_done);
  130. bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len)
  131. {
  132. struct xdp_desc desc;
  133. struct xdp_sock *xs;
  134. rcu_read_lock();
  135. list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
  136. if (!xskq_peek_desc(xs->tx, &desc))
  137. continue;
  138. if (xskq_produce_addr_lazy(umem->cq, desc.addr))
  139. goto out;
  140. *dma = xdp_umem_get_dma(umem, desc.addr);
  141. *len = desc.len;
  142. xskq_discard_desc(xs->tx);
  143. rcu_read_unlock();
  144. return true;
  145. }
  146. out:
  147. rcu_read_unlock();
  148. return false;
  149. }
  150. EXPORT_SYMBOL(xsk_umem_consume_tx);
  151. static int xsk_zc_xmit(struct sock *sk)
  152. {
  153. struct xdp_sock *xs = xdp_sk(sk);
  154. struct net_device *dev = xs->dev;
  155. return dev->netdev_ops->ndo_xsk_async_xmit(dev, xs->queue_id);
  156. }
  157. static void xsk_destruct_skb(struct sk_buff *skb)
  158. {
  159. u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
  160. struct xdp_sock *xs = xdp_sk(skb->sk);
  161. unsigned long flags;
  162. spin_lock_irqsave(&xs->tx_completion_lock, flags);
  163. WARN_ON_ONCE(xskq_produce_addr(xs->umem->cq, addr));
  164. spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
  165. sock_wfree(skb);
  166. }
  167. static int xsk_generic_xmit(struct sock *sk, struct msghdr *m,
  168. size_t total_len)
  169. {
  170. u32 max_batch = TX_BATCH_SIZE;
  171. struct xdp_sock *xs = xdp_sk(sk);
  172. bool sent_frame = false;
  173. struct xdp_desc desc;
  174. struct sk_buff *skb;
  175. int err = 0;
  176. mutex_lock(&xs->mutex);
  177. if (xs->queue_id >= xs->dev->real_num_tx_queues)
  178. goto out;
  179. while (xskq_peek_desc(xs->tx, &desc)) {
  180. char *buffer;
  181. u64 addr;
  182. u32 len;
  183. if (max_batch-- == 0) {
  184. err = -EAGAIN;
  185. goto out;
  186. }
  187. len = desc.len;
  188. skb = sock_alloc_send_skb(sk, len, 1, &err);
  189. if (unlikely(!skb)) {
  190. err = -EAGAIN;
  191. goto out;
  192. }
  193. skb_put(skb, len);
  194. addr = desc.addr;
  195. buffer = xdp_umem_get_data(xs->umem, addr);
  196. err = skb_store_bits(skb, 0, buffer, len);
  197. if (unlikely(err) || xskq_reserve_addr(xs->umem->cq)) {
  198. kfree_skb(skb);
  199. goto out;
  200. }
  201. skb->dev = xs->dev;
  202. skb->priority = sk->sk_priority;
  203. skb->mark = sk->sk_mark;
  204. skb_shinfo(skb)->destructor_arg = (void *)(long)addr;
  205. skb->destructor = xsk_destruct_skb;
  206. err = dev_direct_xmit(skb, xs->queue_id);
  207. xskq_discard_desc(xs->tx);
  208. /* Ignore NET_XMIT_CN as packet might have been sent */
  209. if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
  210. /* SKB completed but not sent */
  211. err = -EBUSY;
  212. goto out;
  213. }
  214. sent_frame = true;
  215. }
  216. out:
  217. if (sent_frame)
  218. sk->sk_write_space(sk);
  219. mutex_unlock(&xs->mutex);
  220. return err;
  221. }
  222. static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
  223. {
  224. bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
  225. struct sock *sk = sock->sk;
  226. struct xdp_sock *xs = xdp_sk(sk);
  227. if (unlikely(!xs->dev))
  228. return -ENXIO;
  229. if (unlikely(!(xs->dev->flags & IFF_UP)))
  230. return -ENETDOWN;
  231. if (unlikely(!xs->tx))
  232. return -ENOBUFS;
  233. if (need_wait)
  234. return -EOPNOTSUPP;
  235. return (xs->zc) ? xsk_zc_xmit(sk) : xsk_generic_xmit(sk, m, total_len);
  236. }
  237. static unsigned int xsk_poll(struct file *file, struct socket *sock,
  238. struct poll_table_struct *wait)
  239. {
  240. unsigned int mask = datagram_poll(file, sock, wait);
  241. struct sock *sk = sock->sk;
  242. struct xdp_sock *xs = xdp_sk(sk);
  243. if (xs->rx && !xskq_empty_desc(xs->rx))
  244. mask |= POLLIN | POLLRDNORM;
  245. if (xs->tx && !xskq_full_desc(xs->tx))
  246. mask |= POLLOUT | POLLWRNORM;
  247. return mask;
  248. }
  249. static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
  250. bool umem_queue)
  251. {
  252. struct xsk_queue *q;
  253. if (entries == 0 || *queue || !is_power_of_2(entries))
  254. return -EINVAL;
  255. q = xskq_create(entries, umem_queue);
  256. if (!q)
  257. return -ENOMEM;
  258. /* Make sure queue is ready before it can be seen by others */
  259. smp_wmb();
  260. WRITE_ONCE(*queue, q);
  261. return 0;
  262. }
  263. static int xsk_release(struct socket *sock)
  264. {
  265. struct sock *sk = sock->sk;
  266. struct xdp_sock *xs = xdp_sk(sk);
  267. struct net *net;
  268. if (!sk)
  269. return 0;
  270. net = sock_net(sk);
  271. local_bh_disable();
  272. sock_prot_inuse_add(net, sk->sk_prot, -1);
  273. local_bh_enable();
  274. if (xs->dev) {
  275. struct net_device *dev = xs->dev;
  276. /* Wait for driver to stop using the xdp socket. */
  277. xdp_del_sk_umem(xs->umem, xs);
  278. xs->dev = NULL;
  279. synchronize_net();
  280. dev_put(dev);
  281. }
  282. xskq_destroy(xs->rx);
  283. xskq_destroy(xs->tx);
  284. sock_orphan(sk);
  285. sock->sk = NULL;
  286. sk_refcnt_debug_release(sk);
  287. sock_put(sk);
  288. return 0;
  289. }
  290. static struct socket *xsk_lookup_xsk_from_fd(int fd)
  291. {
  292. struct socket *sock;
  293. int err;
  294. sock = sockfd_lookup(fd, &err);
  295. if (!sock)
  296. return ERR_PTR(-ENOTSOCK);
  297. if (sock->sk->sk_family != PF_XDP) {
  298. sockfd_put(sock);
  299. return ERR_PTR(-ENOPROTOOPT);
  300. }
  301. return sock;
  302. }
  303. static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  304. {
  305. struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
  306. struct sock *sk = sock->sk;
  307. struct xdp_sock *xs = xdp_sk(sk);
  308. struct net_device *dev;
  309. u32 flags, qid;
  310. int err = 0;
  311. if (addr_len < sizeof(struct sockaddr_xdp))
  312. return -EINVAL;
  313. if (sxdp->sxdp_family != AF_XDP)
  314. return -EINVAL;
  315. mutex_lock(&xs->mutex);
  316. if (xs->dev) {
  317. err = -EBUSY;
  318. goto out_release;
  319. }
  320. dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
  321. if (!dev) {
  322. err = -ENODEV;
  323. goto out_release;
  324. }
  325. if (!xs->rx && !xs->tx) {
  326. err = -EINVAL;
  327. goto out_unlock;
  328. }
  329. qid = sxdp->sxdp_queue_id;
  330. if ((xs->rx && qid >= dev->real_num_rx_queues) ||
  331. (xs->tx && qid >= dev->real_num_tx_queues)) {
  332. err = -EINVAL;
  333. goto out_unlock;
  334. }
  335. flags = sxdp->sxdp_flags;
  336. if (flags & XDP_SHARED_UMEM) {
  337. struct xdp_sock *umem_xs;
  338. struct socket *sock;
  339. if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY)) {
  340. /* Cannot specify flags for shared sockets. */
  341. err = -EINVAL;
  342. goto out_unlock;
  343. }
  344. if (xs->umem) {
  345. /* We have already our own. */
  346. err = -EINVAL;
  347. goto out_unlock;
  348. }
  349. sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
  350. if (IS_ERR(sock)) {
  351. err = PTR_ERR(sock);
  352. goto out_unlock;
  353. }
  354. umem_xs = xdp_sk(sock->sk);
  355. if (!umem_xs->umem) {
  356. /* No umem to inherit. */
  357. err = -EBADF;
  358. sockfd_put(sock);
  359. goto out_unlock;
  360. } else if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
  361. err = -EINVAL;
  362. sockfd_put(sock);
  363. goto out_unlock;
  364. }
  365. xdp_get_umem(umem_xs->umem);
  366. WRITE_ONCE(xs->umem, umem_xs->umem);
  367. sockfd_put(sock);
  368. } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
  369. err = -EINVAL;
  370. goto out_unlock;
  371. } else {
  372. /* This xsk has its own umem. */
  373. xskq_set_umem(xs->umem->fq, &xs->umem->props);
  374. xskq_set_umem(xs->umem->cq, &xs->umem->props);
  375. err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
  376. if (err)
  377. goto out_unlock;
  378. }
  379. xs->dev = dev;
  380. xs->zc = xs->umem->zc;
  381. xs->queue_id = qid;
  382. xskq_set_umem(xs->rx, &xs->umem->props);
  383. xskq_set_umem(xs->tx, &xs->umem->props);
  384. xdp_add_sk_umem(xs->umem, xs);
  385. out_unlock:
  386. if (err)
  387. dev_put(dev);
  388. out_release:
  389. mutex_unlock(&xs->mutex);
  390. return err;
  391. }
  392. static int xsk_setsockopt(struct socket *sock, int level, int optname,
  393. char __user *optval, unsigned int optlen)
  394. {
  395. struct sock *sk = sock->sk;
  396. struct xdp_sock *xs = xdp_sk(sk);
  397. int err;
  398. if (level != SOL_XDP)
  399. return -ENOPROTOOPT;
  400. switch (optname) {
  401. case XDP_RX_RING:
  402. case XDP_TX_RING:
  403. {
  404. struct xsk_queue **q;
  405. int entries;
  406. if (optlen < sizeof(entries))
  407. return -EINVAL;
  408. if (copy_from_user(&entries, optval, sizeof(entries)))
  409. return -EFAULT;
  410. mutex_lock(&xs->mutex);
  411. q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
  412. err = xsk_init_queue(entries, q, false);
  413. mutex_unlock(&xs->mutex);
  414. return err;
  415. }
  416. case XDP_UMEM_REG:
  417. {
  418. struct xdp_umem_reg mr;
  419. struct xdp_umem *umem;
  420. if (copy_from_user(&mr, optval, sizeof(mr)))
  421. return -EFAULT;
  422. mutex_lock(&xs->mutex);
  423. if (xs->umem) {
  424. mutex_unlock(&xs->mutex);
  425. return -EBUSY;
  426. }
  427. umem = xdp_umem_create(&mr);
  428. if (IS_ERR(umem)) {
  429. mutex_unlock(&xs->mutex);
  430. return PTR_ERR(umem);
  431. }
  432. /* Make sure umem is ready before it can be seen by others */
  433. smp_wmb();
  434. WRITE_ONCE(xs->umem, umem);
  435. mutex_unlock(&xs->mutex);
  436. return 0;
  437. }
  438. case XDP_UMEM_FILL_RING:
  439. case XDP_UMEM_COMPLETION_RING:
  440. {
  441. struct xsk_queue **q;
  442. int entries;
  443. if (copy_from_user(&entries, optval, sizeof(entries)))
  444. return -EFAULT;
  445. mutex_lock(&xs->mutex);
  446. if (!xs->umem) {
  447. mutex_unlock(&xs->mutex);
  448. return -EINVAL;
  449. }
  450. q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
  451. &xs->umem->cq;
  452. err = xsk_init_queue(entries, q, true);
  453. mutex_unlock(&xs->mutex);
  454. return err;
  455. }
  456. default:
  457. break;
  458. }
  459. return -ENOPROTOOPT;
  460. }
  461. static int xsk_getsockopt(struct socket *sock, int level, int optname,
  462. char __user *optval, int __user *optlen)
  463. {
  464. struct sock *sk = sock->sk;
  465. struct xdp_sock *xs = xdp_sk(sk);
  466. int len;
  467. if (level != SOL_XDP)
  468. return -ENOPROTOOPT;
  469. if (get_user(len, optlen))
  470. return -EFAULT;
  471. if (len < 0)
  472. return -EINVAL;
  473. switch (optname) {
  474. case XDP_STATISTICS:
  475. {
  476. struct xdp_statistics stats;
  477. if (len < sizeof(stats))
  478. return -EINVAL;
  479. mutex_lock(&xs->mutex);
  480. stats.rx_dropped = xs->rx_dropped;
  481. stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
  482. stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
  483. mutex_unlock(&xs->mutex);
  484. if (copy_to_user(optval, &stats, sizeof(stats)))
  485. return -EFAULT;
  486. if (put_user(sizeof(stats), optlen))
  487. return -EFAULT;
  488. return 0;
  489. }
  490. case XDP_MMAP_OFFSETS:
  491. {
  492. struct xdp_mmap_offsets off;
  493. if (len < sizeof(off))
  494. return -EINVAL;
  495. off.rx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
  496. off.rx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
  497. off.rx.desc = offsetof(struct xdp_rxtx_ring, desc);
  498. off.tx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
  499. off.tx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
  500. off.tx.desc = offsetof(struct xdp_rxtx_ring, desc);
  501. off.fr.producer = offsetof(struct xdp_umem_ring, ptrs.producer);
  502. off.fr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
  503. off.fr.desc = offsetof(struct xdp_umem_ring, desc);
  504. off.cr.producer = offsetof(struct xdp_umem_ring, ptrs.producer);
  505. off.cr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
  506. off.cr.desc = offsetof(struct xdp_umem_ring, desc);
  507. len = sizeof(off);
  508. if (copy_to_user(optval, &off, len))
  509. return -EFAULT;
  510. if (put_user(len, optlen))
  511. return -EFAULT;
  512. return 0;
  513. }
  514. default:
  515. break;
  516. }
  517. return -EOPNOTSUPP;
  518. }
  519. static int xsk_mmap(struct file *file, struct socket *sock,
  520. struct vm_area_struct *vma)
  521. {
  522. loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  523. unsigned long size = vma->vm_end - vma->vm_start;
  524. struct xdp_sock *xs = xdp_sk(sock->sk);
  525. struct xsk_queue *q = NULL;
  526. struct xdp_umem *umem;
  527. unsigned long pfn;
  528. struct page *qpg;
  529. if (offset == XDP_PGOFF_RX_RING) {
  530. q = READ_ONCE(xs->rx);
  531. } else if (offset == XDP_PGOFF_TX_RING) {
  532. q = READ_ONCE(xs->tx);
  533. } else {
  534. umem = READ_ONCE(xs->umem);
  535. if (!umem)
  536. return -EINVAL;
  537. /* Matches the smp_wmb() in XDP_UMEM_REG */
  538. smp_rmb();
  539. if (offset == XDP_UMEM_PGOFF_FILL_RING)
  540. q = READ_ONCE(umem->fq);
  541. else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
  542. q = READ_ONCE(umem->cq);
  543. }
  544. if (!q)
  545. return -EINVAL;
  546. /* Matches the smp_wmb() in xsk_init_queue */
  547. smp_rmb();
  548. qpg = virt_to_head_page(q->ring);
  549. if (size > (PAGE_SIZE << compound_order(qpg)))
  550. return -EINVAL;
  551. pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
  552. return remap_pfn_range(vma, vma->vm_start, pfn,
  553. size, vma->vm_page_prot);
  554. }
  555. static struct proto xsk_proto = {
  556. .name = "XDP",
  557. .owner = THIS_MODULE,
  558. .obj_size = sizeof(struct xdp_sock),
  559. };
  560. static const struct proto_ops xsk_proto_ops = {
  561. .family = PF_XDP,
  562. .owner = THIS_MODULE,
  563. .release = xsk_release,
  564. .bind = xsk_bind,
  565. .connect = sock_no_connect,
  566. .socketpair = sock_no_socketpair,
  567. .accept = sock_no_accept,
  568. .getname = sock_no_getname,
  569. .poll = xsk_poll,
  570. .ioctl = sock_no_ioctl,
  571. .listen = sock_no_listen,
  572. .shutdown = sock_no_shutdown,
  573. .setsockopt = xsk_setsockopt,
  574. .getsockopt = xsk_getsockopt,
  575. .sendmsg = xsk_sendmsg,
  576. .recvmsg = sock_no_recvmsg,
  577. .mmap = xsk_mmap,
  578. .sendpage = sock_no_sendpage,
  579. };
  580. static void xsk_destruct(struct sock *sk)
  581. {
  582. struct xdp_sock *xs = xdp_sk(sk);
  583. if (!sock_flag(sk, SOCK_DEAD))
  584. return;
  585. xdp_put_umem(xs->umem);
  586. sk_refcnt_debug_dec(sk);
  587. }
  588. static int xsk_create(struct net *net, struct socket *sock, int protocol,
  589. int kern)
  590. {
  591. struct sock *sk;
  592. struct xdp_sock *xs;
  593. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  594. return -EPERM;
  595. if (sock->type != SOCK_RAW)
  596. return -ESOCKTNOSUPPORT;
  597. if (protocol)
  598. return -EPROTONOSUPPORT;
  599. sock->state = SS_UNCONNECTED;
  600. sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
  601. if (!sk)
  602. return -ENOBUFS;
  603. sock->ops = &xsk_proto_ops;
  604. sock_init_data(sock, sk);
  605. sk->sk_family = PF_XDP;
  606. sk->sk_destruct = xsk_destruct;
  607. sk_refcnt_debug_inc(sk);
  608. sock_set_flag(sk, SOCK_RCU_FREE);
  609. xs = xdp_sk(sk);
  610. mutex_init(&xs->mutex);
  611. spin_lock_init(&xs->tx_completion_lock);
  612. local_bh_disable();
  613. sock_prot_inuse_add(net, &xsk_proto, 1);
  614. local_bh_enable();
  615. return 0;
  616. }
  617. static const struct net_proto_family xsk_family_ops = {
  618. .family = PF_XDP,
  619. .create = xsk_create,
  620. .owner = THIS_MODULE,
  621. };
  622. static int __init xsk_init(void)
  623. {
  624. int err;
  625. err = proto_register(&xsk_proto, 0 /* no slab */);
  626. if (err)
  627. goto out;
  628. err = sock_register(&xsk_family_ops);
  629. if (err)
  630. goto out_proto;
  631. return 0;
  632. out_proto:
  633. proto_unregister(&xsk_proto);
  634. out:
  635. return err;
  636. }
  637. fs_initcall(xsk_init);