netmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * netmap access for qemu
  3. *
  4. * Copyright (c) 2012-2013 Luigi Rizzo
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include <sys/ioctl.h>
  26. #include <net/if.h>
  27. #define NETMAP_WITH_LIBS
  28. #include <net/netmap.h>
  29. #include <net/netmap_user.h>
  30. #include "net/net.h"
  31. #include "net/tap.h"
  32. #include "clients.h"
  33. #include "qemu/error-report.h"
  34. #include "qapi/error.h"
  35. #include "qemu/iov.h"
  36. #include "qemu/cutils.h"
  37. #include "qemu/main-loop.h"
  38. typedef struct NetmapState {
  39. NetClientState nc;
  40. struct nm_desc *nmd;
  41. char ifname[IFNAMSIZ];
  42. struct netmap_ring *tx;
  43. struct netmap_ring *rx;
  44. bool read_poll;
  45. bool write_poll;
  46. struct iovec iov[IOV_MAX];
  47. int vnet_hdr_len; /* Current virtio-net header length. */
  48. } NetmapState;
  49. #ifndef __FreeBSD__
  50. #define pkt_copy bcopy
  51. #else
  52. /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
  53. static inline void
  54. pkt_copy(const void *_src, void *_dst, int l)
  55. {
  56. const uint64_t *src = _src;
  57. uint64_t *dst = _dst;
  58. if (unlikely(l >= 1024)) {
  59. bcopy(src, dst, l);
  60. return;
  61. }
  62. for (; l > 0; l -= 64) {
  63. *dst++ = *src++;
  64. *dst++ = *src++;
  65. *dst++ = *src++;
  66. *dst++ = *src++;
  67. *dst++ = *src++;
  68. *dst++ = *src++;
  69. *dst++ = *src++;
  70. *dst++ = *src++;
  71. }
  72. }
  73. #endif /* __FreeBSD__ */
  74. /*
  75. * Open a netmap device. We assume there is only one queue
  76. * (which is the case for the VALE bridge).
  77. */
  78. static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
  79. Error **errp)
  80. {
  81. struct nm_desc *nmd;
  82. struct nmreq req;
  83. memset(&req, 0, sizeof(req));
  84. nmd = nm_open(nm_opts->ifname, &req, NETMAP_NO_TX_POLL,
  85. NULL);
  86. if (nmd == NULL) {
  87. error_setg_errno(errp, errno, "Failed to nm_open() %s",
  88. nm_opts->ifname);
  89. return NULL;
  90. }
  91. return nmd;
  92. }
  93. static void netmap_send(void *opaque);
  94. static void netmap_writable(void *opaque);
  95. /* Set the event-loop handlers for the netmap backend. */
  96. static void netmap_update_fd_handler(NetmapState *s)
  97. {
  98. qemu_set_fd_handler(s->nmd->fd,
  99. s->read_poll ? netmap_send : NULL,
  100. s->write_poll ? netmap_writable : NULL,
  101. s);
  102. }
  103. /* Update the read handler. */
  104. static void netmap_read_poll(NetmapState *s, bool enable)
  105. {
  106. if (s->read_poll != enable) { /* Do nothing if not changed. */
  107. s->read_poll = enable;
  108. netmap_update_fd_handler(s);
  109. }
  110. }
  111. /* Update the write handler. */
  112. static void netmap_write_poll(NetmapState *s, bool enable)
  113. {
  114. if (s->write_poll != enable) {
  115. s->write_poll = enable;
  116. netmap_update_fd_handler(s);
  117. }
  118. }
  119. static void netmap_poll(NetClientState *nc, bool enable)
  120. {
  121. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  122. if (s->read_poll != enable || s->write_poll != enable) {
  123. s->write_poll = enable;
  124. s->read_poll = enable;
  125. netmap_update_fd_handler(s);
  126. }
  127. }
  128. /*
  129. * The fd_write() callback, invoked if the fd is marked as
  130. * writable after a poll. Unregister the handler and flush any
  131. * buffered packets.
  132. */
  133. static void netmap_writable(void *opaque)
  134. {
  135. NetmapState *s = opaque;
  136. netmap_write_poll(s, false);
  137. qemu_flush_queued_packets(&s->nc);
  138. }
  139. static ssize_t netmap_receive_iov(NetClientState *nc,
  140. const struct iovec *iov, int iovcnt)
  141. {
  142. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  143. struct netmap_ring *ring = s->tx;
  144. unsigned int tail = ring->tail;
  145. ssize_t totlen = 0;
  146. uint32_t last;
  147. uint32_t idx;
  148. uint8_t *dst;
  149. int j;
  150. uint32_t i;
  151. last = i = ring->head;
  152. if (nm_ring_space(ring) < iovcnt) {
  153. /* Not enough netmap slots. Tell the kernel that we have seen the new
  154. * available slots (so that it notifies us again when it has more
  155. * ones), but without publishing any new slots to be processed
  156. * (e.g., we don't advance ring->head). */
  157. ring->cur = tail;
  158. netmap_write_poll(s, true);
  159. return 0;
  160. }
  161. for (j = 0; j < iovcnt; j++) {
  162. int iov_frag_size = iov[j].iov_len;
  163. int offset = 0;
  164. int nm_frag_size;
  165. totlen += iov_frag_size;
  166. /* Split each iovec fragment over more netmap slots, if
  167. necessary. */
  168. while (iov_frag_size) {
  169. nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
  170. if (unlikely(i == tail)) {
  171. /* We ran out of netmap slots while splitting the
  172. iovec fragments. */
  173. ring->cur = tail;
  174. netmap_write_poll(s, true);
  175. return 0;
  176. }
  177. idx = ring->slot[i].buf_idx;
  178. dst = (uint8_t *)NETMAP_BUF(ring, idx);
  179. ring->slot[i].len = nm_frag_size;
  180. ring->slot[i].flags = NS_MOREFRAG;
  181. pkt_copy(iov[j].iov_base + offset, dst, nm_frag_size);
  182. last = i;
  183. i = nm_ring_next(ring, i);
  184. offset += nm_frag_size;
  185. iov_frag_size -= nm_frag_size;
  186. }
  187. }
  188. /* The last slot must not have NS_MOREFRAG set. */
  189. ring->slot[last].flags &= ~NS_MOREFRAG;
  190. /* Now update ring->head and ring->cur to publish the new slots and
  191. * the new wakeup point. */
  192. ring->head = ring->cur = i;
  193. ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
  194. return totlen;
  195. }
  196. static ssize_t netmap_receive(NetClientState *nc,
  197. const uint8_t *buf, size_t size)
  198. {
  199. struct iovec iov;
  200. iov.iov_base = (void *)buf;
  201. iov.iov_len = size;
  202. return netmap_receive_iov(nc, &iov, 1);
  203. }
  204. /* Complete a previous send (backend --> guest) and enable the
  205. fd_read callback. */
  206. static void netmap_send_completed(NetClientState *nc, ssize_t len)
  207. {
  208. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  209. netmap_read_poll(s, true);
  210. }
  211. static void netmap_send(void *opaque)
  212. {
  213. NetmapState *s = opaque;
  214. struct netmap_ring *ring = s->rx;
  215. unsigned int tail = ring->tail;
  216. /* Keep sending while there are available slots in the netmap
  217. RX ring and the forwarding path towards the peer is open. */
  218. while (ring->head != tail) {
  219. uint32_t i = ring->head;
  220. uint32_t idx;
  221. bool morefrag;
  222. int iovcnt = 0;
  223. int iovsize;
  224. /* Get a (possibly multi-slot) packet. */
  225. do {
  226. idx = ring->slot[i].buf_idx;
  227. morefrag = (ring->slot[i].flags & NS_MOREFRAG);
  228. s->iov[iovcnt].iov_base = (void *)NETMAP_BUF(ring, idx);
  229. s->iov[iovcnt].iov_len = ring->slot[i].len;
  230. iovcnt++;
  231. i = nm_ring_next(ring, i);
  232. } while (i != tail && morefrag);
  233. /* Advance ring->cur to tell the kernel that we have seen the slots. */
  234. ring->cur = i;
  235. if (unlikely(morefrag)) {
  236. /* This is a truncated packet, so we can stop without releasing the
  237. * incomplete slots by updating ring->head. We will hopefully
  238. * re-read the complete packet the next time we are called. */
  239. break;
  240. }
  241. iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
  242. netmap_send_completed);
  243. /* Release the slots to the kernel. */
  244. ring->head = i;
  245. if (iovsize == 0) {
  246. /* The peer does not receive anymore. Packet is queued, stop
  247. * reading from the backend until netmap_send_completed(). */
  248. netmap_read_poll(s, false);
  249. break;
  250. }
  251. }
  252. }
  253. /* Flush and close. */
  254. static void netmap_cleanup(NetClientState *nc)
  255. {
  256. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  257. qemu_purge_queued_packets(nc);
  258. netmap_poll(nc, false);
  259. nm_close(s->nmd);
  260. s->nmd = NULL;
  261. }
  262. /* Offloading manipulation support callbacks. */
  263. static int netmap_fd_set_vnet_hdr_len(NetmapState *s, int len)
  264. {
  265. struct nmreq req;
  266. /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
  267. * length for the netmap adapter associated to 's->ifname'.
  268. */
  269. memset(&req, 0, sizeof(req));
  270. pstrcpy(req.nr_name, sizeof(req.nr_name), s->ifname);
  271. req.nr_version = NETMAP_API;
  272. req.nr_cmd = NETMAP_BDG_VNET_HDR;
  273. req.nr_arg1 = len;
  274. return ioctl(s->nmd->fd, NIOCREGIF, &req);
  275. }
  276. static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
  277. {
  278. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  279. int prev_len = s->vnet_hdr_len;
  280. /* Check that we can set the new length. */
  281. if (netmap_fd_set_vnet_hdr_len(s, len)) {
  282. return false;
  283. }
  284. /* Restore the previous length. */
  285. if (netmap_fd_set_vnet_hdr_len(s, prev_len)) {
  286. error_report("Failed to restore vnet-hdr length %d on %s: %s",
  287. prev_len, s->ifname, strerror(errno));
  288. abort();
  289. }
  290. return true;
  291. }
  292. /* A netmap interface that supports virtio-net headers always
  293. * supports UFO, so we use this callback also for the has_ufo hook. */
  294. static bool netmap_has_vnet_hdr(NetClientState *nc)
  295. {
  296. return netmap_has_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
  297. }
  298. static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
  299. {
  300. }
  301. static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
  302. {
  303. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  304. int err;
  305. err = netmap_fd_set_vnet_hdr_len(s, len);
  306. if (err) {
  307. error_report("Unable to set vnet-hdr length %d on %s: %s",
  308. len, s->ifname, strerror(errno));
  309. } else {
  310. /* Keep track of the current length. */
  311. s->vnet_hdr_len = len;
  312. }
  313. }
  314. static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  315. int ecn, int ufo)
  316. {
  317. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  318. /* Setting a virtio-net header length greater than zero automatically
  319. * enables the offloadings. */
  320. if (!s->vnet_hdr_len) {
  321. netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
  322. }
  323. }
  324. /* NetClientInfo methods */
  325. static NetClientInfo net_netmap_info = {
  326. .type = NET_CLIENT_DRIVER_NETMAP,
  327. .size = sizeof(NetmapState),
  328. .receive = netmap_receive,
  329. .receive_iov = netmap_receive_iov,
  330. .poll = netmap_poll,
  331. .cleanup = netmap_cleanup,
  332. .has_ufo = netmap_has_vnet_hdr,
  333. .has_vnet_hdr = netmap_has_vnet_hdr,
  334. .has_vnet_hdr_len = netmap_has_vnet_hdr_len,
  335. .using_vnet_hdr = netmap_using_vnet_hdr,
  336. .set_offload = netmap_set_offload,
  337. .set_vnet_hdr_len = netmap_set_vnet_hdr_len,
  338. };
  339. /* The exported init function
  340. *
  341. * ... -net netmap,ifname="..."
  342. */
  343. int net_init_netmap(const Netdev *netdev,
  344. const char *name, NetClientState *peer, Error **errp)
  345. {
  346. const NetdevNetmapOptions *netmap_opts = &netdev->u.netmap;
  347. struct nm_desc *nmd;
  348. NetClientState *nc;
  349. Error *err = NULL;
  350. NetmapState *s;
  351. nmd = netmap_open(netmap_opts, &err);
  352. if (err) {
  353. error_propagate(errp, err);
  354. return -1;
  355. }
  356. /* Create the object. */
  357. nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name);
  358. s = DO_UPCAST(NetmapState, nc, nc);
  359. s->nmd = nmd;
  360. s->tx = NETMAP_TXRING(nmd->nifp, 0);
  361. s->rx = NETMAP_RXRING(nmd->nifp, 0);
  362. s->vnet_hdr_len = 0;
  363. pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
  364. netmap_read_poll(s, true); /* Initially only poll for reads. */
  365. return 0;
  366. }