wwan_hwsim.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * WWAN device simulator for WWAN framework testing.
  4. *
  5. * Copyright (c) 2021, Sergey Ryazanov <ryazanov.s.a@gmail.com>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/device.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/list.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/wwan.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/workqueue.h>
  19. #include <net/arp.h>
  20. static int wwan_hwsim_devsnum = 2;
  21. module_param_named(devices, wwan_hwsim_devsnum, int, 0444);
  22. MODULE_PARM_DESC(devices, "Number of simulated devices");
  23. static const struct class wwan_hwsim_class = {
  24. .name = "wwan_hwsim",
  25. };
  26. static struct dentry *wwan_hwsim_debugfs_topdir;
  27. static struct dentry *wwan_hwsim_debugfs_devcreate;
  28. static DEFINE_SPINLOCK(wwan_hwsim_devs_lock);
  29. static LIST_HEAD(wwan_hwsim_devs);
  30. static unsigned int wwan_hwsim_dev_idx;
  31. static struct workqueue_struct *wwan_wq;
  32. struct wwan_hwsim_dev {
  33. struct list_head list;
  34. unsigned int id;
  35. struct device dev;
  36. struct work_struct del_work;
  37. struct dentry *debugfs_topdir;
  38. struct dentry *debugfs_portcreate;
  39. spinlock_t ports_lock; /* Serialize ports creation/deletion */
  40. unsigned int port_idx;
  41. struct list_head ports;
  42. };
  43. struct wwan_hwsim_port {
  44. struct list_head list;
  45. unsigned int id;
  46. struct wwan_hwsim_dev *dev;
  47. struct wwan_port *wwan;
  48. struct work_struct del_work;
  49. struct dentry *debugfs_topdir;
  50. enum { /* AT command parser state */
  51. AT_PARSER_WAIT_A,
  52. AT_PARSER_WAIT_T,
  53. AT_PARSER_WAIT_TERM,
  54. AT_PARSER_SKIP_LINE,
  55. } pstate;
  56. };
  57. static const struct file_operations wwan_hwsim_debugfs_portdestroy_fops;
  58. static const struct file_operations wwan_hwsim_debugfs_portcreate_fops;
  59. static const struct file_operations wwan_hwsim_debugfs_devdestroy_fops;
  60. static void wwan_hwsim_port_del_work(struct work_struct *work);
  61. static void wwan_hwsim_dev_del_work(struct work_struct *work);
  62. static netdev_tx_t wwan_hwsim_netdev_xmit(struct sk_buff *skb,
  63. struct net_device *ndev)
  64. {
  65. ndev->stats.tx_packets++;
  66. ndev->stats.tx_bytes += skb->len;
  67. consume_skb(skb);
  68. return NETDEV_TX_OK;
  69. }
  70. static const struct net_device_ops wwan_hwsim_netdev_ops = {
  71. .ndo_start_xmit = wwan_hwsim_netdev_xmit,
  72. };
  73. static void wwan_hwsim_netdev_setup(struct net_device *ndev)
  74. {
  75. ndev->netdev_ops = &wwan_hwsim_netdev_ops;
  76. ndev->needs_free_netdev = true;
  77. ndev->mtu = ETH_DATA_LEN;
  78. ndev->min_mtu = ETH_MIN_MTU;
  79. ndev->max_mtu = ETH_MAX_MTU;
  80. ndev->type = ARPHRD_NONE;
  81. ndev->flags = IFF_POINTOPOINT | IFF_NOARP;
  82. }
  83. static const struct wwan_ops wwan_hwsim_wwan_rtnl_ops = {
  84. .priv_size = 0, /* No private data */
  85. .setup = wwan_hwsim_netdev_setup,
  86. };
  87. static int wwan_hwsim_port_start(struct wwan_port *wport)
  88. {
  89. struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport);
  90. port->pstate = AT_PARSER_WAIT_A;
  91. return 0;
  92. }
  93. static void wwan_hwsim_port_stop(struct wwan_port *wport)
  94. {
  95. }
  96. /* Implements a minimalistic AT commands parser that echo input back and
  97. * reply with 'OK' to each input command. See AT command protocol details in the
  98. * ITU-T V.250 recomendations document.
  99. *
  100. * Be aware that this processor is not fully V.250 compliant.
  101. */
  102. static int wwan_hwsim_port_tx(struct wwan_port *wport, struct sk_buff *in)
  103. {
  104. struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport);
  105. struct sk_buff *out;
  106. int i, n, s;
  107. /* Estimate a max possible number of commands by counting the number of
  108. * termination chars (S3 param, CR by default). And then allocate the
  109. * output buffer that will be enough to fit the echo and result codes of
  110. * all commands.
  111. */
  112. for (i = 0, n = 0; i < in->len; ++i)
  113. if (in->data[i] == '\r')
  114. n++;
  115. n = in->len + n * (2 + 2 + 2); /* Output buffer size */
  116. out = alloc_skb(n, GFP_KERNEL);
  117. if (!out)
  118. return -ENOMEM;
  119. for (i = 0, s = 0; i < in->len; ++i) {
  120. char c = in->data[i];
  121. if (port->pstate == AT_PARSER_WAIT_A) {
  122. if (c == 'A' || c == 'a')
  123. port->pstate = AT_PARSER_WAIT_T;
  124. else if (c != '\n') /* Ignore formating char */
  125. port->pstate = AT_PARSER_SKIP_LINE;
  126. } else if (port->pstate == AT_PARSER_WAIT_T) {
  127. if (c == 'T' || c == 't')
  128. port->pstate = AT_PARSER_WAIT_TERM;
  129. else
  130. port->pstate = AT_PARSER_SKIP_LINE;
  131. } else if (port->pstate == AT_PARSER_WAIT_TERM) {
  132. if (c != '\r')
  133. continue;
  134. /* Consume the trailing formatting char as well */
  135. if ((i + 1) < in->len && in->data[i + 1] == '\n')
  136. i++;
  137. n = i - s + 1;
  138. skb_put_data(out, &in->data[s], n);/* Echo */
  139. skb_put_data(out, "\r\nOK\r\n", 6);
  140. s = i + 1;
  141. port->pstate = AT_PARSER_WAIT_A;
  142. } else if (port->pstate == AT_PARSER_SKIP_LINE) {
  143. if (c != '\r')
  144. continue;
  145. port->pstate = AT_PARSER_WAIT_A;
  146. }
  147. }
  148. if (i > s) {
  149. /* Echo the processed portion of a not yet completed command */
  150. n = i - s;
  151. skb_put_data(out, &in->data[s], n);
  152. }
  153. consume_skb(in);
  154. wwan_port_rx(wport, out);
  155. return 0;
  156. }
  157. static const struct wwan_port_ops wwan_hwsim_port_ops = {
  158. .start = wwan_hwsim_port_start,
  159. .stop = wwan_hwsim_port_stop,
  160. .tx = wwan_hwsim_port_tx,
  161. };
  162. static struct wwan_hwsim_port *wwan_hwsim_port_new(struct wwan_hwsim_dev *dev)
  163. {
  164. struct wwan_hwsim_port *port;
  165. char name[0x10];
  166. int err;
  167. port = kzalloc(sizeof(*port), GFP_KERNEL);
  168. if (!port)
  169. return ERR_PTR(-ENOMEM);
  170. port->dev = dev;
  171. spin_lock(&dev->ports_lock);
  172. port->id = dev->port_idx++;
  173. spin_unlock(&dev->ports_lock);
  174. port->wwan = wwan_create_port(&dev->dev, WWAN_PORT_AT,
  175. &wwan_hwsim_port_ops,
  176. NULL, port);
  177. if (IS_ERR(port->wwan)) {
  178. err = PTR_ERR(port->wwan);
  179. goto err_free_port;
  180. }
  181. INIT_WORK(&port->del_work, wwan_hwsim_port_del_work);
  182. snprintf(name, sizeof(name), "port%u", port->id);
  183. port->debugfs_topdir = debugfs_create_dir(name, dev->debugfs_topdir);
  184. debugfs_create_file("destroy", 0200, port->debugfs_topdir, port,
  185. &wwan_hwsim_debugfs_portdestroy_fops);
  186. return port;
  187. err_free_port:
  188. kfree(port);
  189. return ERR_PTR(err);
  190. }
  191. static void wwan_hwsim_port_del(struct wwan_hwsim_port *port)
  192. {
  193. debugfs_remove(port->debugfs_topdir);
  194. /* Make sure that there is no pending deletion work */
  195. if (current_work() != &port->del_work)
  196. cancel_work_sync(&port->del_work);
  197. wwan_remove_port(port->wwan);
  198. kfree(port);
  199. }
  200. static void wwan_hwsim_port_del_work(struct work_struct *work)
  201. {
  202. struct wwan_hwsim_port *port =
  203. container_of(work, typeof(*port), del_work);
  204. struct wwan_hwsim_dev *dev = port->dev;
  205. spin_lock(&dev->ports_lock);
  206. if (list_empty(&port->list)) {
  207. /* Someone else deleting port at the moment */
  208. spin_unlock(&dev->ports_lock);
  209. return;
  210. }
  211. list_del_init(&port->list);
  212. spin_unlock(&dev->ports_lock);
  213. wwan_hwsim_port_del(port);
  214. }
  215. static void wwan_hwsim_dev_release(struct device *sysdev)
  216. {
  217. struct wwan_hwsim_dev *dev = container_of(sysdev, typeof(*dev), dev);
  218. kfree(dev);
  219. }
  220. static struct wwan_hwsim_dev *wwan_hwsim_dev_new(void)
  221. {
  222. struct wwan_hwsim_dev *dev;
  223. int err;
  224. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  225. if (!dev)
  226. return ERR_PTR(-ENOMEM);
  227. spin_lock(&wwan_hwsim_devs_lock);
  228. dev->id = wwan_hwsim_dev_idx++;
  229. spin_unlock(&wwan_hwsim_devs_lock);
  230. dev->dev.release = wwan_hwsim_dev_release;
  231. dev->dev.class = &wwan_hwsim_class;
  232. dev_set_name(&dev->dev, "hwsim%u", dev->id);
  233. spin_lock_init(&dev->ports_lock);
  234. INIT_LIST_HEAD(&dev->ports);
  235. err = device_register(&dev->dev);
  236. if (err)
  237. goto err_free_dev;
  238. INIT_WORK(&dev->del_work, wwan_hwsim_dev_del_work);
  239. err = wwan_register_ops(&dev->dev, &wwan_hwsim_wwan_rtnl_ops, dev, 1);
  240. if (err)
  241. goto err_unreg_dev;
  242. dev->debugfs_topdir = debugfs_create_dir(dev_name(&dev->dev),
  243. wwan_hwsim_debugfs_topdir);
  244. debugfs_create_file("destroy", 0200, dev->debugfs_topdir, dev,
  245. &wwan_hwsim_debugfs_devdestroy_fops);
  246. dev->debugfs_portcreate =
  247. debugfs_create_file("portcreate", 0200,
  248. dev->debugfs_topdir, dev,
  249. &wwan_hwsim_debugfs_portcreate_fops);
  250. return dev;
  251. err_unreg_dev:
  252. device_unregister(&dev->dev);
  253. /* Memory will be freed in the device release callback */
  254. return ERR_PTR(err);
  255. err_free_dev:
  256. put_device(&dev->dev);
  257. return ERR_PTR(err);
  258. }
  259. static void wwan_hwsim_dev_del(struct wwan_hwsim_dev *dev)
  260. {
  261. debugfs_remove(dev->debugfs_portcreate); /* Avoid new ports */
  262. spin_lock(&dev->ports_lock);
  263. while (!list_empty(&dev->ports)) {
  264. struct wwan_hwsim_port *port;
  265. port = list_first_entry(&dev->ports, struct wwan_hwsim_port,
  266. list);
  267. list_del_init(&port->list);
  268. spin_unlock(&dev->ports_lock);
  269. wwan_hwsim_port_del(port);
  270. spin_lock(&dev->ports_lock);
  271. }
  272. spin_unlock(&dev->ports_lock);
  273. debugfs_remove(dev->debugfs_topdir);
  274. /* This will remove all child netdev(s) */
  275. wwan_unregister_ops(&dev->dev);
  276. /* Make sure that there is no pending deletion work */
  277. if (current_work() != &dev->del_work)
  278. cancel_work_sync(&dev->del_work);
  279. device_unregister(&dev->dev);
  280. /* Memory will be freed in the device release callback */
  281. }
  282. static void wwan_hwsim_dev_del_work(struct work_struct *work)
  283. {
  284. struct wwan_hwsim_dev *dev = container_of(work, typeof(*dev), del_work);
  285. spin_lock(&wwan_hwsim_devs_lock);
  286. if (list_empty(&dev->list)) {
  287. /* Someone else deleting device at the moment */
  288. spin_unlock(&wwan_hwsim_devs_lock);
  289. return;
  290. }
  291. list_del_init(&dev->list);
  292. spin_unlock(&wwan_hwsim_devs_lock);
  293. wwan_hwsim_dev_del(dev);
  294. }
  295. static ssize_t wwan_hwsim_debugfs_portdestroy_write(struct file *file,
  296. const char __user *usrbuf,
  297. size_t count, loff_t *ppos)
  298. {
  299. struct wwan_hwsim_port *port = file->private_data;
  300. /* We can not delete port here since it will cause a deadlock due to
  301. * waiting this callback to finish in the debugfs_remove() call. So,
  302. * use workqueue.
  303. */
  304. queue_work(wwan_wq, &port->del_work);
  305. return count;
  306. }
  307. static const struct file_operations wwan_hwsim_debugfs_portdestroy_fops = {
  308. .write = wwan_hwsim_debugfs_portdestroy_write,
  309. .open = simple_open,
  310. .llseek = noop_llseek,
  311. };
  312. static ssize_t wwan_hwsim_debugfs_portcreate_write(struct file *file,
  313. const char __user *usrbuf,
  314. size_t count, loff_t *ppos)
  315. {
  316. struct wwan_hwsim_dev *dev = file->private_data;
  317. struct wwan_hwsim_port *port;
  318. port = wwan_hwsim_port_new(dev);
  319. if (IS_ERR(port))
  320. return PTR_ERR(port);
  321. spin_lock(&dev->ports_lock);
  322. list_add_tail(&port->list, &dev->ports);
  323. spin_unlock(&dev->ports_lock);
  324. return count;
  325. }
  326. static const struct file_operations wwan_hwsim_debugfs_portcreate_fops = {
  327. .write = wwan_hwsim_debugfs_portcreate_write,
  328. .open = simple_open,
  329. .llseek = noop_llseek,
  330. };
  331. static ssize_t wwan_hwsim_debugfs_devdestroy_write(struct file *file,
  332. const char __user *usrbuf,
  333. size_t count, loff_t *ppos)
  334. {
  335. struct wwan_hwsim_dev *dev = file->private_data;
  336. /* We can not delete device here since it will cause a deadlock due to
  337. * waiting this callback to finish in the debugfs_remove() call. So,
  338. * use workqueue.
  339. */
  340. queue_work(wwan_wq, &dev->del_work);
  341. return count;
  342. }
  343. static const struct file_operations wwan_hwsim_debugfs_devdestroy_fops = {
  344. .write = wwan_hwsim_debugfs_devdestroy_write,
  345. .open = simple_open,
  346. .llseek = noop_llseek,
  347. };
  348. static ssize_t wwan_hwsim_debugfs_devcreate_write(struct file *file,
  349. const char __user *usrbuf,
  350. size_t count, loff_t *ppos)
  351. {
  352. struct wwan_hwsim_dev *dev;
  353. dev = wwan_hwsim_dev_new();
  354. if (IS_ERR(dev))
  355. return PTR_ERR(dev);
  356. spin_lock(&wwan_hwsim_devs_lock);
  357. list_add_tail(&dev->list, &wwan_hwsim_devs);
  358. spin_unlock(&wwan_hwsim_devs_lock);
  359. return count;
  360. }
  361. static const struct file_operations wwan_hwsim_debugfs_devcreate_fops = {
  362. .write = wwan_hwsim_debugfs_devcreate_write,
  363. .open = simple_open,
  364. .llseek = noop_llseek,
  365. };
  366. static int __init wwan_hwsim_init_devs(void)
  367. {
  368. struct wwan_hwsim_dev *dev;
  369. int i, j;
  370. for (i = 0; i < wwan_hwsim_devsnum; ++i) {
  371. dev = wwan_hwsim_dev_new();
  372. if (IS_ERR(dev))
  373. return PTR_ERR(dev);
  374. spin_lock(&wwan_hwsim_devs_lock);
  375. list_add_tail(&dev->list, &wwan_hwsim_devs);
  376. spin_unlock(&wwan_hwsim_devs_lock);
  377. /* Create a couple of ports per each device to accelerate
  378. * the simulator readiness time.
  379. */
  380. for (j = 0; j < 2; ++j) {
  381. struct wwan_hwsim_port *port;
  382. port = wwan_hwsim_port_new(dev);
  383. if (IS_ERR(port))
  384. return PTR_ERR(port);
  385. spin_lock(&dev->ports_lock);
  386. list_add_tail(&port->list, &dev->ports);
  387. spin_unlock(&dev->ports_lock);
  388. }
  389. }
  390. return 0;
  391. }
  392. static void wwan_hwsim_free_devs(void)
  393. {
  394. struct wwan_hwsim_dev *dev;
  395. spin_lock(&wwan_hwsim_devs_lock);
  396. while (!list_empty(&wwan_hwsim_devs)) {
  397. dev = list_first_entry(&wwan_hwsim_devs, struct wwan_hwsim_dev,
  398. list);
  399. list_del_init(&dev->list);
  400. spin_unlock(&wwan_hwsim_devs_lock);
  401. wwan_hwsim_dev_del(dev);
  402. spin_lock(&wwan_hwsim_devs_lock);
  403. }
  404. spin_unlock(&wwan_hwsim_devs_lock);
  405. }
  406. static int __init wwan_hwsim_init(void)
  407. {
  408. int err;
  409. if (wwan_hwsim_devsnum < 0 || wwan_hwsim_devsnum > 128)
  410. return -EINVAL;
  411. wwan_wq = alloc_workqueue("wwan_wq", 0, 0);
  412. if (!wwan_wq)
  413. return -ENOMEM;
  414. err = class_register(&wwan_hwsim_class);
  415. if (err)
  416. goto err_wq_destroy;
  417. wwan_hwsim_debugfs_topdir = debugfs_create_dir("wwan_hwsim", NULL);
  418. wwan_hwsim_debugfs_devcreate =
  419. debugfs_create_file("devcreate", 0200,
  420. wwan_hwsim_debugfs_topdir, NULL,
  421. &wwan_hwsim_debugfs_devcreate_fops);
  422. err = wwan_hwsim_init_devs();
  423. if (err)
  424. goto err_clean_devs;
  425. return 0;
  426. err_clean_devs:
  427. debugfs_remove(wwan_hwsim_debugfs_devcreate); /* Avoid new devs */
  428. wwan_hwsim_free_devs();
  429. flush_workqueue(wwan_wq); /* Wait deletion works completion */
  430. debugfs_remove(wwan_hwsim_debugfs_topdir);
  431. class_unregister(&wwan_hwsim_class);
  432. err_wq_destroy:
  433. destroy_workqueue(wwan_wq);
  434. return err;
  435. }
  436. static void __exit wwan_hwsim_exit(void)
  437. {
  438. debugfs_remove(wwan_hwsim_debugfs_devcreate); /* Avoid new devs */
  439. wwan_hwsim_free_devs();
  440. flush_workqueue(wwan_wq); /* Wait deletion works completion */
  441. debugfs_remove(wwan_hwsim_debugfs_topdir);
  442. class_unregister(&wwan_hwsim_class);
  443. destroy_workqueue(wwan_wq);
  444. }
  445. module_init(wwan_hwsim_init);
  446. module_exit(wwan_hwsim_exit);
  447. MODULE_AUTHOR("Sergey Ryazanov");
  448. MODULE_DESCRIPTION("Device simulator for WWAN framework");
  449. MODULE_LICENSE("GPL");