cangen: support socket priority

Add '-P' option for allow user to set the socket priority. This can be
useful in conjuction with queuing discipline.

Signed-off-by: Zhu Yi <yi.zhu5@cn.bosch.com>
Signed-off-by: Hubert Streidl <hubert.streidl@de.bosch.com>
Signed-off-by: Mark Jonas <mark.jonas@de.bosch.com>
Link: https://lore.kernel.org/r/20250120162332.19157-1-mark.jonas@de.bosch.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
pull/571/head
Zhu Yi 2025-01-20 17:23:30 +01:00 committed by Marc Kleine-Budde
parent fc1f6979c0
commit 6eb97b57c5
1 changed files with 21 additions and 1 deletions

View File

@ -189,6 +189,7 @@ static void print_usage(char *prg)
fprintf(stderr, " -A <mode> (CAN XL AF generation mode - see below, no e/o mode)\n"); fprintf(stderr, " -A <mode> (CAN XL AF generation mode - see below, no e/o mode)\n");
fprintf(stderr, " -V <mode> (CAN XL VCID generation mode - see below, no e/o mode)\n"); fprintf(stderr, " -V <mode> (CAN XL VCID generation mode - see below, no e/o mode)\n");
fprintf(stderr, " -p <timeout> (poll on -ENOBUFS to write frames with <timeout> ms)\n"); fprintf(stderr, " -p <timeout> (poll on -ENOBUFS to write frames with <timeout> ms)\n");
fprintf(stderr, " -P <priority> (set socket priority using SO_PRIORITY)\n");
fprintf(stderr, " -n <count> (terminate after <count> CAN frames - default infinite)\n"); fprintf(stderr, " -n <count> (terminate after <count> CAN frames - default infinite)\n");
fprintf(stderr, " -i (ignore -ENOBUFS return values on write() syscalls)\n"); fprintf(stderr, " -i (ignore -ENOBUFS return values on write() syscalls)\n");
fprintf(stderr, " -x (disable local loopback of generated CAN frames)\n"); fprintf(stderr, " -x (disable local loopback of generated CAN frames)\n");
@ -479,6 +480,7 @@ int main(int argc, char **argv)
uint64_t incdata = 0; uint64_t incdata = 0;
__u8 *data; /* base pointer for CC/FD or XL data */ __u8 *data; /* base pointer for CC/FD or XL data */
int incdlc = 0; int incdlc = 0;
int priority = -1;
unsigned long rnd; unsigned long rnd;
unsigned char fixdata[CANFD_MAX_DLEN]; unsigned char fixdata[CANFD_MAX_DLEN];
unsigned char rand_position[CANFD_MAX_DLEN] = { 0 }; unsigned char rand_position[CANFD_MAX_DLEN] = { 0 };
@ -512,7 +514,7 @@ int main(int argc, char **argv)
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
while ((opt = getopt_long(argc, argv, "g:atefbEXR8mI:L:D:F:S:A:V:p:n:ixc:vh?", long_options, NULL)) != -1) { while ((opt = getopt_long(argc, argv, "g:atefbEXR8mI:L:D:F:S:A:V:p:P:n:ixc:vh?", long_options, NULL)) != -1) {
switch (opt) { switch (opt) {
case 'g': case 'g':
gap = strtod(optarg, NULL); gap = strtod(optarg, NULL);
@ -682,6 +684,14 @@ int main(int argc, char **argv)
} }
break; break;
case 'P':
priority = atoi(optarg);
if (priority < 0) {
printf("socket priority has to be >= 0\n");
exit(1);
}
break;
case 'i': case 'i':
ignore_enobufs = true; ignore_enobufs = true;
break; break;
@ -750,6 +760,16 @@ int main(int argc, char **argv)
*/ */
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
/*
* user can use tc to configure the queuing discipline (e.g. mqprio),
* together with SO_PRIORITY option to specify the message send from
* this socket should go to which queue.
*/
if (priority >= 0 &&
setsockopt(s, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority))) {
printf("error setting SO_PRIORITY\n");
}
if (loopback_disable) { if (loopback_disable) {
const int loopback = 0; const int loopback = 0;