netmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. #include <sys/mman.h>
  28. #define NETMAP_WITH_LIBS
  29. #include <net/netmap.h>
  30. #include <net/netmap_user.h>
  31. #include "net/net.h"
  32. #include "net/tap.h"
  33. #include "clients.h"
  34. #include "sysemu/sysemu.h"
  35. #include "qemu/error-report.h"
  36. #include "qemu/iov.h"
  37. typedef struct NetmapState {
  38. NetClientState nc;
  39. struct nm_desc *nmd;
  40. char ifname[IFNAMSIZ];
  41. struct netmap_ring *tx;
  42. struct netmap_ring *rx;
  43. bool read_poll;
  44. bool write_poll;
  45. struct iovec iov[IOV_MAX];
  46. int vnet_hdr_len; /* Current virtio-net header length. */
  47. } NetmapState;
  48. #ifndef __FreeBSD__
  49. #define pkt_copy bcopy
  50. #else
  51. /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
  52. static inline void
  53. pkt_copy(const void *_src, void *_dst, int l)
  54. {
  55. const uint64_t *src = _src;
  56. uint64_t *dst = _dst;
  57. if (unlikely(l >= 1024)) {
  58. bcopy(src, dst, l);
  59. return;
  60. }
  61. for (; l > 0; l -= 64) {
  62. *dst++ = *src++;
  63. *dst++ = *src++;
  64. *dst++ = *src++;
  65. *dst++ = *src++;
  66. *dst++ = *src++;
  67. *dst++ = *src++;
  68. *dst++ = *src++;
  69. *dst++ = *src++;
  70. }
  71. }
  72. #endif /* __FreeBSD__ */
  73. /*
  74. * Open a netmap device. We assume there is only one queue
  75. * (which is the case for the VALE bridge).
  76. */
  77. static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
  78. Error **errp)
  79. {
  80. struct nm_desc *nmd;
  81. struct nmreq req;
  82. memset(&req, 0, sizeof(req));
  83. nmd = nm_open(nm_opts->ifname, &req, NETMAP_NO_TX_POLL,
  84. NULL);
  85. if (nmd == NULL) {
  86. error_setg_errno(errp, errno, "Failed to nm_open() %s",
  87. nm_opts->ifname);
  88. return NULL;
  89. }
  90. return nmd;
  91. }
  92. static void netmap_send(void *opaque);
  93. static void netmap_writable(void *opaque);
  94. /* Set the event-loop handlers for the netmap backend. */
  95. static void netmap_update_fd_handler(NetmapState *s)
  96. {
  97. qemu_set_fd_handler(s->nmd->fd,
  98. s->read_poll ? netmap_send : NULL,
  99. s->write_poll ? netmap_writable : NULL,
  100. s);
  101. }
  102. /* Update the read handler. */
  103. static void netmap_read_poll(NetmapState *s, bool enable)
  104. {
  105. if (s->read_poll != enable) { /* Do nothing if not changed. */
  106. s->read_poll = enable;
  107. netmap_update_fd_handler(s);
  108. }
  109. }
  110. /* Update the write handler. */
  111. static void netmap_write_poll(NetmapState *s, bool enable)
  112. {
  113. if (s->write_poll != enable) {
  114. s->write_poll = enable;
  115. netmap_update_fd_handler(s);
  116. }
  117. }
  118. static void netmap_poll(NetClientState *nc, bool enable)
  119. {
  120. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  121. if (s->read_poll != enable || s->write_poll != enable) {
  122. s->write_poll = enable;
  123. s->read_poll = enable;
  124. netmap_update_fd_handler(s);
  125. }
  126. }
  127. /*
  128. * The fd_write() callback, invoked if the fd is marked as
  129. * writable after a poll. Unregister the handler and flush any
  130. * buffered packets.
  131. */
  132. static void netmap_writable(void *opaque)
  133. {
  134. NetmapState *s = opaque;
  135. netmap_write_poll(s, false);
  136. qemu_flush_queued_packets(&s->nc);
  137. }
  138. static ssize_t netmap_receive(NetClientState *nc,
  139. const uint8_t *buf, size_t size)
  140. {
  141. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  142. struct netmap_ring *ring = s->tx;
  143. uint32_t i;
  144. uint32_t idx;
  145. uint8_t *dst;
  146. if (unlikely(!ring)) {
  147. /* Drop. */
  148. return size;
  149. }
  150. if (unlikely(size > ring->nr_buf_size)) {
  151. RD(5, "[netmap_receive] drop packet of size %d > %d\n",
  152. (int)size, ring->nr_buf_size);
  153. return size;
  154. }
  155. if (nm_ring_empty(ring)) {
  156. /* No available slots in the netmap TX ring. */
  157. netmap_write_poll(s, true);
  158. return 0;
  159. }
  160. i = ring->cur;
  161. idx = ring->slot[i].buf_idx;
  162. dst = (uint8_t *)NETMAP_BUF(ring, idx);
  163. ring->slot[i].len = size;
  164. ring->slot[i].flags = 0;
  165. pkt_copy(buf, dst, size);
  166. ring->cur = ring->head = nm_ring_next(ring, i);
  167. ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
  168. return size;
  169. }
  170. static ssize_t netmap_receive_iov(NetClientState *nc,
  171. const struct iovec *iov, int iovcnt)
  172. {
  173. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  174. struct netmap_ring *ring = s->tx;
  175. uint32_t last;
  176. uint32_t idx;
  177. uint8_t *dst;
  178. int j;
  179. uint32_t i;
  180. if (unlikely(!ring)) {
  181. /* Drop the packet. */
  182. return iov_size(iov, iovcnt);
  183. }
  184. last = i = ring->cur;
  185. if (nm_ring_space(ring) < iovcnt) {
  186. /* Not enough netmap slots. */
  187. netmap_write_poll(s, true);
  188. return 0;
  189. }
  190. for (j = 0; j < iovcnt; j++) {
  191. int iov_frag_size = iov[j].iov_len;
  192. int offset = 0;
  193. int nm_frag_size;
  194. /* Split each iovec fragment over more netmap slots, if
  195. necessary. */
  196. while (iov_frag_size) {
  197. nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
  198. if (unlikely(nm_ring_empty(ring))) {
  199. /* We run out of netmap slots while splitting the
  200. iovec fragments. */
  201. netmap_write_poll(s, true);
  202. return 0;
  203. }
  204. idx = ring->slot[i].buf_idx;
  205. dst = (uint8_t *)NETMAP_BUF(ring, idx);
  206. ring->slot[i].len = nm_frag_size;
  207. ring->slot[i].flags = NS_MOREFRAG;
  208. pkt_copy(iov[j].iov_base + offset, dst, nm_frag_size);
  209. last = i;
  210. i = nm_ring_next(ring, i);
  211. offset += nm_frag_size;
  212. iov_frag_size -= nm_frag_size;
  213. }
  214. }
  215. /* The last slot must not have NS_MOREFRAG set. */
  216. ring->slot[last].flags &= ~NS_MOREFRAG;
  217. /* Now update ring->cur and ring->head. */
  218. ring->cur = ring->head = i;
  219. ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
  220. return iov_size(iov, iovcnt);
  221. }
  222. /* Complete a previous send (backend --> guest) and enable the
  223. fd_read callback. */
  224. static void netmap_send_completed(NetClientState *nc, ssize_t len)
  225. {
  226. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  227. netmap_read_poll(s, true);
  228. }
  229. static void netmap_send(void *opaque)
  230. {
  231. NetmapState *s = opaque;
  232. struct netmap_ring *ring = s->rx;
  233. /* Keep sending while there are available packets into the netmap
  234. RX ring and the forwarding path towards the peer is open. */
  235. while (!nm_ring_empty(ring)) {
  236. uint32_t i;
  237. uint32_t idx;
  238. bool morefrag;
  239. int iovcnt = 0;
  240. int iovsize;
  241. do {
  242. i = ring->cur;
  243. idx = ring->slot[i].buf_idx;
  244. morefrag = (ring->slot[i].flags & NS_MOREFRAG);
  245. s->iov[iovcnt].iov_base = (u_char *)NETMAP_BUF(ring, idx);
  246. s->iov[iovcnt].iov_len = ring->slot[i].len;
  247. iovcnt++;
  248. ring->cur = ring->head = nm_ring_next(ring, i);
  249. } while (!nm_ring_empty(ring) && morefrag);
  250. if (unlikely(nm_ring_empty(ring) && morefrag)) {
  251. RD(5, "[netmap_send] ran out of slots, with a pending"
  252. "incomplete packet\n");
  253. }
  254. iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
  255. netmap_send_completed);
  256. if (iovsize == 0) {
  257. /* The peer does not receive anymore. Packet is queued, stop
  258. * reading from the backend until netmap_send_completed()
  259. */
  260. netmap_read_poll(s, false);
  261. break;
  262. }
  263. }
  264. }
  265. /* Flush and close. */
  266. static void netmap_cleanup(NetClientState *nc)
  267. {
  268. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  269. qemu_purge_queued_packets(nc);
  270. netmap_poll(nc, false);
  271. nm_close(s->nmd);
  272. s->nmd = NULL;
  273. }
  274. /* Offloading manipulation support callbacks. */
  275. static bool netmap_has_ufo(NetClientState *nc)
  276. {
  277. return true;
  278. }
  279. static bool netmap_has_vnet_hdr(NetClientState *nc)
  280. {
  281. return true;
  282. }
  283. static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
  284. {
  285. return len == 0 || len == sizeof(struct virtio_net_hdr) ||
  286. len == sizeof(struct virtio_net_hdr_mrg_rxbuf);
  287. }
  288. static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
  289. {
  290. }
  291. static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
  292. {
  293. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  294. int err;
  295. struct nmreq req;
  296. /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
  297. * length for the netmap adapter associated to 's->ifname'.
  298. */
  299. memset(&req, 0, sizeof(req));
  300. pstrcpy(req.nr_name, sizeof(req.nr_name), s->ifname);
  301. req.nr_version = NETMAP_API;
  302. req.nr_cmd = NETMAP_BDG_VNET_HDR;
  303. req.nr_arg1 = len;
  304. err = ioctl(s->nmd->fd, NIOCREGIF, &req);
  305. if (err) {
  306. error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
  307. s->ifname, strerror(errno));
  308. } else {
  309. /* Keep track of the current length. */
  310. s->vnet_hdr_len = len;
  311. }
  312. }
  313. static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  314. int ecn, int ufo)
  315. {
  316. NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
  317. /* Setting a virtio-net header length greater than zero automatically
  318. * enables the offloadings.
  319. */
  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_OPTIONS_KIND_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_ufo,
  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 NetClientOptions *opts,
  344. const char *name, NetClientState *peer, Error **errp)
  345. {
  346. const NetdevNetmapOptions *netmap_opts = opts->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. }