2
0

netmap.c 12 KB

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