vector_kern.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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/bootmem.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);
  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. goto out_fail;
  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. result->skbuff_vector = kmalloc(
  451. (sizeof(void *) * max_size), GFP_KERNEL);
  452. if (result->mmsg_vector == NULL || result->skbuff_vector == NULL)
  453. goto out_fail;
  454. mmsg_vector = result->mmsg_vector;
  455. for (i = 0; i < max_size; i++) {
  456. /* Clear all pointers - we use non-NULL as marking on
  457. * what to free on destruction
  458. */
  459. *(result->skbuff_vector + i) = NULL;
  460. mmsg_vector->msg_hdr.msg_iov = NULL;
  461. mmsg_vector++;
  462. }
  463. mmsg_vector = result->mmsg_vector;
  464. result->max_iov_frags = num_extra_frags;
  465. for (i = 0; i < max_size; i++) {
  466. if (vp->header_size > 0)
  467. iov = kmalloc(
  468. sizeof(struct iovec) * (3 + num_extra_frags),
  469. GFP_KERNEL
  470. );
  471. else
  472. iov = kmalloc(
  473. sizeof(struct iovec) * (2 + num_extra_frags),
  474. GFP_KERNEL
  475. );
  476. if (iov == NULL)
  477. goto out_fail;
  478. mmsg_vector->msg_hdr.msg_iov = iov;
  479. mmsg_vector->msg_hdr.msg_iovlen = 1;
  480. mmsg_vector->msg_hdr.msg_control = NULL;
  481. mmsg_vector->msg_hdr.msg_controllen = 0;
  482. mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT;
  483. mmsg_vector->msg_hdr.msg_name = NULL;
  484. mmsg_vector->msg_hdr.msg_namelen = 0;
  485. if (vp->header_size > 0) {
  486. iov->iov_base = kmalloc(header_size, GFP_KERNEL);
  487. if (iov->iov_base == NULL)
  488. goto out_fail;
  489. iov->iov_len = header_size;
  490. mmsg_vector->msg_hdr.msg_iovlen = 2;
  491. iov++;
  492. }
  493. iov->iov_base = NULL;
  494. iov->iov_len = 0;
  495. mmsg_vector++;
  496. }
  497. spin_lock_init(&result->head_lock);
  498. spin_lock_init(&result->tail_lock);
  499. result->queue_depth = 0;
  500. result->head = 0;
  501. result->tail = 0;
  502. return result;
  503. out_fail:
  504. destroy_queue(result);
  505. return NULL;
  506. }
  507. /*
  508. * We do not use the RX queue as a proper wraparound queue for now
  509. * This is not necessary because the consumption via netif_rx()
  510. * happens in-line. While we can try using the return code of
  511. * netif_rx() for flow control there are no drivers doing this today.
  512. * For this RX specific use we ignore the tail/head locks and
  513. * just read into a prepared queue filled with skbuffs.
  514. */
  515. static struct sk_buff *prep_skb(
  516. struct vector_private *vp,
  517. struct user_msghdr *msg)
  518. {
  519. int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN;
  520. struct sk_buff *result;
  521. int iov_index = 0, len;
  522. struct iovec *iov = msg->msg_iov;
  523. int err, nr_frags, frag;
  524. skb_frag_t *skb_frag;
  525. if (vp->req_size <= linear)
  526. len = linear;
  527. else
  528. len = vp->req_size;
  529. result = alloc_skb_with_frags(
  530. linear,
  531. len - vp->max_packet,
  532. 3,
  533. &err,
  534. GFP_ATOMIC
  535. );
  536. if (vp->header_size > 0)
  537. iov_index++;
  538. if (result == NULL) {
  539. iov[iov_index].iov_base = NULL;
  540. iov[iov_index].iov_len = 0;
  541. goto done;
  542. }
  543. skb_reserve(result, vp->headroom);
  544. result->dev = vp->dev;
  545. skb_put(result, vp->max_packet);
  546. result->data_len = len - vp->max_packet;
  547. result->len += len - vp->max_packet;
  548. skb_reset_mac_header(result);
  549. result->ip_summed = CHECKSUM_NONE;
  550. iov[iov_index].iov_base = result->data;
  551. iov[iov_index].iov_len = vp->max_packet;
  552. iov_index++;
  553. nr_frags = skb_shinfo(result)->nr_frags;
  554. for (frag = 0; frag < nr_frags; frag++) {
  555. skb_frag = &skb_shinfo(result)->frags[frag];
  556. iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
  557. if (iov[iov_index].iov_base != NULL)
  558. iov[iov_index].iov_len = skb_frag_size(skb_frag);
  559. else
  560. iov[iov_index].iov_len = 0;
  561. iov_index++;
  562. }
  563. done:
  564. msg->msg_iovlen = iov_index;
  565. return result;
  566. }
  567. /* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs*/
  568. static void prep_queue_for_rx(struct vector_queue *qi)
  569. {
  570. struct vector_private *vp = netdev_priv(qi->dev);
  571. struct mmsghdr *mmsg_vector = qi->mmsg_vector;
  572. void **skbuff_vector = qi->skbuff_vector;
  573. int i;
  574. if (qi->queue_depth == 0)
  575. return;
  576. for (i = 0; i < qi->queue_depth; i++) {
  577. /* it is OK if allocation fails - recvmmsg with NULL data in
  578. * iov argument still performs an RX, just drops the packet
  579. * This allows us stop faffing around with a "drop buffer"
  580. */
  581. *skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr);
  582. skbuff_vector++;
  583. mmsg_vector++;
  584. }
  585. qi->queue_depth = 0;
  586. }
  587. static struct vector_device *find_device(int n)
  588. {
  589. struct vector_device *device;
  590. struct list_head *ele;
  591. spin_lock(&vector_devices_lock);
  592. list_for_each(ele, &vector_devices) {
  593. device = list_entry(ele, struct vector_device, list);
  594. if (device->unit == n)
  595. goto out;
  596. }
  597. device = NULL;
  598. out:
  599. spin_unlock(&vector_devices_lock);
  600. return device;
  601. }
  602. static int vector_parse(char *str, int *index_out, char **str_out,
  603. char **error_out)
  604. {
  605. int n, len, err;
  606. char *start = str;
  607. len = strlen(str);
  608. while ((*str != ':') && (strlen(str) > 1))
  609. str++;
  610. if (*str != ':') {
  611. *error_out = "Expected ':' after device number";
  612. return -EINVAL;
  613. }
  614. *str = '\0';
  615. err = kstrtouint(start, 0, &n);
  616. if (err < 0) {
  617. *error_out = "Bad device number";
  618. return err;
  619. }
  620. str++;
  621. if (find_device(n)) {
  622. *error_out = "Device already configured";
  623. return -EINVAL;
  624. }
  625. *index_out = n;
  626. *str_out = str;
  627. return 0;
  628. }
  629. static int vector_config(char *str, char **error_out)
  630. {
  631. int err, n;
  632. char *params;
  633. struct arglist *parsed;
  634. err = vector_parse(str, &n, &params, error_out);
  635. if (err != 0)
  636. return err;
  637. /* This string is broken up and the pieces used by the underlying
  638. * driver. We should copy it to make sure things do not go wrong
  639. * later.
  640. */
  641. params = kstrdup(params, GFP_KERNEL);
  642. if (params == NULL) {
  643. *error_out = "vector_config failed to strdup string";
  644. return -ENOMEM;
  645. }
  646. parsed = uml_parse_vector_ifspec(params);
  647. if (parsed == NULL) {
  648. *error_out = "vector_config failed to parse parameters";
  649. return -EINVAL;
  650. }
  651. vector_eth_configure(n, parsed);
  652. return 0;
  653. }
  654. static int vector_id(char **str, int *start_out, int *end_out)
  655. {
  656. char *end;
  657. int n;
  658. n = simple_strtoul(*str, &end, 0);
  659. if ((*end != '\0') || (end == *str))
  660. return -1;
  661. *start_out = n;
  662. *end_out = n;
  663. *str = end;
  664. return n;
  665. }
  666. static int vector_remove(int n, char **error_out)
  667. {
  668. struct vector_device *vec_d;
  669. struct net_device *dev;
  670. struct vector_private *vp;
  671. vec_d = find_device(n);
  672. if (vec_d == NULL)
  673. return -ENODEV;
  674. dev = vec_d->dev;
  675. vp = netdev_priv(dev);
  676. if (vp->fds != NULL)
  677. return -EBUSY;
  678. unregister_netdev(dev);
  679. platform_device_unregister(&vec_d->pdev);
  680. return 0;
  681. }
  682. /*
  683. * There is no shared per-transport initialization code, so
  684. * we will just initialize each interface one by one and
  685. * add them to a list
  686. */
  687. static struct platform_driver uml_net_driver = {
  688. .driver = {
  689. .name = DRIVER_NAME,
  690. },
  691. };
  692. static void vector_device_release(struct device *dev)
  693. {
  694. struct vector_device *device = dev_get_drvdata(dev);
  695. struct net_device *netdev = device->dev;
  696. list_del(&device->list);
  697. kfree(device);
  698. free_netdev(netdev);
  699. }
  700. /* Bog standard recv using recvmsg - not used normally unless the user
  701. * explicitly specifies not to use recvmmsg vector RX.
  702. */
  703. static int vector_legacy_rx(struct vector_private *vp)
  704. {
  705. int pkt_len;
  706. struct user_msghdr hdr;
  707. struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */
  708. int iovpos = 0;
  709. struct sk_buff *skb;
  710. int header_check;
  711. hdr.msg_name = NULL;
  712. hdr.msg_namelen = 0;
  713. hdr.msg_iov = (struct iovec *) &iov;
  714. hdr.msg_control = NULL;
  715. hdr.msg_controllen = 0;
  716. hdr.msg_flags = 0;
  717. if (vp->header_size > 0) {
  718. iov[0].iov_base = vp->header_rxbuffer;
  719. iov[0].iov_len = vp->header_size;
  720. }
  721. skb = prep_skb(vp, &hdr);
  722. if (skb == NULL) {
  723. /* Read a packet into drop_buffer and don't do
  724. * anything with it.
  725. */
  726. iov[iovpos].iov_base = drop_buffer;
  727. iov[iovpos].iov_len = DROP_BUFFER_SIZE;
  728. hdr.msg_iovlen = 1;
  729. vp->dev->stats.rx_dropped++;
  730. }
  731. pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0);
  732. if (skb != NULL) {
  733. if (pkt_len > vp->header_size) {
  734. if (vp->header_size > 0) {
  735. header_check = vp->verify_header(
  736. vp->header_rxbuffer, skb, vp);
  737. if (header_check < 0) {
  738. dev_kfree_skb_irq(skb);
  739. vp->dev->stats.rx_dropped++;
  740. vp->estats.rx_encaps_errors++;
  741. return 0;
  742. }
  743. if (header_check > 0) {
  744. vp->estats.rx_csum_offload_good++;
  745. skb->ip_summed = CHECKSUM_UNNECESSARY;
  746. }
  747. }
  748. pskb_trim(skb, pkt_len - vp->rx_header_size);
  749. skb->protocol = eth_type_trans(skb, skb->dev);
  750. vp->dev->stats.rx_bytes += skb->len;
  751. vp->dev->stats.rx_packets++;
  752. netif_rx(skb);
  753. } else {
  754. dev_kfree_skb_irq(skb);
  755. }
  756. }
  757. return pkt_len;
  758. }
  759. /*
  760. * Packet at a time TX which falls back to vector TX if the
  761. * underlying transport is busy.
  762. */
  763. static int writev_tx(struct vector_private *vp, struct sk_buff *skb)
  764. {
  765. struct iovec iov[3 + MAX_IOV_SIZE];
  766. int iov_count, pkt_len = 0;
  767. iov[0].iov_base = vp->header_txbuffer;
  768. iov_count = prep_msg(vp, skb, (struct iovec *) &iov);
  769. if (iov_count < 1)
  770. goto drop;
  771. pkt_len = uml_vector_writev(
  772. vp->fds->tx_fd,
  773. (struct iovec *) &iov,
  774. iov_count
  775. );
  776. netif_trans_update(vp->dev);
  777. netif_wake_queue(vp->dev);
  778. if (pkt_len > 0) {
  779. vp->dev->stats.tx_bytes += skb->len;
  780. vp->dev->stats.tx_packets++;
  781. } else {
  782. vp->dev->stats.tx_dropped++;
  783. }
  784. consume_skb(skb);
  785. return pkt_len;
  786. drop:
  787. vp->dev->stats.tx_dropped++;
  788. consume_skb(skb);
  789. return pkt_len;
  790. }
  791. /*
  792. * Receive as many messages as we can in one call using the special
  793. * mmsg vector matched to an skb vector which we prepared earlier.
  794. */
  795. static int vector_mmsg_rx(struct vector_private *vp)
  796. {
  797. int packet_count, i;
  798. struct vector_queue *qi = vp->rx_queue;
  799. struct sk_buff *skb;
  800. struct mmsghdr *mmsg_vector = qi->mmsg_vector;
  801. void **skbuff_vector = qi->skbuff_vector;
  802. int header_check;
  803. /* Refresh the vector and make sure it is with new skbs and the
  804. * iovs are updated to point to them.
  805. */
  806. prep_queue_for_rx(qi);
  807. /* Fire the Lazy Gun - get as many packets as we can in one go. */
  808. packet_count = uml_vector_recvmmsg(
  809. vp->fds->rx_fd, qi->mmsg_vector, qi->max_depth, 0);
  810. if (packet_count <= 0)
  811. return packet_count;
  812. /* We treat packet processing as enqueue, buffer refresh as dequeue
  813. * The queue_depth tells us how many buffers have been used and how
  814. * many do we need to prep the next time prep_queue_for_rx() is called.
  815. */
  816. qi->queue_depth = packet_count;
  817. for (i = 0; i < packet_count; i++) {
  818. skb = (*skbuff_vector);
  819. if (mmsg_vector->msg_len > vp->header_size) {
  820. if (vp->header_size > 0) {
  821. header_check = vp->verify_header(
  822. mmsg_vector->msg_hdr.msg_iov->iov_base,
  823. skb,
  824. vp
  825. );
  826. if (header_check < 0) {
  827. /* Overlay header failed to verify - discard.
  828. * We can actually keep this skb and reuse it,
  829. * but that will make the prep logic too
  830. * complex.
  831. */
  832. dev_kfree_skb_irq(skb);
  833. vp->estats.rx_encaps_errors++;
  834. continue;
  835. }
  836. if (header_check > 0) {
  837. vp->estats.rx_csum_offload_good++;
  838. skb->ip_summed = CHECKSUM_UNNECESSARY;
  839. }
  840. }
  841. pskb_trim(skb,
  842. mmsg_vector->msg_len - vp->rx_header_size);
  843. skb->protocol = eth_type_trans(skb, skb->dev);
  844. /*
  845. * We do not need to lock on updating stats here
  846. * The interrupt loop is non-reentrant.
  847. */
  848. vp->dev->stats.rx_bytes += skb->len;
  849. vp->dev->stats.rx_packets++;
  850. netif_rx(skb);
  851. } else {
  852. /* Overlay header too short to do anything - discard.
  853. * We can actually keep this skb and reuse it,
  854. * but that will make the prep logic too complex.
  855. */
  856. if (skb != NULL)
  857. dev_kfree_skb_irq(skb);
  858. }
  859. (*skbuff_vector) = NULL;
  860. /* Move to the next buffer element */
  861. mmsg_vector++;
  862. skbuff_vector++;
  863. }
  864. if (packet_count > 0) {
  865. if (vp->estats.rx_queue_max < packet_count)
  866. vp->estats.rx_queue_max = packet_count;
  867. vp->estats.rx_queue_running_average =
  868. (vp->estats.rx_queue_running_average + packet_count) >> 1;
  869. }
  870. return packet_count;
  871. }
  872. static void vector_rx(struct vector_private *vp)
  873. {
  874. int err;
  875. if ((vp->options & VECTOR_RX) > 0)
  876. while ((err = vector_mmsg_rx(vp)) > 0)
  877. ;
  878. else
  879. while ((err = vector_legacy_rx(vp)) > 0)
  880. ;
  881. if ((err != 0) && net_ratelimit())
  882. netdev_err(vp->dev, "vector_rx: error(%d)\n", err);
  883. }
  884. static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  885. {
  886. struct vector_private *vp = netdev_priv(dev);
  887. int queue_depth = 0;
  888. if ((vp->options & VECTOR_TX) == 0) {
  889. writev_tx(vp, skb);
  890. return NETDEV_TX_OK;
  891. }
  892. /* We do BQL only in the vector path, no point doing it in
  893. * packet at a time mode as there is no device queue
  894. */
  895. netdev_sent_queue(vp->dev, skb->len);
  896. queue_depth = vector_enqueue(vp->tx_queue, skb);
  897. /* if the device queue is full, stop the upper layers and
  898. * flush it.
  899. */
  900. if (queue_depth >= vp->tx_queue->max_depth - 1) {
  901. vp->estats.tx_kicks++;
  902. netif_stop_queue(dev);
  903. vector_send(vp->tx_queue);
  904. return NETDEV_TX_OK;
  905. }
  906. if (skb->xmit_more) {
  907. mod_timer(&vp->tl, vp->coalesce);
  908. return NETDEV_TX_OK;
  909. }
  910. if (skb->len < TX_SMALL_PACKET) {
  911. vp->estats.tx_kicks++;
  912. vector_send(vp->tx_queue);
  913. } else
  914. tasklet_schedule(&vp->tx_poll);
  915. return NETDEV_TX_OK;
  916. }
  917. static irqreturn_t vector_rx_interrupt(int irq, void *dev_id)
  918. {
  919. struct net_device *dev = dev_id;
  920. struct vector_private *vp = netdev_priv(dev);
  921. if (!netif_running(dev))
  922. return IRQ_NONE;
  923. vector_rx(vp);
  924. return IRQ_HANDLED;
  925. }
  926. static irqreturn_t vector_tx_interrupt(int irq, void *dev_id)
  927. {
  928. struct net_device *dev = dev_id;
  929. struct vector_private *vp = netdev_priv(dev);
  930. if (!netif_running(dev))
  931. return IRQ_NONE;
  932. /* We need to pay attention to it only if we got
  933. * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise
  934. * we ignore it. In the future, it may be worth
  935. * it to improve the IRQ controller a bit to make
  936. * tweaking the IRQ mask less costly
  937. */
  938. if (vp->in_write_poll)
  939. tasklet_schedule(&vp->tx_poll);
  940. return IRQ_HANDLED;
  941. }
  942. static int irq_rr;
  943. static int vector_net_close(struct net_device *dev)
  944. {
  945. struct vector_private *vp = netdev_priv(dev);
  946. unsigned long flags;
  947. netif_stop_queue(dev);
  948. del_timer(&vp->tl);
  949. if (vp->fds == NULL)
  950. return 0;
  951. /* Disable and free all IRQS */
  952. if (vp->rx_irq > 0) {
  953. um_free_irq(vp->rx_irq, dev);
  954. vp->rx_irq = 0;
  955. }
  956. if (vp->tx_irq > 0) {
  957. um_free_irq(vp->tx_irq, dev);
  958. vp->tx_irq = 0;
  959. }
  960. tasklet_kill(&vp->tx_poll);
  961. if (vp->fds->rx_fd > 0) {
  962. os_close_file(vp->fds->rx_fd);
  963. vp->fds->rx_fd = -1;
  964. }
  965. if (vp->fds->tx_fd > 0) {
  966. os_close_file(vp->fds->tx_fd);
  967. vp->fds->tx_fd = -1;
  968. }
  969. if (vp->bpf != NULL)
  970. kfree(vp->bpf);
  971. if (vp->fds->remote_addr != NULL)
  972. kfree(vp->fds->remote_addr);
  973. if (vp->transport_data != NULL)
  974. kfree(vp->transport_data);
  975. if (vp->header_rxbuffer != NULL)
  976. kfree(vp->header_rxbuffer);
  977. if (vp->header_txbuffer != NULL)
  978. kfree(vp->header_txbuffer);
  979. if (vp->rx_queue != NULL)
  980. destroy_queue(vp->rx_queue);
  981. if (vp->tx_queue != NULL)
  982. destroy_queue(vp->tx_queue);
  983. kfree(vp->fds);
  984. vp->fds = NULL;
  985. spin_lock_irqsave(&vp->lock, flags);
  986. vp->opened = false;
  987. spin_unlock_irqrestore(&vp->lock, flags);
  988. return 0;
  989. }
  990. /* TX tasklet */
  991. static void vector_tx_poll(unsigned long data)
  992. {
  993. struct vector_private *vp = (struct vector_private *)data;
  994. vp->estats.tx_kicks++;
  995. vector_send(vp->tx_queue);
  996. }
  997. static void vector_reset_tx(struct work_struct *work)
  998. {
  999. struct vector_private *vp =
  1000. container_of(work, struct vector_private, reset_tx);
  1001. netdev_reset_queue(vp->dev);
  1002. netif_start_queue(vp->dev);
  1003. netif_wake_queue(vp->dev);
  1004. }
  1005. static int vector_net_open(struct net_device *dev)
  1006. {
  1007. struct vector_private *vp = netdev_priv(dev);
  1008. unsigned long flags;
  1009. int err = -EINVAL;
  1010. struct vector_device *vdevice;
  1011. spin_lock_irqsave(&vp->lock, flags);
  1012. if (vp->opened) {
  1013. spin_unlock_irqrestore(&vp->lock, flags);
  1014. return -ENXIO;
  1015. }
  1016. vp->opened = true;
  1017. spin_unlock_irqrestore(&vp->lock, flags);
  1018. vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
  1019. if (vp->fds == NULL)
  1020. goto out_close;
  1021. if (build_transport_data(vp) < 0)
  1022. goto out_close;
  1023. if ((vp->options & VECTOR_RX) > 0) {
  1024. vp->rx_queue = create_queue(
  1025. vp,
  1026. get_depth(vp->parsed),
  1027. vp->rx_header_size,
  1028. MAX_IOV_SIZE
  1029. );
  1030. vp->rx_queue->queue_depth = get_depth(vp->parsed);
  1031. } else {
  1032. vp->header_rxbuffer = kmalloc(
  1033. vp->rx_header_size,
  1034. GFP_KERNEL
  1035. );
  1036. if (vp->header_rxbuffer == NULL)
  1037. goto out_close;
  1038. }
  1039. if ((vp->options & VECTOR_TX) > 0) {
  1040. vp->tx_queue = create_queue(
  1041. vp,
  1042. get_depth(vp->parsed),
  1043. vp->header_size,
  1044. MAX_IOV_SIZE
  1045. );
  1046. } else {
  1047. vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL);
  1048. if (vp->header_txbuffer == NULL)
  1049. goto out_close;
  1050. }
  1051. /* READ IRQ */
  1052. err = um_request_irq(
  1053. irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd,
  1054. IRQ_READ, vector_rx_interrupt,
  1055. IRQF_SHARED, dev->name, dev);
  1056. if (err != 0) {
  1057. netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err);
  1058. err = -ENETUNREACH;
  1059. goto out_close;
  1060. }
  1061. vp->rx_irq = irq_rr + VECTOR_BASE_IRQ;
  1062. dev->irq = irq_rr + VECTOR_BASE_IRQ;
  1063. irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
  1064. /* WRITE IRQ - we need it only if we have vector TX */
  1065. if ((vp->options & VECTOR_TX) > 0) {
  1066. err = um_request_irq(
  1067. irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd,
  1068. IRQ_WRITE, vector_tx_interrupt,
  1069. IRQF_SHARED, dev->name, dev);
  1070. if (err != 0) {
  1071. netdev_err(dev,
  1072. "vector_open: failed to get tx irq(%d)\n", err);
  1073. err = -ENETUNREACH;
  1074. goto out_close;
  1075. }
  1076. vp->tx_irq = irq_rr + VECTOR_BASE_IRQ;
  1077. irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
  1078. }
  1079. if ((vp->options & VECTOR_QDISC_BYPASS) != 0) {
  1080. if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
  1081. vp->options = vp->options | VECTOR_BPF;
  1082. }
  1083. if ((vp->options & VECTOR_BPF) != 0)
  1084. vp->bpf = uml_vector_default_bpf(vp->fds->rx_fd, dev->dev_addr);
  1085. netif_start_queue(dev);
  1086. /* clear buffer - it can happen that the host side of the interface
  1087. * is full when we get here. In this case, new data is never queued,
  1088. * SIGIOs never arrive, and the net never works.
  1089. */
  1090. vector_rx(vp);
  1091. vector_reset_stats(vp);
  1092. vdevice = find_device(vp->unit);
  1093. vdevice->opened = 1;
  1094. if ((vp->options & VECTOR_TX) != 0)
  1095. add_timer(&vp->tl);
  1096. return 0;
  1097. out_close:
  1098. vector_net_close(dev);
  1099. return err;
  1100. }
  1101. static void vector_net_set_multicast_list(struct net_device *dev)
  1102. {
  1103. /* TODO: - we can do some BPF games here */
  1104. return;
  1105. }
  1106. static void vector_net_tx_timeout(struct net_device *dev)
  1107. {
  1108. struct vector_private *vp = netdev_priv(dev);
  1109. vp->estats.tx_timeout_count++;
  1110. netif_trans_update(dev);
  1111. schedule_work(&vp->reset_tx);
  1112. }
  1113. static netdev_features_t vector_fix_features(struct net_device *dev,
  1114. netdev_features_t features)
  1115. {
  1116. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  1117. return features;
  1118. }
  1119. static int vector_set_features(struct net_device *dev,
  1120. netdev_features_t features)
  1121. {
  1122. struct vector_private *vp = netdev_priv(dev);
  1123. /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is
  1124. * no way to negotiate it on raw sockets, so we can change
  1125. * only our side.
  1126. */
  1127. if (features & NETIF_F_GRO)
  1128. /* All new frame buffers will be GRO-sized */
  1129. vp->req_size = 65536;
  1130. else
  1131. /* All new frame buffers will be normal sized */
  1132. vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN;
  1133. return 0;
  1134. }
  1135. #ifdef CONFIG_NET_POLL_CONTROLLER
  1136. static void vector_net_poll_controller(struct net_device *dev)
  1137. {
  1138. disable_irq(dev->irq);
  1139. vector_rx_interrupt(dev->irq, dev);
  1140. enable_irq(dev->irq);
  1141. }
  1142. #endif
  1143. static void vector_net_get_drvinfo(struct net_device *dev,
  1144. struct ethtool_drvinfo *info)
  1145. {
  1146. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  1147. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  1148. }
  1149. static void vector_get_ringparam(struct net_device *netdev,
  1150. struct ethtool_ringparam *ring)
  1151. {
  1152. struct vector_private *vp = netdev_priv(netdev);
  1153. ring->rx_max_pending = vp->rx_queue->max_depth;
  1154. ring->tx_max_pending = vp->tx_queue->max_depth;
  1155. ring->rx_pending = vp->rx_queue->max_depth;
  1156. ring->tx_pending = vp->tx_queue->max_depth;
  1157. }
  1158. static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  1159. {
  1160. switch (stringset) {
  1161. case ETH_SS_TEST:
  1162. *buf = '\0';
  1163. break;
  1164. case ETH_SS_STATS:
  1165. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  1166. break;
  1167. default:
  1168. WARN_ON(1);
  1169. break;
  1170. }
  1171. }
  1172. static int vector_get_sset_count(struct net_device *dev, int sset)
  1173. {
  1174. switch (sset) {
  1175. case ETH_SS_TEST:
  1176. return 0;
  1177. case ETH_SS_STATS:
  1178. return VECTOR_NUM_STATS;
  1179. default:
  1180. return -EOPNOTSUPP;
  1181. }
  1182. }
  1183. static void vector_get_ethtool_stats(struct net_device *dev,
  1184. struct ethtool_stats *estats,
  1185. u64 *tmp_stats)
  1186. {
  1187. struct vector_private *vp = netdev_priv(dev);
  1188. memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats));
  1189. }
  1190. static int vector_get_coalesce(struct net_device *netdev,
  1191. struct ethtool_coalesce *ec)
  1192. {
  1193. struct vector_private *vp = netdev_priv(netdev);
  1194. ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ;
  1195. return 0;
  1196. }
  1197. static int vector_set_coalesce(struct net_device *netdev,
  1198. struct ethtool_coalesce *ec)
  1199. {
  1200. struct vector_private *vp = netdev_priv(netdev);
  1201. vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000;
  1202. if (vp->coalesce == 0)
  1203. vp->coalesce = 1;
  1204. return 0;
  1205. }
  1206. static const struct ethtool_ops vector_net_ethtool_ops = {
  1207. .get_drvinfo = vector_net_get_drvinfo,
  1208. .get_link = ethtool_op_get_link,
  1209. .get_ts_info = ethtool_op_get_ts_info,
  1210. .get_ringparam = vector_get_ringparam,
  1211. .get_strings = vector_get_strings,
  1212. .get_sset_count = vector_get_sset_count,
  1213. .get_ethtool_stats = vector_get_ethtool_stats,
  1214. .get_coalesce = vector_get_coalesce,
  1215. .set_coalesce = vector_set_coalesce,
  1216. };
  1217. static const struct net_device_ops vector_netdev_ops = {
  1218. .ndo_open = vector_net_open,
  1219. .ndo_stop = vector_net_close,
  1220. .ndo_start_xmit = vector_net_start_xmit,
  1221. .ndo_set_rx_mode = vector_net_set_multicast_list,
  1222. .ndo_tx_timeout = vector_net_tx_timeout,
  1223. .ndo_set_mac_address = eth_mac_addr,
  1224. .ndo_validate_addr = eth_validate_addr,
  1225. .ndo_fix_features = vector_fix_features,
  1226. .ndo_set_features = vector_set_features,
  1227. #ifdef CONFIG_NET_POLL_CONTROLLER
  1228. .ndo_poll_controller = vector_net_poll_controller,
  1229. #endif
  1230. };
  1231. static void vector_timer_expire(struct timer_list *t)
  1232. {
  1233. struct vector_private *vp = from_timer(vp, t, tl);
  1234. vp->estats.tx_kicks++;
  1235. vector_send(vp->tx_queue);
  1236. }
  1237. static void vector_eth_configure(
  1238. int n,
  1239. struct arglist *def
  1240. )
  1241. {
  1242. struct vector_device *device;
  1243. struct net_device *dev;
  1244. struct vector_private *vp;
  1245. int err;
  1246. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1247. if (device == NULL) {
  1248. printk(KERN_ERR "eth_configure failed to allocate struct "
  1249. "vector_device\n");
  1250. return;
  1251. }
  1252. dev = alloc_etherdev(sizeof(struct vector_private));
  1253. if (dev == NULL) {
  1254. printk(KERN_ERR "eth_configure: failed to allocate struct "
  1255. "net_device for vec%d\n", n);
  1256. goto out_free_device;
  1257. }
  1258. dev->mtu = get_mtu(def);
  1259. INIT_LIST_HEAD(&device->list);
  1260. device->unit = n;
  1261. /* If this name ends up conflicting with an existing registered
  1262. * netdevice, that is OK, register_netdev{,ice}() will notice this
  1263. * and fail.
  1264. */
  1265. snprintf(dev->name, sizeof(dev->name), "vec%d", n);
  1266. uml_net_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac"));
  1267. vp = netdev_priv(dev);
  1268. /* sysfs register */
  1269. if (!driver_registered) {
  1270. platform_driver_register(&uml_net_driver);
  1271. driver_registered = 1;
  1272. }
  1273. device->pdev.id = n;
  1274. device->pdev.name = DRIVER_NAME;
  1275. device->pdev.dev.release = vector_device_release;
  1276. dev_set_drvdata(&device->pdev.dev, device);
  1277. if (platform_device_register(&device->pdev))
  1278. goto out_free_netdev;
  1279. SET_NETDEV_DEV(dev, &device->pdev.dev);
  1280. device->dev = dev;
  1281. *vp = ((struct vector_private)
  1282. {
  1283. .list = LIST_HEAD_INIT(vp->list),
  1284. .dev = dev,
  1285. .unit = n,
  1286. .options = get_transport_options(def),
  1287. .rx_irq = 0,
  1288. .tx_irq = 0,
  1289. .parsed = def,
  1290. .max_packet = get_mtu(def) + ETH_HEADER_OTHER,
  1291. /* TODO - we need to calculate headroom so that ip header
  1292. * is 16 byte aligned all the time
  1293. */
  1294. .headroom = get_headroom(def),
  1295. .form_header = NULL,
  1296. .verify_header = NULL,
  1297. .header_rxbuffer = NULL,
  1298. .header_txbuffer = NULL,
  1299. .header_size = 0,
  1300. .rx_header_size = 0,
  1301. .rexmit_scheduled = false,
  1302. .opened = false,
  1303. .transport_data = NULL,
  1304. .in_write_poll = false,
  1305. .coalesce = 2,
  1306. .req_size = get_req_size(def)
  1307. });
  1308. dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
  1309. tasklet_init(&vp->tx_poll, vector_tx_poll, (unsigned long)vp);
  1310. INIT_WORK(&vp->reset_tx, vector_reset_tx);
  1311. timer_setup(&vp->tl, vector_timer_expire, 0);
  1312. spin_lock_init(&vp->lock);
  1313. /* FIXME */
  1314. dev->netdev_ops = &vector_netdev_ops;
  1315. dev->ethtool_ops = &vector_net_ethtool_ops;
  1316. dev->watchdog_timeo = (HZ >> 1);
  1317. /* primary IRQ - fixme */
  1318. dev->irq = 0; /* we will adjust this once opened */
  1319. rtnl_lock();
  1320. err = register_netdevice(dev);
  1321. rtnl_unlock();
  1322. if (err)
  1323. goto out_undo_user_init;
  1324. spin_lock(&vector_devices_lock);
  1325. list_add(&device->list, &vector_devices);
  1326. spin_unlock(&vector_devices_lock);
  1327. return;
  1328. out_undo_user_init:
  1329. return;
  1330. out_free_netdev:
  1331. free_netdev(dev);
  1332. out_free_device:
  1333. kfree(device);
  1334. }
  1335. /*
  1336. * Invoked late in the init
  1337. */
  1338. static int __init vector_init(void)
  1339. {
  1340. struct list_head *ele;
  1341. struct vector_cmd_line_arg *def;
  1342. struct arglist *parsed;
  1343. list_for_each(ele, &vec_cmd_line) {
  1344. def = list_entry(ele, struct vector_cmd_line_arg, list);
  1345. parsed = uml_parse_vector_ifspec(def->arguments);
  1346. if (parsed != NULL)
  1347. vector_eth_configure(def->unit, parsed);
  1348. }
  1349. return 0;
  1350. }
  1351. /* Invoked at initial argument parsing, only stores
  1352. * arguments until a proper vector_init is called
  1353. * later
  1354. */
  1355. static int __init vector_setup(char *str)
  1356. {
  1357. char *error;
  1358. int n, err;
  1359. struct vector_cmd_line_arg *new;
  1360. err = vector_parse(str, &n, &str, &error);
  1361. if (err) {
  1362. printk(KERN_ERR "vector_setup - Couldn't parse '%s' : %s\n",
  1363. str, error);
  1364. return 1;
  1365. }
  1366. new = alloc_bootmem(sizeof(*new));
  1367. INIT_LIST_HEAD(&new->list);
  1368. new->unit = n;
  1369. new->arguments = str;
  1370. list_add_tail(&new->list, &vec_cmd_line);
  1371. return 1;
  1372. }
  1373. __setup("vec", vector_setup);
  1374. __uml_help(vector_setup,
  1375. "vec[0-9]+:<option>=<value>,<option>=<value>\n"
  1376. " Configure a vector io network device.\n\n"
  1377. );
  1378. late_initcall(vector_init);
  1379. static struct mc_device vector_mc = {
  1380. .list = LIST_HEAD_INIT(vector_mc.list),
  1381. .name = "vec",
  1382. .config = vector_config,
  1383. .get_config = NULL,
  1384. .id = vector_id,
  1385. .remove = vector_remove,
  1386. };
  1387. #ifdef CONFIG_INET
  1388. static int vector_inetaddr_event(
  1389. struct notifier_block *this,
  1390. unsigned long event,
  1391. void *ptr)
  1392. {
  1393. return NOTIFY_DONE;
  1394. }
  1395. static struct notifier_block vector_inetaddr_notifier = {
  1396. .notifier_call = vector_inetaddr_event,
  1397. };
  1398. static void inet_register(void)
  1399. {
  1400. register_inetaddr_notifier(&vector_inetaddr_notifier);
  1401. }
  1402. #else
  1403. static inline void inet_register(void)
  1404. {
  1405. }
  1406. #endif
  1407. static int vector_net_init(void)
  1408. {
  1409. mconsole_register_dev(&vector_mc);
  1410. inet_register();
  1411. return 0;
  1412. }
  1413. __initcall(vector_net_init);