cangen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * cangen.c - CAN frames generator for testing purposes
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <linux-can@vger.kernel.org>
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <stdint.h>
  46. #include <unistd.h>
  47. #include <string.h>
  48. #include <signal.h>
  49. #include <poll.h>
  50. #include <ctype.h>
  51. #include <libgen.h>
  52. #include <time.h>
  53. #include <errno.h>
  54. #include <sys/time.h>
  55. #include <sys/types.h>
  56. #include <sys/socket.h>
  57. #include <sys/ioctl.h>
  58. #include <sys/uio.h>
  59. #include <net/if.h>
  60. #include <linux/can.h>
  61. #include <linux/can/raw.h>
  62. #include "lib.h"
  63. #define DEFAULT_GAP 200 /* ms */
  64. #define MODE_RANDOM 0
  65. #define MODE_INCREMENT 1
  66. #define MODE_FIX 2
  67. extern int optind, opterr, optopt;
  68. static volatile int running = 1;
  69. static unsigned long long enobufs_count;
  70. void print_usage(char *prg)
  71. {
  72. fprintf(stderr, "\n%s: generate CAN frames\n\n", prg);
  73. fprintf(stderr, "Usage: %s [options] <CAN interface>\n", prg);
  74. fprintf(stderr, "Options: -g <ms> (gap in milli seconds "
  75. "- default: %d ms)\n", DEFAULT_GAP);
  76. fprintf(stderr, " -e (generate extended frame mode "
  77. "(EFF) CAN frames)\n");
  78. fprintf(stderr, " -f (generate CAN FD CAN frames)\n");
  79. fprintf(stderr, " -b (generate CAN FD CAN frames"
  80. " with bitrate switch (BRS))\n");
  81. fprintf(stderr, " -R (send RTR frame)\n");
  82. fprintf(stderr, " -m (mix -e -f -b -R frames)\n");
  83. fprintf(stderr, " -I <mode> (CAN ID"
  84. " generation mode - see below)\n");
  85. fprintf(stderr, " -L <mode> (CAN data length code (dlc)"
  86. " generation mode - see below)\n");
  87. fprintf(stderr, " -D <mode> (CAN data (payload)"
  88. " generation mode - see below)\n");
  89. fprintf(stderr, " -p <timeout> (poll on -ENOBUFS to write frames"
  90. " with <timeout> ms)\n");
  91. fprintf(stderr, " -n <count> (terminate after <count> CAN frames "
  92. "- default infinite)\n");
  93. fprintf(stderr, " -i (ignore -ENOBUFS return values on"
  94. " write() syscalls)\n");
  95. fprintf(stderr, " -x (disable local loopback of "
  96. "generated CAN frames)\n");
  97. fprintf(stderr, " -v (increment verbose level for "
  98. "printing sent CAN frames)\n\n");
  99. fprintf(stderr, "Generation modes:\n");
  100. fprintf(stderr, "'r' => random values (default)\n");
  101. fprintf(stderr, "'i' => increment values\n");
  102. fprintf(stderr, "<hexvalue> => fix value using <hexvalue>\n\n");
  103. fprintf(stderr, "When incrementing the CAN data the data length code "
  104. "minimum is set to 1.\n");
  105. fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
  106. fprintf(stderr, "Examples:\n");
  107. fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v ", prg);
  108. fprintf(stderr, "(fixed CAN ID and length, inc. data)\n");
  109. fprintf(stderr, "%s vcan0 -e -L i -v -v -v ", prg);
  110. fprintf(stderr, "(generate EFF frames, incr. length)\n");
  111. fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8 ", prg);
  112. fprintf(stderr, "(fixed CAN data payload and length)\n");
  113. fprintf(stderr, "%s vcan0 -g 0 -i -x ", prg);
  114. fprintf(stderr, "(full load test ignoring -ENOBUFS)\n");
  115. fprintf(stderr, "%s vcan0 -g 0 -p 10 -x ", prg);
  116. fprintf(stderr, "(full load test with polling, 10ms timeout)\n");
  117. fprintf(stderr, "%s vcan0 ", prg);
  118. fprintf(stderr, "(my favourite default :)\n\n");
  119. }
  120. void sigterm(int signo)
  121. {
  122. running = 0;
  123. }
  124. int main(int argc, char **argv)
  125. {
  126. double gap = DEFAULT_GAP;
  127. unsigned long polltimeout = 0;
  128. unsigned char ignore_enobufs = 0;
  129. unsigned char extended = 0;
  130. unsigned char canfd = 0;
  131. unsigned char brs = 0;
  132. unsigned char mix = 0;
  133. unsigned char id_mode = MODE_RANDOM;
  134. unsigned char data_mode = MODE_RANDOM;
  135. unsigned char dlc_mode = MODE_RANDOM;
  136. unsigned char loopback_disable = 0;
  137. unsigned char verbose = 0;
  138. unsigned char rtr_frame = 0;
  139. int count = 0;
  140. int mtu, maxdlen;
  141. uint64_t incdata = 0;
  142. int incdlc = 0;
  143. unsigned long rnd;
  144. unsigned char fixdata[CANFD_MAX_DLEN];
  145. int opt;
  146. int s; /* socket */
  147. struct pollfd fds;
  148. struct sockaddr_can addr;
  149. static struct canfd_frame frame;
  150. int nbytes;
  151. int i;
  152. struct ifreq ifr;
  153. struct timespec ts;
  154. struct timeval now;
  155. /* set seed value for pseudo random numbers */
  156. gettimeofday(&now, NULL);
  157. srandom(now.tv_usec);
  158. signal(SIGTERM, sigterm);
  159. signal(SIGHUP, sigterm);
  160. signal(SIGINT, sigterm);
  161. while ((opt = getopt(argc, argv, "ig:ebfmI:L:D:xp:n:vRh?")) != -1) {
  162. switch (opt) {
  163. case 'i':
  164. ignore_enobufs = 1;
  165. break;
  166. case 'g':
  167. gap = strtod(optarg, NULL);
  168. break;
  169. case 'e':
  170. extended = 1;
  171. break;
  172. case 'f':
  173. canfd = 1;
  174. break;
  175. case 'b':
  176. brs = 1; /* bitrate switch implies CAN FD */
  177. canfd = 1;
  178. break;
  179. case 'm':
  180. mix = 1;
  181. canfd = 1; /* to switch the socket into CAN FD mode */
  182. break;
  183. case 'I':
  184. if (optarg[0] == 'r') {
  185. id_mode = MODE_RANDOM;
  186. } else if (optarg[0] == 'i') {
  187. id_mode = MODE_INCREMENT;
  188. } else {
  189. id_mode = MODE_FIX;
  190. frame.can_id = strtoul(optarg, NULL, 16);
  191. }
  192. break;
  193. case 'L':
  194. if (optarg[0] == 'r') {
  195. dlc_mode = MODE_RANDOM;
  196. } else if (optarg[0] == 'i') {
  197. dlc_mode = MODE_INCREMENT;
  198. } else {
  199. dlc_mode = MODE_FIX;
  200. frame.len = atoi(optarg) & 0xFF; /* is cut to 8 / 64 later */
  201. }
  202. break;
  203. case 'D':
  204. if (optarg[0] == 'r') {
  205. data_mode = MODE_RANDOM;
  206. } else if (optarg[0] == 'i') {
  207. data_mode = MODE_INCREMENT;
  208. } else {
  209. data_mode = MODE_FIX;
  210. if (hexstring2data(optarg, fixdata, CANFD_MAX_DLEN)) {
  211. printf ("wrong fix data definition\n");
  212. return 1;
  213. }
  214. }
  215. break;
  216. case 'v':
  217. verbose++;
  218. break;
  219. case 'x':
  220. loopback_disable = 1;
  221. break;
  222. case 'R':
  223. rtr_frame = 1;
  224. break;
  225. case 'p':
  226. polltimeout = strtoul(optarg, NULL, 10);
  227. break;
  228. case 'n':
  229. count = atoi(optarg);
  230. if (count < 1) {
  231. print_usage(basename(argv[0]));
  232. return 1;
  233. }
  234. break;
  235. case '?':
  236. case 'h':
  237. default:
  238. print_usage(basename(argv[0]));
  239. return 1;
  240. break;
  241. }
  242. }
  243. if (optind == argc) {
  244. print_usage(basename(argv[0]));
  245. return 1;
  246. }
  247. ts.tv_sec = gap / 1000;
  248. ts.tv_nsec = (long)(((long long)(gap * 1000000)) % 1000000000ll);
  249. /* recognize obviously missing commandline option */
  250. if (id_mode == MODE_FIX && frame.can_id > 0x7FF && !extended) {
  251. printf("The given CAN-ID is greater than 0x7FF and "
  252. "the '-e' option is not set.\n");
  253. return 1;
  254. }
  255. if (strlen(argv[optind]) >= IFNAMSIZ) {
  256. printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
  257. return 1;
  258. }
  259. if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
  260. perror("socket");
  261. return 1;
  262. }
  263. addr.can_family = AF_CAN;
  264. strcpy(ifr.ifr_name, argv[optind]);
  265. if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
  266. perror("SIOCGIFINDEX");
  267. return 1;
  268. }
  269. addr.can_ifindex = ifr.ifr_ifindex;
  270. /* disable default receive filter on this RAW socket */
  271. /* This is obsolete as we do not read from the socket at all, but for */
  272. /* this reason we can remove the receive list in the Kernel to save a */
  273. /* little (really a very little!) CPU usage. */
  274. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  275. if (loopback_disable) {
  276. int loopback = 0;
  277. setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
  278. &loopback, sizeof(loopback));
  279. }
  280. if (canfd) {
  281. int enable_canfd = 1;
  282. /* check if the frame fits into the CAN netdevice */
  283. if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
  284. perror("SIOCGIFMTU");
  285. return 1;
  286. }
  287. if (ifr.ifr_mtu != CANFD_MTU) {
  288. printf("CAN interface is not CAN FD capable - sorry.\n");
  289. return 1;
  290. }
  291. /* interface is ok - try to switch the socket into CAN FD mode */
  292. if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))){
  293. printf("error when enabling CAN FD support\n");
  294. return 1;
  295. }
  296. /* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
  297. frame.len = can_dlc2len(can_len2dlc(frame.len));
  298. } else {
  299. /* sanitize CAN 2.0 frame length */
  300. if (frame.len > 8)
  301. frame.len = 8;
  302. }
  303. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  304. perror("bind");
  305. return 1;
  306. }
  307. if (polltimeout) {
  308. fds.fd = s;
  309. fds.events = POLLOUT;
  310. }
  311. while (running) {
  312. frame.flags = 0;
  313. if (count && (--count == 0))
  314. running = 0;
  315. if (canfd){
  316. mtu = CANFD_MTU;
  317. maxdlen = CANFD_MAX_DLEN;
  318. if (brs)
  319. frame.flags |= CANFD_BRS;
  320. } else {
  321. mtu = CAN_MTU;
  322. maxdlen = CAN_MAX_DLEN;
  323. }
  324. if (id_mode == MODE_RANDOM)
  325. frame.can_id = random();
  326. if (extended) {
  327. frame.can_id &= CAN_EFF_MASK;
  328. frame.can_id |= CAN_EFF_FLAG;
  329. } else
  330. frame.can_id &= CAN_SFF_MASK;
  331. if (rtr_frame && !canfd)
  332. frame.can_id |= CAN_RTR_FLAG;
  333. if (dlc_mode == MODE_RANDOM) {
  334. if (canfd)
  335. frame.len = can_dlc2len(random() & 0xF);
  336. else {
  337. frame.len = random() & 0xF;
  338. if (frame.len & 8)
  339. frame.len = 8; /* for about 50% of the frames */
  340. }
  341. }
  342. if (data_mode == MODE_INCREMENT && !frame.len)
  343. frame.len = 1; /* min dlc value for incr. data */
  344. if (data_mode == MODE_RANDOM) {
  345. rnd = random();
  346. memcpy(&frame.data[0], &rnd, 4);
  347. rnd = random();
  348. memcpy(&frame.data[4], &rnd, 4);
  349. /* omit extra random number generation for CAN FD */
  350. if (canfd && frame.len > 8) {
  351. memcpy(&frame.data[8], &frame.data[0], 8);
  352. memcpy(&frame.data[16], &frame.data[0], 16);
  353. memcpy(&frame.data[32], &frame.data[0], 32);
  354. }
  355. }
  356. if (data_mode == MODE_FIX)
  357. memcpy(frame.data, fixdata, CANFD_MAX_DLEN);
  358. /* set unused payload data to zero like the CAN driver does it on rx */
  359. if (frame.len < maxdlen)
  360. memset(&frame.data[frame.len], 0, maxdlen - frame.len);
  361. if (verbose) {
  362. printf(" %s ", argv[optind]);
  363. if (verbose > 1)
  364. fprint_long_canframe(stdout, &frame, "\n", (verbose > 2)?1:0, maxdlen);
  365. else
  366. fprint_canframe(stdout, &frame, "\n", 1, maxdlen);
  367. }
  368. resend:
  369. nbytes = write(s, &frame, mtu);
  370. if (nbytes < 0) {
  371. if (errno != ENOBUFS) {
  372. perror("write");
  373. return 1;
  374. }
  375. if (!ignore_enobufs && !polltimeout) {
  376. perror("write");
  377. return 1;
  378. }
  379. if (polltimeout) {
  380. /* wait for the write socket (with timeout) */
  381. if (poll(&fds, 1, polltimeout) < 0) {
  382. perror("poll");
  383. return 1;
  384. } else
  385. goto resend;
  386. } else
  387. enobufs_count++;
  388. } else if (nbytes < mtu) {
  389. fprintf(stderr, "write: incomplete CAN frame\n");
  390. return 1;
  391. }
  392. if (gap) /* gap == 0 => performance test :-] */
  393. if (nanosleep(&ts, NULL))
  394. return 1;
  395. if (id_mode == MODE_INCREMENT)
  396. frame.can_id++;
  397. if (dlc_mode == MODE_INCREMENT) {
  398. incdlc++;
  399. if (canfd && !mix) {
  400. incdlc &= 0xF;
  401. frame.len = can_dlc2len(incdlc);
  402. } else {
  403. incdlc %= 9;
  404. frame.len = incdlc;
  405. }
  406. }
  407. if (data_mode == MODE_INCREMENT) {
  408. incdata++;
  409. for (i=0; i<8 ;i++)
  410. frame.data[i] = (incdata >> i*8) & 0xFFULL;
  411. }
  412. if (mix) {
  413. i = random();
  414. extended = i&1;
  415. canfd = i&2;
  416. if (canfd)
  417. brs = i&4;
  418. rtr_frame = ((i&24) == 24); /* reduce RTR frames to 1/4 */
  419. }
  420. }
  421. if (enobufs_count)
  422. printf("\nCounted %llu ENOBUFS return values on write().\n\n",
  423. enobufs_count);
  424. close(s);
  425. return 0;
  426. }