vector_kern.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 - 2019 Cambridge Greys Limited
  4. * Copyright (C) 2011 - 2014 Cisco Systems Inc
  5. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  6. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  7. * James Leu (jleu@mindspring.net).
  8. * Copyright (C) 2001 by various other people who didn't put their name here.
  9. */
  10. #include <linux/memblock.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/inetdevice.h>
  14. #include <linux/init.h>
  15. #include <linux/list.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/firmware.h>
  23. #include <linux/fs.h>
  24. #include <asm/atomic.h>
  25. #include <uapi/linux/filter.h>
  26. #include <init.h>
  27. #include <irq_kern.h>
  28. #include <irq_user.h>
  29. #include <net_kern.h>
  30. #include <os.h>
  31. #include "mconsole_kern.h"
  32. #include "vector_user.h"
  33. #include "vector_kern.h"
  34. /*
  35. * Adapted from network devices with the following major changes:
  36. * All transports are static - simplifies the code significantly
  37. * Multiple FDs/IRQs per device
  38. * Vector IO optionally used for read/write, falling back to legacy
  39. * based on configuration and/or availability
  40. * Configuration is no longer positional - L2TPv3 and GRE require up to
  41. * 10 parameters, passing this as positional is not fit for purpose.
  42. * Only socket transports are supported
  43. */
  44. #define DRIVER_NAME "uml-vector"
  45. struct vector_cmd_line_arg {
  46. struct list_head list;
  47. int unit;
  48. char *arguments;
  49. };
  50. struct vector_device {
  51. struct list_head list;
  52. struct net_device *dev;
  53. struct platform_device pdev;
  54. int unit;
  55. int opened;
  56. };
  57. static LIST_HEAD(vec_cmd_line);
  58. static DEFINE_SPINLOCK(vector_devices_lock);
  59. static LIST_HEAD(vector_devices);
  60. static int driver_registered;
  61. static void vector_eth_configure(int n, struct arglist *def);
  62. static int vector_mmsg_rx(struct vector_private *vp, int budget);
  63. /* Argument accessors to set variables (and/or set default values)
  64. * mtu, buffer sizing, default headroom, etc
  65. */
  66. #define DEFAULT_HEADROOM 2
  67. #define SAFETY_MARGIN 32
  68. #define DEFAULT_VECTOR_SIZE 64
  69. #define TX_SMALL_PACKET 128
  70. #define MAX_IOV_SIZE (MAX_SKB_FRAGS + 1)
  71. static const struct {
  72. const char string[ETH_GSTRING_LEN];
  73. } ethtool_stats_keys[] = {
  74. { "rx_queue_max" },
  75. { "rx_queue_running_average" },
  76. { "tx_queue_max" },
  77. { "tx_queue_running_average" },
  78. { "rx_encaps_errors" },
  79. { "tx_timeout_count" },
  80. { "tx_restart_queue" },
  81. { "tx_kicks" },
  82. { "tx_flow_control_xon" },
  83. { "tx_flow_control_xoff" },
  84. { "rx_csum_offload_good" },
  85. { "rx_csum_offload_errors"},
  86. { "sg_ok"},
  87. { "sg_linearized"},
  88. };
  89. #define VECTOR_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
  90. static void vector_reset_stats(struct vector_private *vp)
  91. {
  92. /* We reuse the existing queue locks for stats */
  93. /* RX stats are modified with RX head_lock held
  94. * in vector_poll.
  95. */
  96. spin_lock(&vp->rx_queue->head_lock);
  97. vp->estats.rx_queue_max = 0;
  98. vp->estats.rx_queue_running_average = 0;
  99. vp->estats.rx_encaps_errors = 0;
  100. vp->estats.sg_ok = 0;
  101. vp->estats.sg_linearized = 0;
  102. spin_unlock(&vp->rx_queue->head_lock);
  103. /* TX stats are modified with TX head_lock held
  104. * in vector_send.
  105. */
  106. spin_lock(&vp->tx_queue->head_lock);
  107. vp->estats.tx_timeout_count = 0;
  108. vp->estats.tx_restart_queue = 0;
  109. vp->estats.tx_kicks = 0;
  110. vp->estats.tx_flow_control_xon = 0;
  111. vp->estats.tx_flow_control_xoff = 0;
  112. vp->estats.tx_queue_max = 0;
  113. vp->estats.tx_queue_running_average = 0;
  114. spin_unlock(&vp->tx_queue->head_lock);
  115. }
  116. static int get_mtu(struct arglist *def)
  117. {
  118. char *mtu = uml_vector_fetch_arg(def, "mtu");
  119. long result;
  120. if (mtu != NULL) {
  121. if (kstrtoul(mtu, 10, &result) == 0)
  122. if ((result < (1 << 16) - 1) && (result >= 576))
  123. return result;
  124. }
  125. return ETH_MAX_PACKET;
  126. }
  127. static char *get_bpf_file(struct arglist *def)
  128. {
  129. return uml_vector_fetch_arg(def, "bpffile");
  130. }
  131. static bool get_bpf_flash(struct arglist *def)
  132. {
  133. char *allow = uml_vector_fetch_arg(def, "bpfflash");
  134. long result;
  135. if (allow != NULL) {
  136. if (kstrtoul(allow, 10, &result) == 0)
  137. return result > 0;
  138. }
  139. return false;
  140. }
  141. static int get_depth(struct arglist *def)
  142. {
  143. char *mtu = uml_vector_fetch_arg(def, "depth");
  144. long result;
  145. if (mtu != NULL) {
  146. if (kstrtoul(mtu, 10, &result) == 0)
  147. return result;
  148. }
  149. return DEFAULT_VECTOR_SIZE;
  150. }
  151. static int get_headroom(struct arglist *def)
  152. {
  153. char *mtu = uml_vector_fetch_arg(def, "headroom");
  154. long result;
  155. if (mtu != NULL) {
  156. if (kstrtoul(mtu, 10, &result) == 0)
  157. return result;
  158. }
  159. return DEFAULT_HEADROOM;
  160. }
  161. static int get_req_size(struct arglist *def)
  162. {
  163. char *gro = uml_vector_fetch_arg(def, "gro");
  164. long result;
  165. if (gro != NULL) {
  166. if (kstrtoul(gro, 10, &result) == 0) {
  167. if (result > 0)
  168. return 65536;
  169. }
  170. }
  171. return get_mtu(def) + ETH_HEADER_OTHER +
  172. get_headroom(def) + SAFETY_MARGIN;
  173. }
  174. static int get_transport_options(struct arglist *def)
  175. {
  176. char *transport = uml_vector_fetch_arg(def, "transport");
  177. char *vector = uml_vector_fetch_arg(def, "vec");
  178. int vec_rx = VECTOR_RX;
  179. int vec_tx = VECTOR_TX;
  180. long parsed;
  181. int result = 0;
  182. if (transport == NULL)
  183. return -EINVAL;
  184. if (vector != NULL) {
  185. if (kstrtoul(vector, 10, &parsed) == 0) {
  186. if (parsed == 0) {
  187. vec_rx = 0;
  188. vec_tx = 0;
  189. }
  190. }
  191. }
  192. if (get_bpf_flash(def))
  193. result = VECTOR_BPF_FLASH;
  194. if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
  195. return result;
  196. if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
  197. return (result | vec_rx | VECTOR_BPF);
  198. if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
  199. return (result | vec_rx | vec_tx | VECTOR_QDISC_BYPASS);
  200. return (result | vec_rx | vec_tx);
  201. }
  202. /* A mini-buffer for packet drop read
  203. * All of our supported transports are datagram oriented and we always
  204. * read using recvmsg or recvmmsg. If we pass a buffer which is smaller
  205. * than the packet size it still counts as full packet read and will
  206. * clean the incoming stream to keep sigio/epoll happy
  207. */
  208. #define DROP_BUFFER_SIZE 32
  209. static char *drop_buffer;
  210. /*
  211. * Advance the mmsg queue head by n = advance. Resets the queue to
  212. * maximum enqueue/dequeue-at-once capacity if possible. Called by
  213. * dequeuers. Caller must hold the head_lock!
  214. */
  215. static int vector_advancehead(struct vector_queue *qi, int advance)
  216. {
  217. qi->head =
  218. (qi->head + advance)
  219. % qi->max_depth;
  220. atomic_sub(advance, &qi->queue_depth);
  221. return atomic_read(&qi->queue_depth);
  222. }
  223. /* Advance the queue tail by n = advance.
  224. * This is called by enqueuers which should hold the
  225. * head lock already
  226. */
  227. static int vector_advancetail(struct vector_queue *qi, int advance)
  228. {
  229. qi->tail =
  230. (qi->tail + advance)
  231. % qi->max_depth;
  232. atomic_add(advance, &qi->queue_depth);
  233. return atomic_read(&qi->queue_depth);
  234. }
  235. static int prep_msg(struct vector_private *vp,
  236. struct sk_buff *skb,
  237. struct iovec *iov)
  238. {
  239. int iov_index = 0;
  240. int nr_frags, frag;
  241. skb_frag_t *skb_frag;
  242. nr_frags = skb_shinfo(skb)->nr_frags;
  243. if (nr_frags > MAX_IOV_SIZE) {
  244. if (skb_linearize(skb) != 0)
  245. goto drop;
  246. }
  247. if (vp->header_size > 0) {
  248. iov[iov_index].iov_len = vp->header_size;
  249. vp->form_header(iov[iov_index].iov_base, skb, vp);
  250. iov_index++;
  251. }
  252. iov[iov_index].iov_base = skb->data;
  253. if (nr_frags > 0) {
  254. iov[iov_index].iov_len = skb->len - skb->data_len;
  255. vp->estats.sg_ok++;
  256. } else
  257. iov[iov_index].iov_len = skb->len;
  258. iov_index++;
  259. for (frag = 0; frag < nr_frags; frag++) {
  260. skb_frag = &skb_shinfo(skb)->frags[frag];
  261. iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
  262. iov[iov_index].iov_len = skb_frag_size(skb_frag);
  263. iov_index++;
  264. }
  265. return iov_index;
  266. drop:
  267. return -1;
  268. }
  269. /*
  270. * Generic vector enqueue with support for forming headers using transport
  271. * specific callback. Allows GRE, L2TPv3, RAW and other transports
  272. * to use a common enqueue procedure in vector mode
  273. */
  274. static int vector_enqueue(struct vector_queue *qi, struct sk_buff *skb)
  275. {
  276. struct vector_private *vp = netdev_priv(qi->dev);
  277. int queue_depth;
  278. int packet_len;
  279. struct mmsghdr *mmsg_vector = qi->mmsg_vector;
  280. int iov_count;
  281. spin_lock(&qi->tail_lock);
  282. queue_depth = atomic_read(&qi->queue_depth);
  283. if (skb)
  284. packet_len = skb->len;
  285. if (queue_depth < qi->max_depth) {
  286. *(qi->skbuff_vector + qi->tail) = skb;
  287. mmsg_vector += qi->tail;
  288. iov_count = prep_msg(
  289. vp,
  290. skb,
  291. mmsg_vector->msg_hdr.msg_iov
  292. );
  293. if (iov_count < 1)
  294. goto drop;
  295. mmsg_vector->msg_hdr.msg_iovlen = iov_count;
  296. mmsg_vector->msg_hdr.msg_name = vp->fds->remote_addr;
  297. mmsg_vector->msg_hdr.msg_namelen = vp->fds->remote_addr_size;
  298. wmb(); /* Make the packet visible to the NAPI poll thread */
  299. queue_depth = vector_advancetail(qi, 1);
  300. } else
  301. goto drop;
  302. spin_unlock(&qi->tail_lock);
  303. return queue_depth;
  304. drop:
  305. qi->dev->stats.tx_dropped++;
  306. if (skb != NULL) {
  307. packet_len = skb->len;
  308. dev_consume_skb_any(skb);
  309. netdev_completed_queue(qi->dev, 1, packet_len);
  310. }
  311. spin_unlock(&qi->tail_lock);
  312. return queue_depth;
  313. }
  314. static int consume_vector_skbs(struct vector_queue *qi, int count)
  315. {
  316. struct sk_buff *skb;
  317. int skb_index;
  318. int bytes_compl = 0;
  319. for (skb_index = qi->head; skb_index < qi->head + count; skb_index++) {
  320. skb = *(qi->skbuff_vector + skb_index);
  321. /* mark as empty to ensure correct destruction if
  322. * needed
  323. */
  324. bytes_compl += skb->len;
  325. *(qi->skbuff_vector + skb_index) = NULL;
  326. dev_consume_skb_any(skb);
  327. }
  328. qi->dev->stats.tx_bytes += bytes_compl;
  329. qi->dev->stats.tx_packets += count;
  330. netdev_completed_queue(qi->dev, count, bytes_compl);
  331. return vector_advancehead(qi, count);
  332. }
  333. /*
  334. * Generic vector dequeue via sendmmsg with support for forming headers
  335. * using transport specific callback. Allows GRE, L2TPv3, RAW and
  336. * other transports to use a common dequeue procedure in vector mode
  337. */
  338. static int vector_send(struct vector_queue *qi)
  339. {
  340. struct vector_private *vp = netdev_priv(qi->dev);
  341. struct mmsghdr *send_from;
  342. int result = 0, send_len;
  343. if (spin_trylock(&qi->head_lock)) {
  344. /* update queue_depth to current value */
  345. while (atomic_read(&qi->queue_depth) > 0) {
  346. /* Calculate the start of the vector */
  347. send_len = atomic_read(&qi->queue_depth);
  348. send_from = qi->mmsg_vector;
  349. send_from += qi->head;
  350. /* Adjust vector size if wraparound */
  351. if (send_len + qi->head > qi->max_depth)
  352. send_len = qi->max_depth - qi->head;
  353. /* Try to TX as many packets as possible */
  354. if (send_len > 0) {
  355. result = uml_vector_sendmmsg(
  356. vp->fds->tx_fd,
  357. send_from,
  358. send_len,
  359. 0
  360. );
  361. vp->in_write_poll =
  362. (result != send_len);
  363. }
  364. /* For some of the sendmmsg error scenarios
  365. * we may end being unsure in the TX success
  366. * for all packets. It is safer to declare
  367. * them all TX-ed and blame the network.
  368. */
  369. if (result < 0) {
  370. if (net_ratelimit())
  371. netdev_err(vp->dev, "sendmmsg err=%i\n",
  372. result);
  373. vp->in_error = true;
  374. result = send_len;
  375. }
  376. if (result > 0) {
  377. consume_vector_skbs(qi, result);
  378. /* This is equivalent to an TX IRQ.
  379. * Restart the upper layers to feed us
  380. * more packets.
  381. */
  382. if (result > vp->estats.tx_queue_max)
  383. vp->estats.tx_queue_max = result;
  384. vp->estats.tx_queue_running_average =
  385. (vp->estats.tx_queue_running_average + result) >> 1;
  386. }
  387. netif_wake_queue(qi->dev);
  388. /* if TX is busy, break out of the send loop,
  389. * poll write IRQ will reschedule xmit for us.
  390. */
  391. if (result != send_len) {
  392. vp->estats.tx_restart_queue++;
  393. break;
  394. }
  395. }
  396. spin_unlock(&qi->head_lock);
  397. }
  398. return atomic_read(&qi->queue_depth);
  399. }
  400. /* Queue destructor. Deliberately stateless so we can use
  401. * it in queue cleanup if initialization fails.
  402. */
  403. static void destroy_queue(struct vector_queue *qi)
  404. {
  405. int i;
  406. struct iovec *iov;
  407. struct vector_private *vp = netdev_priv(qi->dev);
  408. struct mmsghdr *mmsg_vector;
  409. if (qi == NULL)
  410. return;
  411. /* deallocate any skbuffs - we rely on any unused to be
  412. * set to NULL.
  413. */
  414. if (qi->skbuff_vector != NULL) {
  415. for (i = 0; i < qi->max_depth; i++) {
  416. if (*(qi->skbuff_vector + i) != NULL)
  417. dev_kfree_skb_any(*(qi->skbuff_vector + i));
  418. }
  419. kfree(qi->skbuff_vector);
  420. }
  421. /* deallocate matching IOV structures including header buffs */
  422. if (qi->mmsg_vector != NULL) {
  423. mmsg_vector = qi->mmsg_vector;
  424. for (i = 0; i < qi->max_depth; i++) {
  425. iov = mmsg_vector->msg_hdr.msg_iov;
  426. if (iov != NULL) {
  427. if ((vp->header_size > 0) &&
  428. (iov->iov_base != NULL))
  429. kfree(iov->iov_base);
  430. kfree(iov);
  431. }
  432. mmsg_vector++;
  433. }
  434. kfree(qi->mmsg_vector);
  435. }
  436. kfree(qi);
  437. }
  438. /*
  439. * Queue constructor. Create a queue with a given side.
  440. */
  441. static struct vector_queue *create_queue(
  442. struct vector_private *vp,
  443. int max_size,
  444. int header_size,
  445. int num_extra_frags)
  446. {
  447. struct vector_queue *result;
  448. int i;
  449. struct iovec *iov;
  450. struct mmsghdr *mmsg_vector;
  451. result = kmalloc(sizeof(struct vector_queue), GFP_KERNEL);
  452. if (result == NULL)
  453. return NULL;
  454. result->max_depth = max_size;
  455. result->dev = vp->dev;
  456. result->mmsg_vector = kmalloc(
  457. (sizeof(struct mmsghdr) * max_size), GFP_KERNEL);
  458. if (result->mmsg_vector == NULL)
  459. goto out_mmsg_fail;
  460. result->skbuff_vector = kmalloc(
  461. (sizeof(void *) * max_size), GFP_KERNEL);
  462. if (result->skbuff_vector == NULL)
  463. goto out_skb_fail;
  464. /* further failures can be handled safely by destroy_queue*/
  465. mmsg_vector = result->mmsg_vector;
  466. for (i = 0; i < max_size; i++) {
  467. /* Clear all pointers - we use non-NULL as marking on
  468. * what to free on destruction
  469. */
  470. *(result->skbuff_vector + i) = NULL;
  471. mmsg_vector->msg_hdr.msg_iov = NULL;
  472. mmsg_vector++;
  473. }
  474. mmsg_vector = result->mmsg_vector;
  475. result->max_iov_frags = num_extra_frags;
  476. for (i = 0; i < max_size; i++) {
  477. if (vp->header_size > 0)
  478. iov = kmalloc_array(3 + num_extra_frags,
  479. sizeof(struct iovec),
  480. GFP_KERNEL
  481. );
  482. else
  483. iov = kmalloc_array(2 + num_extra_frags,
  484. sizeof(struct iovec),
  485. GFP_KERNEL
  486. );
  487. if (iov == NULL)
  488. goto out_fail;
  489. mmsg_vector->msg_hdr.msg_iov = iov;
  490. mmsg_vector->msg_hdr.msg_iovlen = 1;
  491. mmsg_vector->msg_hdr.msg_control = NULL;
  492. mmsg_vector->msg_hdr.msg_controllen = 0;
  493. mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT;
  494. mmsg_vector->msg_hdr.msg_name = NULL;
  495. mmsg_vector->msg_hdr.msg_namelen = 0;
  496. if (vp->header_size > 0) {
  497. iov->iov_base = kmalloc(header_size, GFP_KERNEL);
  498. if (iov->iov_base == NULL)
  499. goto out_fail;
  500. iov->iov_len = header_size;
  501. mmsg_vector->msg_hdr.msg_iovlen = 2;
  502. iov++;
  503. }
  504. iov->iov_base = NULL;
  505. iov->iov_len = 0;
  506. mmsg_vector++;
  507. }
  508. spin_lock_init(&result->head_lock);
  509. spin_lock_init(&result->tail_lock);
  510. atomic_set(&result->queue_depth, 0);
  511. result->head = 0;
  512. result->tail = 0;
  513. return result;
  514. out_skb_fail:
  515. kfree(result->mmsg_vector);
  516. out_mmsg_fail:
  517. kfree(result);
  518. return NULL;
  519. out_fail:
  520. destroy_queue(result);
  521. return NULL;
  522. }
  523. /*
  524. * We do not use the RX queue as a proper wraparound queue for now
  525. * This is not necessary because the consumption via napi_gro_receive()
  526. * happens in-line. While we can try using the return code of
  527. * netif_rx() for flow control there are no drivers doing this today.
  528. * For this RX specific use we ignore the tail/head locks and
  529. * just read into a prepared queue filled with skbuffs.
  530. */
  531. static struct sk_buff *prep_skb(
  532. struct vector_private *vp,
  533. struct user_msghdr *msg)
  534. {
  535. int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN;
  536. struct sk_buff *result;
  537. int iov_index = 0, len;
  538. struct iovec *iov = msg->msg_iov;
  539. int err, nr_frags, frag;
  540. skb_frag_t *skb_frag;
  541. if (vp->req_size <= linear)
  542. len = linear;
  543. else
  544. len = vp->req_size;
  545. result = alloc_skb_with_frags(
  546. linear,
  547. len - vp->max_packet,
  548. 3,
  549. &err,
  550. GFP_ATOMIC
  551. );
  552. if (vp->header_size > 0)
  553. iov_index++;
  554. if (result == NULL) {
  555. iov[iov_index].iov_base = NULL;
  556. iov[iov_index].iov_len = 0;
  557. goto done;
  558. }
  559. skb_reserve(result, vp->headroom);
  560. result->dev = vp->dev;
  561. skb_put(result, vp->max_packet);
  562. result->data_len = len - vp->max_packet;
  563. result->len += len - vp->max_packet;
  564. skb_reset_mac_header(result);
  565. result->ip_summed = CHECKSUM_NONE;
  566. iov[iov_index].iov_base = result->data;
  567. iov[iov_index].iov_len = vp->max_packet;
  568. iov_index++;
  569. nr_frags = skb_shinfo(result)->nr_frags;
  570. for (frag = 0; frag < nr_frags; frag++) {
  571. skb_frag = &skb_shinfo(result)->frags[frag];
  572. iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
  573. if (iov[iov_index].iov_base != NULL)
  574. iov[iov_index].iov_len = skb_frag_size(skb_frag);
  575. else
  576. iov[iov_index].iov_len = 0;
  577. iov_index++;
  578. }
  579. done:
  580. msg->msg_iovlen = iov_index;
  581. return result;
  582. }
  583. /* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs */
  584. static void prep_queue_for_rx(struct vector_queue *qi)
  585. {
  586. struct vector_private *vp = netdev_priv(qi->dev);
  587. struct mmsghdr *mmsg_vector = qi->mmsg_vector;
  588. void **skbuff_vector = qi->skbuff_vector;
  589. int i, queue_depth;
  590. queue_depth = atomic_read(&qi->queue_depth);
  591. if (queue_depth == 0)
  592. return;
  593. /* RX is always emptied 100% during each cycle, so we do not
  594. * have to do the tail wraparound math for it.
  595. */
  596. qi->head = qi->tail = 0;
  597. for (i = 0; i < queue_depth; i++) {
  598. /* it is OK if allocation fails - recvmmsg with NULL data in
  599. * iov argument still performs an RX, just drops the packet
  600. * This allows us stop faffing around with a "drop buffer"
  601. */
  602. *skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr);
  603. skbuff_vector++;
  604. mmsg_vector++;
  605. }
  606. atomic_set(&qi->queue_depth, 0);
  607. }
  608. static struct vector_device *find_device(int n)
  609. {
  610. struct vector_device *device;
  611. struct list_head *ele;
  612. spin_lock(&vector_devices_lock);
  613. list_for_each(ele, &vector_devices) {
  614. device = list_entry(ele, struct vector_device, list);
  615. if (device->unit == n)
  616. goto out;
  617. }
  618. device = NULL;
  619. out:
  620. spin_unlock(&vector_devices_lock);
  621. return device;
  622. }
  623. static int vector_parse(char *str, int *index_out, char **str_out,
  624. char **error_out)
  625. {
  626. int n, err;
  627. char *start = str;
  628. while ((*str != ':') && (strlen(str) > 1))
  629. str++;
  630. if (*str != ':') {
  631. *error_out = "Expected ':' after device number";
  632. return -EINVAL;
  633. }
  634. *str = '\0';
  635. err = kstrtouint(start, 0, &n);
  636. if (err < 0) {
  637. *error_out = "Bad device number";
  638. return err;
  639. }
  640. str++;
  641. if (find_device(n)) {
  642. *error_out = "Device already configured";
  643. return -EINVAL;
  644. }
  645. *index_out = n;
  646. *str_out = str;
  647. return 0;
  648. }
  649. static int vector_config(char *str, char **error_out)
  650. {
  651. int err, n;
  652. char *params;
  653. struct arglist *parsed;
  654. err = vector_parse(str, &n, &params, error_out);
  655. if (err != 0)
  656. return err;
  657. /* This string is broken up and the pieces used by the underlying
  658. * driver. We should copy it to make sure things do not go wrong
  659. * later.
  660. */
  661. params = kstrdup(params, GFP_KERNEL);
  662. if (params == NULL) {
  663. *error_out = "vector_config failed to strdup string";
  664. return -ENOMEM;
  665. }
  666. parsed = uml_parse_vector_ifspec(params);
  667. if (parsed == NULL) {
  668. *error_out = "vector_config failed to parse parameters";
  669. kfree(params);
  670. return -EINVAL;
  671. }
  672. vector_eth_configure(n, parsed);
  673. return 0;
  674. }
  675. static int vector_id(char **str, int *start_out, int *end_out)
  676. {
  677. char *end;
  678. int n;
  679. n = simple_strtoul(*str, &end, 0);
  680. if ((*end != '\0') || (end == *str))
  681. return -1;
  682. *start_out = n;
  683. *end_out = n;
  684. *str = end;
  685. return n;
  686. }
  687. static int vector_remove(int n, char **error_out)
  688. {
  689. struct vector_device *vec_d;
  690. struct net_device *dev;
  691. struct vector_private *vp;
  692. vec_d = find_device(n);
  693. if (vec_d == NULL)
  694. return -ENODEV;
  695. dev = vec_d->dev;
  696. vp = netdev_priv(dev);
  697. if (vp->fds != NULL)
  698. return -EBUSY;
  699. unregister_netdev(dev);
  700. platform_device_unregister(&vec_d->pdev);
  701. return 0;
  702. }
  703. /*
  704. * There is no shared per-transport initialization code, so
  705. * we will just initialize each interface one by one and
  706. * add them to a list
  707. */
  708. static struct platform_driver uml_net_driver = {
  709. .driver = {
  710. .name = DRIVER_NAME,
  711. },
  712. };
  713. static void vector_device_release(struct device *dev)
  714. {
  715. struct vector_device *device =
  716. container_of(dev, struct vector_device, pdev.dev);
  717. struct net_device *netdev = device->dev;
  718. list_del(&device->list);
  719. kfree(device);
  720. free_netdev(netdev);
  721. }
  722. /* Bog standard recv using recvmsg - not used normally unless the user
  723. * explicitly specifies not to use recvmmsg vector RX.
  724. */
  725. static int vector_legacy_rx(struct vector_private *vp)
  726. {
  727. int pkt_len;
  728. struct user_msghdr hdr;
  729. struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */
  730. int iovpos = 0;
  731. struct sk_buff *skb;
  732. int header_check;
  733. hdr.msg_name = NULL;
  734. hdr.msg_namelen = 0;
  735. hdr.msg_iov = (struct iovec *) &iov;
  736. hdr.msg_control = NULL;
  737. hdr.msg_controllen = 0;
  738. hdr.msg_flags = 0;
  739. if (vp->header_size > 0) {
  740. iov[0].iov_base = vp->header_rxbuffer;
  741. iov[0].iov_len = vp->header_size;
  742. }
  743. skb = prep_skb(vp, &hdr);
  744. if (skb == NULL) {
  745. /* Read a packet into drop_buffer and don't do
  746. * anything with it.
  747. */
  748. iov[iovpos].iov_base = drop_buffer;
  749. iov[iovpos].iov_len = DROP_BUFFER_SIZE;
  750. hdr.msg_iovlen = 1;
  751. vp->dev->stats.rx_dropped++;
  752. }
  753. pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0);
  754. if (pkt_len < 0) {
  755. vp->in_error = true;
  756. return pkt_len;
  757. }
  758. if (skb != NULL) {
  759. if (pkt_len > vp->header_size) {
  760. if (vp->header_size > 0) {
  761. header_check = vp->verify_header(
  762. vp->header_rxbuffer, skb, vp);
  763. if (header_check < 0) {
  764. dev_kfree_skb_irq(skb);
  765. vp->dev->stats.rx_dropped++;
  766. vp->estats.rx_encaps_errors++;
  767. return 0;
  768. }
  769. if (header_check > 0) {
  770. vp->estats.rx_csum_offload_good++;
  771. skb->ip_summed = CHECKSUM_UNNECESSARY;
  772. }
  773. }
  774. pskb_trim(skb, pkt_len - vp->rx_header_size);
  775. skb->protocol = eth_type_trans(skb, skb->dev);
  776. vp->dev->stats.rx_bytes += skb->len;
  777. vp->dev->stats.rx_packets++;
  778. napi_gro_receive(&vp->napi, skb);
  779. } else {
  780. dev_kfree_skb_irq(skb);
  781. }
  782. }
  783. return pkt_len;
  784. }
  785. /*
  786. * Packet at a time TX which falls back to vector TX if the
  787. * underlying transport is busy.
  788. */
  789. static int writev_tx(struct vector_private *vp, struct sk_buff *skb)
  790. {
  791. struct iovec iov[3 + MAX_IOV_SIZE];
  792. int iov_count, pkt_len = 0;
  793. iov[0].iov_base = vp->header_txbuffer;
  794. iov_count = prep_msg(vp, skb, (struct iovec *) &iov);
  795. if (iov_count < 1)
  796. goto drop;
  797. pkt_len = uml_vector_writev(
  798. vp->fds->tx_fd,
  799. (struct iovec *) &iov,
  800. iov_count
  801. );
  802. if (pkt_len < 0)
  803. goto drop;
  804. netif_trans_update(vp->dev);
  805. netif_wake_queue(vp->dev);
  806. if (pkt_len > 0) {
  807. vp->dev->stats.tx_bytes += skb->len;
  808. vp->dev->stats.tx_packets++;
  809. } else {
  810. vp->dev->stats.tx_dropped++;
  811. }
  812. consume_skb(skb);
  813. return pkt_len;
  814. drop:
  815. vp->dev->stats.tx_dropped++;
  816. consume_skb(skb);
  817. if (pkt_len < 0)
  818. vp->in_error = true;
  819. return pkt_len;
  820. }
  821. /*
  822. * Receive as many messages as we can in one call using the special
  823. * mmsg vector matched to an skb vector which we prepared earlier.
  824. */
  825. static int vector_mmsg_rx(struct vector_private *vp, int budget)
  826. {
  827. int packet_count, i;
  828. struct vector_queue *qi = vp->rx_queue;
  829. struct sk_buff *skb;
  830. struct mmsghdr *mmsg_vector = qi->mmsg_vector;
  831. void **skbuff_vector = qi->skbuff_vector;
  832. int header_check;
  833. /* Refresh the vector and make sure it is with new skbs and the
  834. * iovs are updated to point to them.
  835. */
  836. prep_queue_for_rx(qi);
  837. /* Fire the Lazy Gun - get as many packets as we can in one go. */
  838. if (budget > qi->max_depth)
  839. budget = qi->max_depth;
  840. packet_count = uml_vector_recvmmsg(
  841. vp->fds->rx_fd, qi->mmsg_vector, budget, 0);
  842. if (packet_count < 0)
  843. vp->in_error = true;
  844. if (packet_count <= 0)
  845. return packet_count;
  846. /* We treat packet processing as enqueue, buffer refresh as dequeue
  847. * The queue_depth tells us how many buffers have been used and how
  848. * many do we need to prep the next time prep_queue_for_rx() is called.
  849. */
  850. atomic_add(packet_count, &qi->queue_depth);
  851. for (i = 0; i < packet_count; i++) {
  852. skb = (*skbuff_vector);
  853. if (mmsg_vector->msg_len > vp->header_size) {
  854. if (vp->header_size > 0) {
  855. header_check = vp->verify_header(
  856. mmsg_vector->msg_hdr.msg_iov->iov_base,
  857. skb,
  858. vp
  859. );
  860. if (header_check < 0) {
  861. /* Overlay header failed to verify - discard.
  862. * We can actually keep this skb and reuse it,
  863. * but that will make the prep logic too
  864. * complex.
  865. */
  866. dev_kfree_skb_irq(skb);
  867. vp->estats.rx_encaps_errors++;
  868. continue;
  869. }
  870. if (header_check > 0) {
  871. vp->estats.rx_csum_offload_good++;
  872. skb->ip_summed = CHECKSUM_UNNECESSARY;
  873. }
  874. }
  875. pskb_trim(skb,
  876. mmsg_vector->msg_len - vp->rx_header_size);
  877. skb->protocol = eth_type_trans(skb, skb->dev);
  878. /*
  879. * We do not need to lock on updating stats here
  880. * The interrupt loop is non-reentrant.
  881. */
  882. vp->dev->stats.rx_bytes += skb->len;
  883. vp->dev->stats.rx_packets++;
  884. napi_gro_receive(&vp->napi, skb);
  885. } else {
  886. /* Overlay header too short to do anything - discard.
  887. * We can actually keep this skb and reuse it,
  888. * but that will make the prep logic too complex.
  889. */
  890. if (skb != NULL)
  891. dev_kfree_skb_irq(skb);
  892. }
  893. (*skbuff_vector) = NULL;
  894. /* Move to the next buffer element */
  895. mmsg_vector++;
  896. skbuff_vector++;
  897. }
  898. if (packet_count > 0) {
  899. if (vp->estats.rx_queue_max < packet_count)
  900. vp->estats.rx_queue_max = packet_count;
  901. vp->estats.rx_queue_running_average =
  902. (vp->estats.rx_queue_running_average + packet_count) >> 1;
  903. }
  904. return packet_count;
  905. }
  906. static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  907. {
  908. struct vector_private *vp = netdev_priv(dev);
  909. int queue_depth = 0;
  910. if (vp->in_error) {
  911. deactivate_fd(vp->fds->rx_fd, vp->rx_irq);
  912. if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0))
  913. deactivate_fd(vp->fds->tx_fd, vp->tx_irq);
  914. return NETDEV_TX_BUSY;
  915. }
  916. if ((vp->options & VECTOR_TX) == 0) {
  917. writev_tx(vp, skb);
  918. return NETDEV_TX_OK;
  919. }
  920. /* We do BQL only in the vector path, no point doing it in
  921. * packet at a time mode as there is no device queue
  922. */
  923. netdev_sent_queue(vp->dev, skb->len);
  924. queue_depth = vector_enqueue(vp->tx_queue, skb);
  925. if (queue_depth < vp->tx_queue->max_depth && netdev_xmit_more()) {
  926. mod_timer(&vp->tl, vp->coalesce);
  927. return NETDEV_TX_OK;
  928. } else {
  929. queue_depth = vector_send(vp->tx_queue);
  930. if (queue_depth > 0)
  931. napi_schedule(&vp->napi);
  932. }
  933. return NETDEV_TX_OK;
  934. }
  935. static irqreturn_t vector_rx_interrupt(int irq, void *dev_id)
  936. {
  937. struct net_device *dev = dev_id;
  938. struct vector_private *vp = netdev_priv(dev);
  939. if (!netif_running(dev))
  940. return IRQ_NONE;
  941. napi_schedule(&vp->napi);
  942. return IRQ_HANDLED;
  943. }
  944. static irqreturn_t vector_tx_interrupt(int irq, void *dev_id)
  945. {
  946. struct net_device *dev = dev_id;
  947. struct vector_private *vp = netdev_priv(dev);
  948. if (!netif_running(dev))
  949. return IRQ_NONE;
  950. /* We need to pay attention to it only if we got
  951. * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise
  952. * we ignore it. In the future, it may be worth
  953. * it to improve the IRQ controller a bit to make
  954. * tweaking the IRQ mask less costly
  955. */
  956. napi_schedule(&vp->napi);
  957. return IRQ_HANDLED;
  958. }
  959. static int irq_rr;
  960. static int vector_net_close(struct net_device *dev)
  961. {
  962. struct vector_private *vp = netdev_priv(dev);
  963. netif_stop_queue(dev);
  964. del_timer(&vp->tl);
  965. vp->opened = false;
  966. if (vp->fds == NULL)
  967. return 0;
  968. /* Disable and free all IRQS */
  969. if (vp->rx_irq > 0) {
  970. um_free_irq(vp->rx_irq, dev);
  971. vp->rx_irq = 0;
  972. }
  973. if (vp->tx_irq > 0) {
  974. um_free_irq(vp->tx_irq, dev);
  975. vp->tx_irq = 0;
  976. }
  977. napi_disable(&vp->napi);
  978. netif_napi_del(&vp->napi);
  979. if (vp->fds->rx_fd > 0) {
  980. if (vp->bpf)
  981. uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
  982. os_close_file(vp->fds->rx_fd);
  983. vp->fds->rx_fd = -1;
  984. }
  985. if (vp->fds->tx_fd > 0) {
  986. os_close_file(vp->fds->tx_fd);
  987. vp->fds->tx_fd = -1;
  988. }
  989. if (vp->bpf != NULL)
  990. kfree(vp->bpf->filter);
  991. kfree(vp->bpf);
  992. vp->bpf = NULL;
  993. kfree(vp->fds->remote_addr);
  994. kfree(vp->transport_data);
  995. kfree(vp->header_rxbuffer);
  996. kfree(vp->header_txbuffer);
  997. if (vp->rx_queue != NULL)
  998. destroy_queue(vp->rx_queue);
  999. if (vp->tx_queue != NULL)
  1000. destroy_queue(vp->tx_queue);
  1001. kfree(vp->fds);
  1002. vp->fds = NULL;
  1003. vp->in_error = false;
  1004. return 0;
  1005. }
  1006. static int vector_poll(struct napi_struct *napi, int budget)
  1007. {
  1008. struct vector_private *vp = container_of(napi, struct vector_private, napi);
  1009. int work_done = 0;
  1010. int err;
  1011. bool tx_enqueued = false;
  1012. if ((vp->options & VECTOR_TX) != 0)
  1013. tx_enqueued = (vector_send(vp->tx_queue) > 0);
  1014. spin_lock(&vp->rx_queue->head_lock);
  1015. if ((vp->options & VECTOR_RX) > 0)
  1016. err = vector_mmsg_rx(vp, budget);
  1017. else {
  1018. err = vector_legacy_rx(vp);
  1019. if (err > 0)
  1020. err = 1;
  1021. }
  1022. spin_unlock(&vp->rx_queue->head_lock);
  1023. if (err > 0)
  1024. work_done += err;
  1025. if (tx_enqueued || err > 0)
  1026. napi_schedule(napi);
  1027. if (work_done <= budget)
  1028. napi_complete_done(napi, work_done);
  1029. return work_done;
  1030. }
  1031. static void vector_reset_tx(struct work_struct *work)
  1032. {
  1033. struct vector_private *vp =
  1034. container_of(work, struct vector_private, reset_tx);
  1035. netdev_reset_queue(vp->dev);
  1036. netif_start_queue(vp->dev);
  1037. netif_wake_queue(vp->dev);
  1038. }
  1039. static int vector_net_open(struct net_device *dev)
  1040. {
  1041. struct vector_private *vp = netdev_priv(dev);
  1042. int err = -EINVAL;
  1043. struct vector_device *vdevice;
  1044. if (vp->opened)
  1045. return -ENXIO;
  1046. vp->opened = true;
  1047. vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed));
  1048. vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
  1049. if (vp->fds == NULL)
  1050. goto out_close;
  1051. if (build_transport_data(vp) < 0)
  1052. goto out_close;
  1053. if ((vp->options & VECTOR_RX) > 0) {
  1054. vp->rx_queue = create_queue(
  1055. vp,
  1056. get_depth(vp->parsed),
  1057. vp->rx_header_size,
  1058. MAX_IOV_SIZE
  1059. );
  1060. atomic_set(&vp->rx_queue->queue_depth, get_depth(vp->parsed));
  1061. } else {
  1062. vp->header_rxbuffer = kmalloc(
  1063. vp->rx_header_size,
  1064. GFP_KERNEL
  1065. );
  1066. if (vp->header_rxbuffer == NULL)
  1067. goto out_close;
  1068. }
  1069. if ((vp->options & VECTOR_TX) > 0) {
  1070. vp->tx_queue = create_queue(
  1071. vp,
  1072. get_depth(vp->parsed),
  1073. vp->header_size,
  1074. MAX_IOV_SIZE
  1075. );
  1076. } else {
  1077. vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL);
  1078. if (vp->header_txbuffer == NULL)
  1079. goto out_close;
  1080. }
  1081. netif_napi_add_weight(vp->dev, &vp->napi, vector_poll,
  1082. get_depth(vp->parsed));
  1083. napi_enable(&vp->napi);
  1084. /* READ IRQ */
  1085. err = um_request_irq(
  1086. irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd,
  1087. IRQ_READ, vector_rx_interrupt,
  1088. IRQF_SHARED, dev->name, dev);
  1089. if (err < 0) {
  1090. netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err);
  1091. err = -ENETUNREACH;
  1092. goto out_close;
  1093. }
  1094. vp->rx_irq = irq_rr + VECTOR_BASE_IRQ;
  1095. dev->irq = irq_rr + VECTOR_BASE_IRQ;
  1096. irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
  1097. /* WRITE IRQ - we need it only if we have vector TX */
  1098. if ((vp->options & VECTOR_TX) > 0) {
  1099. err = um_request_irq(
  1100. irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd,
  1101. IRQ_WRITE, vector_tx_interrupt,
  1102. IRQF_SHARED, dev->name, dev);
  1103. if (err < 0) {
  1104. netdev_err(dev,
  1105. "vector_open: failed to get tx irq(%d)\n", err);
  1106. err = -ENETUNREACH;
  1107. goto out_close;
  1108. }
  1109. vp->tx_irq = irq_rr + VECTOR_BASE_IRQ;
  1110. irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
  1111. }
  1112. if ((vp->options & VECTOR_QDISC_BYPASS) != 0) {
  1113. if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
  1114. vp->options |= VECTOR_BPF;
  1115. }
  1116. if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL))
  1117. vp->bpf = uml_vector_default_bpf(dev->dev_addr);
  1118. if (vp->bpf != NULL)
  1119. uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
  1120. netif_start_queue(dev);
  1121. vector_reset_stats(vp);
  1122. /* clear buffer - it can happen that the host side of the interface
  1123. * is full when we get here. In this case, new data is never queued,
  1124. * SIGIOs never arrive, and the net never works.
  1125. */
  1126. napi_schedule(&vp->napi);
  1127. vdevice = find_device(vp->unit);
  1128. vdevice->opened = 1;
  1129. if ((vp->options & VECTOR_TX) != 0)
  1130. add_timer(&vp->tl);
  1131. return 0;
  1132. out_close:
  1133. vector_net_close(dev);
  1134. return err;
  1135. }
  1136. static void vector_net_set_multicast_list(struct net_device *dev)
  1137. {
  1138. /* TODO: - we can do some BPF games here */
  1139. return;
  1140. }
  1141. static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
  1142. {
  1143. struct vector_private *vp = netdev_priv(dev);
  1144. vp->estats.tx_timeout_count++;
  1145. netif_trans_update(dev);
  1146. schedule_work(&vp->reset_tx);
  1147. }
  1148. static netdev_features_t vector_fix_features(struct net_device *dev,
  1149. netdev_features_t features)
  1150. {
  1151. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  1152. return features;
  1153. }
  1154. static int vector_set_features(struct net_device *dev,
  1155. netdev_features_t features)
  1156. {
  1157. struct vector_private *vp = netdev_priv(dev);
  1158. /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is
  1159. * no way to negotiate it on raw sockets, so we can change
  1160. * only our side.
  1161. */
  1162. if (features & NETIF_F_GRO)
  1163. /* All new frame buffers will be GRO-sized */
  1164. vp->req_size = 65536;
  1165. else
  1166. /* All new frame buffers will be normal sized */
  1167. vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN;
  1168. return 0;
  1169. }
  1170. #ifdef CONFIG_NET_POLL_CONTROLLER
  1171. static void vector_net_poll_controller(struct net_device *dev)
  1172. {
  1173. disable_irq(dev->irq);
  1174. vector_rx_interrupt(dev->irq, dev);
  1175. enable_irq(dev->irq);
  1176. }
  1177. #endif
  1178. static void vector_net_get_drvinfo(struct net_device *dev,
  1179. struct ethtool_drvinfo *info)
  1180. {
  1181. strscpy(info->driver, DRIVER_NAME);
  1182. }
  1183. static int vector_net_load_bpf_flash(struct net_device *dev,
  1184. struct ethtool_flash *efl)
  1185. {
  1186. struct vector_private *vp = netdev_priv(dev);
  1187. struct vector_device *vdevice;
  1188. const struct firmware *fw;
  1189. int result = 0;
  1190. if (!(vp->options & VECTOR_BPF_FLASH)) {
  1191. netdev_err(dev, "loading firmware not permitted: %s\n", efl->data);
  1192. return -1;
  1193. }
  1194. if (vp->bpf != NULL) {
  1195. if (vp->opened)
  1196. uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
  1197. kfree(vp->bpf->filter);
  1198. vp->bpf->filter = NULL;
  1199. } else {
  1200. vp->bpf = kmalloc(sizeof(struct sock_fprog), GFP_ATOMIC);
  1201. if (vp->bpf == NULL) {
  1202. netdev_err(dev, "failed to allocate memory for firmware\n");
  1203. goto flash_fail;
  1204. }
  1205. }
  1206. vdevice = find_device(vp->unit);
  1207. if (request_firmware(&fw, efl->data, &vdevice->pdev.dev))
  1208. goto flash_fail;
  1209. vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC);
  1210. if (!vp->bpf->filter)
  1211. goto free_buffer;
  1212. vp->bpf->len = fw->size / sizeof(struct sock_filter);
  1213. release_firmware(fw);
  1214. if (vp->opened)
  1215. result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
  1216. return result;
  1217. free_buffer:
  1218. release_firmware(fw);
  1219. flash_fail:
  1220. if (vp->bpf != NULL)
  1221. kfree(vp->bpf->filter);
  1222. kfree(vp->bpf);
  1223. vp->bpf = NULL;
  1224. return -1;
  1225. }
  1226. static void vector_get_ringparam(struct net_device *netdev,
  1227. struct ethtool_ringparam *ring,
  1228. struct kernel_ethtool_ringparam *kernel_ring,
  1229. struct netlink_ext_ack *extack)
  1230. {
  1231. struct vector_private *vp = netdev_priv(netdev);
  1232. ring->rx_max_pending = vp->rx_queue->max_depth;
  1233. ring->tx_max_pending = vp->tx_queue->max_depth;
  1234. ring->rx_pending = vp->rx_queue->max_depth;
  1235. ring->tx_pending = vp->tx_queue->max_depth;
  1236. }
  1237. static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  1238. {
  1239. switch (stringset) {
  1240. case ETH_SS_TEST:
  1241. *buf = '\0';
  1242. break;
  1243. case ETH_SS_STATS:
  1244. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  1245. break;
  1246. default:
  1247. WARN_ON(1);
  1248. break;
  1249. }
  1250. }
  1251. static int vector_get_sset_count(struct net_device *dev, int sset)
  1252. {
  1253. switch (sset) {
  1254. case ETH_SS_TEST:
  1255. return 0;
  1256. case ETH_SS_STATS:
  1257. return VECTOR_NUM_STATS;
  1258. default:
  1259. return -EOPNOTSUPP;
  1260. }
  1261. }
  1262. static void vector_get_ethtool_stats(struct net_device *dev,
  1263. struct ethtool_stats *estats,
  1264. u64 *tmp_stats)
  1265. {
  1266. struct vector_private *vp = netdev_priv(dev);
  1267. /* Stats are modified in the dequeue portions of
  1268. * rx/tx which are protected by the head locks
  1269. * grabbing these locks here ensures they are up
  1270. * to date.
  1271. */
  1272. spin_lock(&vp->tx_queue->head_lock);
  1273. spin_lock(&vp->rx_queue->head_lock);
  1274. memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats));
  1275. spin_unlock(&vp->rx_queue->head_lock);
  1276. spin_unlock(&vp->tx_queue->head_lock);
  1277. }
  1278. static int vector_get_coalesce(struct net_device *netdev,
  1279. struct ethtool_coalesce *ec,
  1280. struct kernel_ethtool_coalesce *kernel_coal,
  1281. struct netlink_ext_ack *extack)
  1282. {
  1283. struct vector_private *vp = netdev_priv(netdev);
  1284. ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ;
  1285. return 0;
  1286. }
  1287. static int vector_set_coalesce(struct net_device *netdev,
  1288. struct ethtool_coalesce *ec,
  1289. struct kernel_ethtool_coalesce *kernel_coal,
  1290. struct netlink_ext_ack *extack)
  1291. {
  1292. struct vector_private *vp = netdev_priv(netdev);
  1293. vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000;
  1294. if (vp->coalesce == 0)
  1295. vp->coalesce = 1;
  1296. return 0;
  1297. }
  1298. static const struct ethtool_ops vector_net_ethtool_ops = {
  1299. .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS,
  1300. .get_drvinfo = vector_net_get_drvinfo,
  1301. .get_link = ethtool_op_get_link,
  1302. .get_ts_info = ethtool_op_get_ts_info,
  1303. .get_ringparam = vector_get_ringparam,
  1304. .get_strings = vector_get_strings,
  1305. .get_sset_count = vector_get_sset_count,
  1306. .get_ethtool_stats = vector_get_ethtool_stats,
  1307. .get_coalesce = vector_get_coalesce,
  1308. .set_coalesce = vector_set_coalesce,
  1309. .flash_device = vector_net_load_bpf_flash,
  1310. };
  1311. static const struct net_device_ops vector_netdev_ops = {
  1312. .ndo_open = vector_net_open,
  1313. .ndo_stop = vector_net_close,
  1314. .ndo_start_xmit = vector_net_start_xmit,
  1315. .ndo_set_rx_mode = vector_net_set_multicast_list,
  1316. .ndo_tx_timeout = vector_net_tx_timeout,
  1317. .ndo_set_mac_address = eth_mac_addr,
  1318. .ndo_validate_addr = eth_validate_addr,
  1319. .ndo_fix_features = vector_fix_features,
  1320. .ndo_set_features = vector_set_features,
  1321. #ifdef CONFIG_NET_POLL_CONTROLLER
  1322. .ndo_poll_controller = vector_net_poll_controller,
  1323. #endif
  1324. };
  1325. static void vector_timer_expire(struct timer_list *t)
  1326. {
  1327. struct vector_private *vp = from_timer(vp, t, tl);
  1328. vp->estats.tx_kicks++;
  1329. napi_schedule(&vp->napi);
  1330. }
  1331. static void vector_eth_configure(
  1332. int n,
  1333. struct arglist *def
  1334. )
  1335. {
  1336. struct vector_device *device;
  1337. struct net_device *dev;
  1338. struct vector_private *vp;
  1339. int err;
  1340. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1341. if (device == NULL) {
  1342. printk(KERN_ERR "eth_configure failed to allocate struct "
  1343. "vector_device\n");
  1344. return;
  1345. }
  1346. dev = alloc_etherdev(sizeof(struct vector_private));
  1347. if (dev == NULL) {
  1348. printk(KERN_ERR "eth_configure: failed to allocate struct "
  1349. "net_device for vec%d\n", n);
  1350. goto out_free_device;
  1351. }
  1352. dev->mtu = get_mtu(def);
  1353. INIT_LIST_HEAD(&device->list);
  1354. device->unit = n;
  1355. /* If this name ends up conflicting with an existing registered
  1356. * netdevice, that is OK, register_netdev{,ice}() will notice this
  1357. * and fail.
  1358. */
  1359. snprintf(dev->name, sizeof(dev->name), "vec%d", n);
  1360. uml_net_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac"));
  1361. vp = netdev_priv(dev);
  1362. /* sysfs register */
  1363. if (!driver_registered) {
  1364. platform_driver_register(&uml_net_driver);
  1365. driver_registered = 1;
  1366. }
  1367. device->pdev.id = n;
  1368. device->pdev.name = DRIVER_NAME;
  1369. device->pdev.dev.release = vector_device_release;
  1370. dev_set_drvdata(&device->pdev.dev, device);
  1371. if (platform_device_register(&device->pdev))
  1372. goto out_free_netdev;
  1373. SET_NETDEV_DEV(dev, &device->pdev.dev);
  1374. device->dev = dev;
  1375. *vp = ((struct vector_private)
  1376. {
  1377. .list = LIST_HEAD_INIT(vp->list),
  1378. .dev = dev,
  1379. .unit = n,
  1380. .options = get_transport_options(def),
  1381. .rx_irq = 0,
  1382. .tx_irq = 0,
  1383. .parsed = def,
  1384. .max_packet = get_mtu(def) + ETH_HEADER_OTHER,
  1385. /* TODO - we need to calculate headroom so that ip header
  1386. * is 16 byte aligned all the time
  1387. */
  1388. .headroom = get_headroom(def),
  1389. .form_header = NULL,
  1390. .verify_header = NULL,
  1391. .header_rxbuffer = NULL,
  1392. .header_txbuffer = NULL,
  1393. .header_size = 0,
  1394. .rx_header_size = 0,
  1395. .rexmit_scheduled = false,
  1396. .opened = false,
  1397. .transport_data = NULL,
  1398. .in_write_poll = false,
  1399. .coalesce = 2,
  1400. .req_size = get_req_size(def),
  1401. .in_error = false,
  1402. .bpf = NULL
  1403. });
  1404. dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
  1405. INIT_WORK(&vp->reset_tx, vector_reset_tx);
  1406. timer_setup(&vp->tl, vector_timer_expire, 0);
  1407. /* FIXME */
  1408. dev->netdev_ops = &vector_netdev_ops;
  1409. dev->ethtool_ops = &vector_net_ethtool_ops;
  1410. dev->watchdog_timeo = (HZ >> 1);
  1411. /* primary IRQ - fixme */
  1412. dev->irq = 0; /* we will adjust this once opened */
  1413. rtnl_lock();
  1414. err = register_netdevice(dev);
  1415. rtnl_unlock();
  1416. if (err)
  1417. goto out_undo_user_init;
  1418. spin_lock(&vector_devices_lock);
  1419. list_add(&device->list, &vector_devices);
  1420. spin_unlock(&vector_devices_lock);
  1421. return;
  1422. out_undo_user_init:
  1423. return;
  1424. out_free_netdev:
  1425. free_netdev(dev);
  1426. out_free_device:
  1427. kfree(device);
  1428. }
  1429. /*
  1430. * Invoked late in the init
  1431. */
  1432. static int __init vector_init(void)
  1433. {
  1434. struct list_head *ele;
  1435. struct vector_cmd_line_arg *def;
  1436. struct arglist *parsed;
  1437. list_for_each(ele, &vec_cmd_line) {
  1438. def = list_entry(ele, struct vector_cmd_line_arg, list);
  1439. parsed = uml_parse_vector_ifspec(def->arguments);
  1440. if (parsed != NULL)
  1441. vector_eth_configure(def->unit, parsed);
  1442. }
  1443. return 0;
  1444. }
  1445. /* Invoked at initial argument parsing, only stores
  1446. * arguments until a proper vector_init is called
  1447. * later
  1448. */
  1449. static int __init vector_setup(char *str)
  1450. {
  1451. char *error;
  1452. int n, err;
  1453. struct vector_cmd_line_arg *new;
  1454. err = vector_parse(str, &n, &str, &error);
  1455. if (err) {
  1456. printk(KERN_ERR "vector_setup - Couldn't parse '%s' : %s\n",
  1457. str, error);
  1458. return 1;
  1459. }
  1460. new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
  1461. if (!new)
  1462. panic("%s: Failed to allocate %zu bytes\n", __func__,
  1463. sizeof(*new));
  1464. INIT_LIST_HEAD(&new->list);
  1465. new->unit = n;
  1466. new->arguments = str;
  1467. list_add_tail(&new->list, &vec_cmd_line);
  1468. return 1;
  1469. }
  1470. __setup("vec", vector_setup);
  1471. __uml_help(vector_setup,
  1472. "vec[0-9]+:<option>=<value>,<option>=<value>\n"
  1473. " Configure a vector io network device.\n\n"
  1474. );
  1475. late_initcall(vector_init);
  1476. static struct mc_device vector_mc = {
  1477. .list = LIST_HEAD_INIT(vector_mc.list),
  1478. .name = "vec",
  1479. .config = vector_config,
  1480. .get_config = NULL,
  1481. .id = vector_id,
  1482. .remove = vector_remove,
  1483. };
  1484. #ifdef CONFIG_INET
  1485. static int vector_inetaddr_event(
  1486. struct notifier_block *this,
  1487. unsigned long event,
  1488. void *ptr)
  1489. {
  1490. return NOTIFY_DONE;
  1491. }
  1492. static struct notifier_block vector_inetaddr_notifier = {
  1493. .notifier_call = vector_inetaddr_event,
  1494. };
  1495. static void inet_register(void)
  1496. {
  1497. register_inetaddr_notifier(&vector_inetaddr_notifier);
  1498. }
  1499. #else
  1500. static inline void inet_register(void)
  1501. {
  1502. }
  1503. #endif
  1504. static int vector_net_init(void)
  1505. {
  1506. mconsole_register_dev(&vector_mc);
  1507. inet_register();
  1508. return 0;
  1509. }
  1510. __initcall(vector_net_init);