netmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. 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(NetClientState *nc,
  140. const uint8_t *buf, size_t size)
  141. {
  142. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  143. struct netmap_ring *ring = s->tx;
  144. uint32_t i;
  145. uint32_t idx;
  146. uint8_t *dst;
  147. if (unlikely(!ring)) {
  148. /* Drop. */
  149. return size;
  150. }
  151. if (unlikely(size > ring->nr_buf_size)) {
  152. RD(5, "[netmap_receive] drop packet of size %d > %d\n",
  153. (int)size, ring->nr_buf_size);
  154. return size;
  155. }
  156. if (nm_ring_empty(ring)) {
  157. /* No available slots in the netmap TX ring. */
  158. netmap_write_poll(s, true);
  159. return 0;
  160. }
  161. i = ring->cur;
  162. idx = ring->slot[i].buf_idx;
  163. dst = (uint8_t *)NETMAP_BUF(ring, idx);
  164. ring->slot[i].len = size;
  165. ring->slot[i].flags = 0;
  166. pkt_copy(buf, dst, size);
  167. ring->cur = ring->head = nm_ring_next(ring, i);
  168. ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
  169. return size;
  170. }
  171. static ssize_t netmap_receive_iov(NetClientState *nc,
  172. const struct iovec *iov, int iovcnt)
  173. {
  174. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  175. struct netmap_ring *ring = s->tx;
  176. uint32_t last;
  177. uint32_t idx;
  178. uint8_t *dst;
  179. int j;
  180. uint32_t i;
  181. if (unlikely(!ring)) {
  182. /* Drop the packet. */
  183. return iov_size(iov, iovcnt);
  184. }
  185. last = i = ring->cur;
  186. if (nm_ring_space(ring) < iovcnt) {
  187. /* Not enough netmap slots. */
  188. netmap_write_poll(s, true);
  189. return 0;
  190. }
  191. for (j = 0; j < iovcnt; j++) {
  192. int iov_frag_size = iov[j].iov_len;
  193. int offset = 0;
  194. int nm_frag_size;
  195. /* Split each iovec fragment over more netmap slots, if
  196. necessary. */
  197. while (iov_frag_size) {
  198. nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
  199. if (unlikely(nm_ring_empty(ring))) {
  200. /* We run out of netmap slots while splitting the
  201. iovec fragments. */
  202. netmap_write_poll(s, true);
  203. return 0;
  204. }
  205. idx = ring->slot[i].buf_idx;
  206. dst = (uint8_t *)NETMAP_BUF(ring, idx);
  207. ring->slot[i].len = nm_frag_size;
  208. ring->slot[i].flags = NS_MOREFRAG;
  209. pkt_copy(iov[j].iov_base + offset, dst, nm_frag_size);
  210. last = i;
  211. i = nm_ring_next(ring, i);
  212. offset += nm_frag_size;
  213. iov_frag_size -= nm_frag_size;
  214. }
  215. }
  216. /* The last slot must not have NS_MOREFRAG set. */
  217. ring->slot[last].flags &= ~NS_MOREFRAG;
  218. /* Now update ring->cur and ring->head. */
  219. ring->cur = ring->head = i;
  220. ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
  221. return iov_size(iov, iovcnt);
  222. }
  223. /* Complete a previous send (backend --> guest) and enable the
  224. fd_read callback. */
  225. static void netmap_send_completed(NetClientState *nc, ssize_t len)
  226. {
  227. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  228. netmap_read_poll(s, true);
  229. }
  230. static void netmap_send(void *opaque)
  231. {
  232. NetmapState *s = opaque;
  233. struct netmap_ring *ring = s->rx;
  234. /* Keep sending while there are available packets into the netmap
  235. RX ring and the forwarding path towards the peer is open. */
  236. while (!nm_ring_empty(ring)) {
  237. uint32_t i;
  238. uint32_t idx;
  239. bool morefrag;
  240. int iovcnt = 0;
  241. int iovsize;
  242. do {
  243. i = ring->cur;
  244. idx = ring->slot[i].buf_idx;
  245. morefrag = (ring->slot[i].flags & NS_MOREFRAG);
  246. s->iov[iovcnt].iov_base = (u_char *)NETMAP_BUF(ring, idx);
  247. s->iov[iovcnt].iov_len = ring->slot[i].len;
  248. iovcnt++;
  249. ring->cur = ring->head = nm_ring_next(ring, i);
  250. } while (!nm_ring_empty(ring) && morefrag);
  251. if (unlikely(nm_ring_empty(ring) && morefrag)) {
  252. RD(5, "[netmap_send] ran out of slots, with a pending"
  253. "incomplete packet\n");
  254. }
  255. iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
  256. netmap_send_completed);
  257. if (iovsize == 0) {
  258. /* The peer does not receive anymore. Packet is queued, stop
  259. * reading from the backend until netmap_send_completed()
  260. */
  261. netmap_read_poll(s, false);
  262. break;
  263. }
  264. }
  265. }
  266. /* Flush and close. */
  267. static void netmap_cleanup(NetClientState *nc)
  268. {
  269. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  270. qemu_purge_queued_packets(nc);
  271. netmap_poll(nc, false);
  272. nm_close(s->nmd);
  273. s->nmd = NULL;
  274. }
  275. /* Offloading manipulation support callbacks. */
  276. static int netmap_fd_set_vnet_hdr_len(NetmapState *s, int len)
  277. {
  278. struct nmreq req;
  279. /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
  280. * length for the netmap adapter associated to 's->ifname'.
  281. */
  282. memset(&req, 0, sizeof(req));
  283. pstrcpy(req.nr_name, sizeof(req.nr_name), s->ifname);
  284. req.nr_version = NETMAP_API;
  285. req.nr_cmd = NETMAP_BDG_VNET_HDR;
  286. req.nr_arg1 = len;
  287. return ioctl(s->nmd->fd, NIOCREGIF, &req);
  288. }
  289. static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
  290. {
  291. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  292. int prev_len = s->vnet_hdr_len;
  293. /* Check that we can set the new length. */
  294. if (netmap_fd_set_vnet_hdr_len(s, len)) {
  295. return false;
  296. }
  297. /* Restore the previous length. */
  298. if (netmap_fd_set_vnet_hdr_len(s, prev_len)) {
  299. error_report("Failed to restore vnet-hdr length %d on %s: %s",
  300. prev_len, s->ifname, strerror(errno));
  301. abort();
  302. }
  303. return true;
  304. }
  305. /* A netmap interface that supports virtio-net headers always
  306. * supports UFO, so we use this callback also for the has_ufo hook. */
  307. static bool netmap_has_vnet_hdr(NetClientState *nc)
  308. {
  309. return netmap_has_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
  310. }
  311. static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
  312. {
  313. }
  314. static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
  315. {
  316. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  317. int err;
  318. err = netmap_fd_set_vnet_hdr_len(s, len);
  319. if (err) {
  320. error_report("Unable to set vnet-hdr length %d on %s: %s",
  321. len, s->ifname, strerror(errno));
  322. } else {
  323. /* Keep track of the current length. */
  324. s->vnet_hdr_len = len;
  325. }
  326. }
  327. static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  328. int ecn, int ufo)
  329. {
  330. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  331. /* Setting a virtio-net header length greater than zero automatically
  332. * enables the offloadings. */
  333. if (!s->vnet_hdr_len) {
  334. netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
  335. }
  336. }
  337. /* NetClientInfo methods */
  338. static NetClientInfo net_netmap_info = {
  339. .type = NET_CLIENT_DRIVER_NETMAP,
  340. .size = sizeof(NetmapState),
  341. .receive = netmap_receive,
  342. .receive_iov = netmap_receive_iov,
  343. .poll = netmap_poll,
  344. .cleanup = netmap_cleanup,
  345. .has_ufo = netmap_has_vnet_hdr,
  346. .has_vnet_hdr = netmap_has_vnet_hdr,
  347. .has_vnet_hdr_len = netmap_has_vnet_hdr_len,
  348. .using_vnet_hdr = netmap_using_vnet_hdr,
  349. .set_offload = netmap_set_offload,
  350. .set_vnet_hdr_len = netmap_set_vnet_hdr_len,
  351. };
  352. /* The exported init function
  353. *
  354. * ... -net netmap,ifname="..."
  355. */
  356. int net_init_netmap(const Netdev *netdev,
  357. const char *name, NetClientState *peer, Error **errp)
  358. {
  359. const NetdevNetmapOptions *netmap_opts = &netdev->u.netmap;
  360. struct nm_desc *nmd;
  361. NetClientState *nc;
  362. Error *err = NULL;
  363. NetmapState *s;
  364. nmd = netmap_open(netmap_opts, &err);
  365. if (err) {
  366. error_propagate(errp, err);
  367. return -1;
  368. }
  369. /* Create the object. */
  370. nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name);
  371. s = DO_UPCAST(NetmapState, nc, nc);
  372. s->nmd = nmd;
  373. s->tx = NETMAP_TXRING(nmd->nifp, 0);
  374. s->rx = NETMAP_RXRING(nmd->nifp, 0);
  375. s->vnet_hdr_len = 0;
  376. pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
  377. netmap_read_poll(s, true); /* Initially only poll for reads. */
  378. return 0;
  379. }