vector_kern.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. kfree(vp->bpf);
  978. kfree(vp->fds->remote_addr);
  979. kfree(vp->transport_data);
  980. kfree(vp->header_rxbuffer);
  981. kfree(vp->header_txbuffer);
  982. if (vp->rx_queue != NULL)
  983. destroy_queue(vp->rx_queue);
  984. if (vp->tx_queue != NULL)
  985. destroy_queue(vp->tx_queue);
  986. kfree(vp->fds);
  987. vp->fds = NULL;
  988. spin_lock_irqsave(&vp->lock, flags);
  989. vp->opened = false;
  990. spin_unlock_irqrestore(&vp->lock, flags);
  991. return 0;
  992. }
  993. /* TX tasklet */
  994. static void vector_tx_poll(unsigned long data)
  995. {
  996. struct vector_private *vp = (struct vector_private *)data;
  997. vp->estats.tx_kicks++;
  998. vector_send(vp->tx_queue);
  999. }
  1000. static void vector_reset_tx(struct work_struct *work)
  1001. {
  1002. struct vector_private *vp =
  1003. container_of(work, struct vector_private, reset_tx);
  1004. netdev_reset_queue(vp->dev);
  1005. netif_start_queue(vp->dev);
  1006. netif_wake_queue(vp->dev);
  1007. }
  1008. static int vector_net_open(struct net_device *dev)
  1009. {
  1010. struct vector_private *vp = netdev_priv(dev);
  1011. unsigned long flags;
  1012. int err = -EINVAL;
  1013. struct vector_device *vdevice;
  1014. spin_lock_irqsave(&vp->lock, flags);
  1015. if (vp->opened) {
  1016. spin_unlock_irqrestore(&vp->lock, flags);
  1017. return -ENXIO;
  1018. }
  1019. vp->opened = true;
  1020. spin_unlock_irqrestore(&vp->lock, flags);
  1021. vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
  1022. if (vp->fds == NULL)
  1023. goto out_close;
  1024. if (build_transport_data(vp) < 0)
  1025. goto out_close;
  1026. if ((vp->options & VECTOR_RX) > 0) {
  1027. vp->rx_queue = create_queue(
  1028. vp,
  1029. get_depth(vp->parsed),
  1030. vp->rx_header_size,
  1031. MAX_IOV_SIZE
  1032. );
  1033. vp->rx_queue->queue_depth = get_depth(vp->parsed);
  1034. } else {
  1035. vp->header_rxbuffer = kmalloc(
  1036. vp->rx_header_size,
  1037. GFP_KERNEL
  1038. );
  1039. if (vp->header_rxbuffer == NULL)
  1040. goto out_close;
  1041. }
  1042. if ((vp->options & VECTOR_TX) > 0) {
  1043. vp->tx_queue = create_queue(
  1044. vp,
  1045. get_depth(vp->parsed),
  1046. vp->header_size,
  1047. MAX_IOV_SIZE
  1048. );
  1049. } else {
  1050. vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL);
  1051. if (vp->header_txbuffer == NULL)
  1052. goto out_close;
  1053. }
  1054. /* READ IRQ */
  1055. err = um_request_irq(
  1056. irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd,
  1057. IRQ_READ, vector_rx_interrupt,
  1058. IRQF_SHARED, dev->name, dev);
  1059. if (err != 0) {
  1060. netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err);
  1061. err = -ENETUNREACH;
  1062. goto out_close;
  1063. }
  1064. vp->rx_irq = irq_rr + VECTOR_BASE_IRQ;
  1065. dev->irq = irq_rr + VECTOR_BASE_IRQ;
  1066. irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
  1067. /* WRITE IRQ - we need it only if we have vector TX */
  1068. if ((vp->options & VECTOR_TX) > 0) {
  1069. err = um_request_irq(
  1070. irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd,
  1071. IRQ_WRITE, vector_tx_interrupt,
  1072. IRQF_SHARED, dev->name, dev);
  1073. if (err != 0) {
  1074. netdev_err(dev,
  1075. "vector_open: failed to get tx irq(%d)\n", err);
  1076. err = -ENETUNREACH;
  1077. goto out_close;
  1078. }
  1079. vp->tx_irq = irq_rr + VECTOR_BASE_IRQ;
  1080. irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
  1081. }
  1082. if ((vp->options & VECTOR_QDISC_BYPASS) != 0) {
  1083. if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
  1084. vp->options |= VECTOR_BPF;
  1085. }
  1086. if ((vp->options & VECTOR_BPF) != 0)
  1087. vp->bpf = uml_vector_default_bpf(vp->fds->rx_fd, dev->dev_addr);
  1088. netif_start_queue(dev);
  1089. /* clear buffer - it can happen that the host side of the interface
  1090. * is full when we get here. In this case, new data is never queued,
  1091. * SIGIOs never arrive, and the net never works.
  1092. */
  1093. vector_rx(vp);
  1094. vector_reset_stats(vp);
  1095. vdevice = find_device(vp->unit);
  1096. vdevice->opened = 1;
  1097. if ((vp->options & VECTOR_TX) != 0)
  1098. add_timer(&vp->tl);
  1099. return 0;
  1100. out_close:
  1101. vector_net_close(dev);
  1102. return err;
  1103. }
  1104. static void vector_net_set_multicast_list(struct net_device *dev)
  1105. {
  1106. /* TODO: - we can do some BPF games here */
  1107. return;
  1108. }
  1109. static void vector_net_tx_timeout(struct net_device *dev)
  1110. {
  1111. struct vector_private *vp = netdev_priv(dev);
  1112. vp->estats.tx_timeout_count++;
  1113. netif_trans_update(dev);
  1114. schedule_work(&vp->reset_tx);
  1115. }
  1116. static netdev_features_t vector_fix_features(struct net_device *dev,
  1117. netdev_features_t features)
  1118. {
  1119. features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  1120. return features;
  1121. }
  1122. static int vector_set_features(struct net_device *dev,
  1123. netdev_features_t features)
  1124. {
  1125. struct vector_private *vp = netdev_priv(dev);
  1126. /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is
  1127. * no way to negotiate it on raw sockets, so we can change
  1128. * only our side.
  1129. */
  1130. if (features & NETIF_F_GRO)
  1131. /* All new frame buffers will be GRO-sized */
  1132. vp->req_size = 65536;
  1133. else
  1134. /* All new frame buffers will be normal sized */
  1135. vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN;
  1136. return 0;
  1137. }
  1138. #ifdef CONFIG_NET_POLL_CONTROLLER
  1139. static void vector_net_poll_controller(struct net_device *dev)
  1140. {
  1141. disable_irq(dev->irq);
  1142. vector_rx_interrupt(dev->irq, dev);
  1143. enable_irq(dev->irq);
  1144. }
  1145. #endif
  1146. static void vector_net_get_drvinfo(struct net_device *dev,
  1147. struct ethtool_drvinfo *info)
  1148. {
  1149. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  1150. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  1151. }
  1152. static void vector_get_ringparam(struct net_device *netdev,
  1153. struct ethtool_ringparam *ring)
  1154. {
  1155. struct vector_private *vp = netdev_priv(netdev);
  1156. ring->rx_max_pending = vp->rx_queue->max_depth;
  1157. ring->tx_max_pending = vp->tx_queue->max_depth;
  1158. ring->rx_pending = vp->rx_queue->max_depth;
  1159. ring->tx_pending = vp->tx_queue->max_depth;
  1160. }
  1161. static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  1162. {
  1163. switch (stringset) {
  1164. case ETH_SS_TEST:
  1165. *buf = '\0';
  1166. break;
  1167. case ETH_SS_STATS:
  1168. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  1169. break;
  1170. default:
  1171. WARN_ON(1);
  1172. break;
  1173. }
  1174. }
  1175. static int vector_get_sset_count(struct net_device *dev, int sset)
  1176. {
  1177. switch (sset) {
  1178. case ETH_SS_TEST:
  1179. return 0;
  1180. case ETH_SS_STATS:
  1181. return VECTOR_NUM_STATS;
  1182. default:
  1183. return -EOPNOTSUPP;
  1184. }
  1185. }
  1186. static void vector_get_ethtool_stats(struct net_device *dev,
  1187. struct ethtool_stats *estats,
  1188. u64 *tmp_stats)
  1189. {
  1190. struct vector_private *vp = netdev_priv(dev);
  1191. memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats));
  1192. }
  1193. static int vector_get_coalesce(struct net_device *netdev,
  1194. struct ethtool_coalesce *ec)
  1195. {
  1196. struct vector_private *vp = netdev_priv(netdev);
  1197. ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ;
  1198. return 0;
  1199. }
  1200. static int vector_set_coalesce(struct net_device *netdev,
  1201. struct ethtool_coalesce *ec)
  1202. {
  1203. struct vector_private *vp = netdev_priv(netdev);
  1204. vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000;
  1205. if (vp->coalesce == 0)
  1206. vp->coalesce = 1;
  1207. return 0;
  1208. }
  1209. static const struct ethtool_ops vector_net_ethtool_ops = {
  1210. .get_drvinfo = vector_net_get_drvinfo,
  1211. .get_link = ethtool_op_get_link,
  1212. .get_ts_info = ethtool_op_get_ts_info,
  1213. .get_ringparam = vector_get_ringparam,
  1214. .get_strings = vector_get_strings,
  1215. .get_sset_count = vector_get_sset_count,
  1216. .get_ethtool_stats = vector_get_ethtool_stats,
  1217. .get_coalesce = vector_get_coalesce,
  1218. .set_coalesce = vector_set_coalesce,
  1219. };
  1220. static const struct net_device_ops vector_netdev_ops = {
  1221. .ndo_open = vector_net_open,
  1222. .ndo_stop = vector_net_close,
  1223. .ndo_start_xmit = vector_net_start_xmit,
  1224. .ndo_set_rx_mode = vector_net_set_multicast_list,
  1225. .ndo_tx_timeout = vector_net_tx_timeout,
  1226. .ndo_set_mac_address = eth_mac_addr,
  1227. .ndo_validate_addr = eth_validate_addr,
  1228. .ndo_fix_features = vector_fix_features,
  1229. .ndo_set_features = vector_set_features,
  1230. #ifdef CONFIG_NET_POLL_CONTROLLER
  1231. .ndo_poll_controller = vector_net_poll_controller,
  1232. #endif
  1233. };
  1234. static void vector_timer_expire(struct timer_list *t)
  1235. {
  1236. struct vector_private *vp = from_timer(vp, t, tl);
  1237. vp->estats.tx_kicks++;
  1238. vector_send(vp->tx_queue);
  1239. }
  1240. static void vector_eth_configure(
  1241. int n,
  1242. struct arglist *def
  1243. )
  1244. {
  1245. struct vector_device *device;
  1246. struct net_device *dev;
  1247. struct vector_private *vp;
  1248. int err;
  1249. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1250. if (device == NULL) {
  1251. printk(KERN_ERR "eth_configure failed to allocate struct "
  1252. "vector_device\n");
  1253. return;
  1254. }
  1255. dev = alloc_etherdev(sizeof(struct vector_private));
  1256. if (dev == NULL) {
  1257. printk(KERN_ERR "eth_configure: failed to allocate struct "
  1258. "net_device for vec%d\n", n);
  1259. goto out_free_device;
  1260. }
  1261. dev->mtu = get_mtu(def);
  1262. INIT_LIST_HEAD(&device->list);
  1263. device->unit = n;
  1264. /* If this name ends up conflicting with an existing registered
  1265. * netdevice, that is OK, register_netdev{,ice}() will notice this
  1266. * and fail.
  1267. */
  1268. snprintf(dev->name, sizeof(dev->name), "vec%d", n);
  1269. uml_net_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac"));
  1270. vp = netdev_priv(dev);
  1271. /* sysfs register */
  1272. if (!driver_registered) {
  1273. platform_driver_register(&uml_net_driver);
  1274. driver_registered = 1;
  1275. }
  1276. device->pdev.id = n;
  1277. device->pdev.name = DRIVER_NAME;
  1278. device->pdev.dev.release = vector_device_release;
  1279. dev_set_drvdata(&device->pdev.dev, device);
  1280. if (platform_device_register(&device->pdev))
  1281. goto out_free_netdev;
  1282. SET_NETDEV_DEV(dev, &device->pdev.dev);
  1283. device->dev = dev;
  1284. *vp = ((struct vector_private)
  1285. {
  1286. .list = LIST_HEAD_INIT(vp->list),
  1287. .dev = dev,
  1288. .unit = n,
  1289. .options = get_transport_options(def),
  1290. .rx_irq = 0,
  1291. .tx_irq = 0,
  1292. .parsed = def,
  1293. .max_packet = get_mtu(def) + ETH_HEADER_OTHER,
  1294. /* TODO - we need to calculate headroom so that ip header
  1295. * is 16 byte aligned all the time
  1296. */
  1297. .headroom = get_headroom(def),
  1298. .form_header = NULL,
  1299. .verify_header = NULL,
  1300. .header_rxbuffer = NULL,
  1301. .header_txbuffer = NULL,
  1302. .header_size = 0,
  1303. .rx_header_size = 0,
  1304. .rexmit_scheduled = false,
  1305. .opened = false,
  1306. .transport_data = NULL,
  1307. .in_write_poll = false,
  1308. .coalesce = 2,
  1309. .req_size = get_req_size(def)
  1310. });
  1311. dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
  1312. tasklet_init(&vp->tx_poll, vector_tx_poll, (unsigned long)vp);
  1313. INIT_WORK(&vp->reset_tx, vector_reset_tx);
  1314. timer_setup(&vp->tl, vector_timer_expire, 0);
  1315. spin_lock_init(&vp->lock);
  1316. /* FIXME */
  1317. dev->netdev_ops = &vector_netdev_ops;
  1318. dev->ethtool_ops = &vector_net_ethtool_ops;
  1319. dev->watchdog_timeo = (HZ >> 1);
  1320. /* primary IRQ - fixme */
  1321. dev->irq = 0; /* we will adjust this once opened */
  1322. rtnl_lock();
  1323. err = register_netdevice(dev);
  1324. rtnl_unlock();
  1325. if (err)
  1326. goto out_undo_user_init;
  1327. spin_lock(&vector_devices_lock);
  1328. list_add(&device->list, &vector_devices);
  1329. spin_unlock(&vector_devices_lock);
  1330. return;
  1331. out_undo_user_init:
  1332. return;
  1333. out_free_netdev:
  1334. free_netdev(dev);
  1335. out_free_device:
  1336. kfree(device);
  1337. }
  1338. /*
  1339. * Invoked late in the init
  1340. */
  1341. static int __init vector_init(void)
  1342. {
  1343. struct list_head *ele;
  1344. struct vector_cmd_line_arg *def;
  1345. struct arglist *parsed;
  1346. list_for_each(ele, &vec_cmd_line) {
  1347. def = list_entry(ele, struct vector_cmd_line_arg, list);
  1348. parsed = uml_parse_vector_ifspec(def->arguments);
  1349. if (parsed != NULL)
  1350. vector_eth_configure(def->unit, parsed);
  1351. }
  1352. return 0;
  1353. }
  1354. /* Invoked at initial argument parsing, only stores
  1355. * arguments until a proper vector_init is called
  1356. * later
  1357. */
  1358. static int __init vector_setup(char *str)
  1359. {
  1360. char *error;
  1361. int n, err;
  1362. struct vector_cmd_line_arg *new;
  1363. err = vector_parse(str, &n, &str, &error);
  1364. if (err) {
  1365. printk(KERN_ERR "vector_setup - Couldn't parse '%s' : %s\n",
  1366. str, error);
  1367. return 1;
  1368. }
  1369. new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
  1370. INIT_LIST_HEAD(&new->list);
  1371. new->unit = n;
  1372. new->arguments = str;
  1373. list_add_tail(&new->list, &vec_cmd_line);
  1374. return 1;
  1375. }
  1376. __setup("vec", vector_setup);
  1377. __uml_help(vector_setup,
  1378. "vec[0-9]+:<option>=<value>,<option>=<value>\n"
  1379. " Configure a vector io network device.\n\n"
  1380. );
  1381. late_initcall(vector_init);
  1382. static struct mc_device vector_mc = {
  1383. .list = LIST_HEAD_INIT(vector_mc.list),
  1384. .name = "vec",
  1385. .config = vector_config,
  1386. .get_config = NULL,
  1387. .id = vector_id,
  1388. .remove = vector_remove,
  1389. };
  1390. #ifdef CONFIG_INET
  1391. static int vector_inetaddr_event(
  1392. struct notifier_block *this,
  1393. unsigned long event,
  1394. void *ptr)
  1395. {
  1396. return NOTIFY_DONE;
  1397. }
  1398. static struct notifier_block vector_inetaddr_notifier = {
  1399. .notifier_call = vector_inetaddr_event,
  1400. };
  1401. static void inet_register(void)
  1402. {
  1403. register_inetaddr_notifier(&vector_inetaddr_notifier);
  1404. }
  1405. #else
  1406. static inline void inet_register(void)
  1407. {
  1408. }
  1409. #endif
  1410. static int vector_net_init(void)
  1411. {
  1412. mconsole_register_dev(&vector_mc);
  1413. inet_register();
  1414. return 0;
  1415. }
  1416. __initcall(vector_net_init);