vector_kern.c 42 KB

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