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