vector_kern.c 38 KB

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