119 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
122 #include <linux/types.h>
123 #include <linux/module.h>
125 #include <linux/kernel.h>
127 #include <linux/sched.h>
128 #include <linux/slab.h>
131 #include <linux/string.h>
132 #include <linux/ptrace.h>
133 #include <linux/errno.h>
136 #include <linux/capability.h>
137 #include <linux/hrtimer.h>
141 #include <linux/list.h>
144 #include <linux/netdevice.h>
147 #include <linux/rtnetlink.h>
148 #include <linux/if_arp.h>
149 #include <linux/if_vlan.h>
150 #include <linux/in.h>
151 #include <linux/ip.h>
152 #include <linux/ipv6.h>
153 #include <linux/udp.h>
156 #include <linux/wait.h>
159 #include <linux/prefetch.h>
167 #include <asm/byteorder.h>
169 #include <linux/bitops.h>
171 #include <linux/timex.h>
174 #include <asm/div64.h>
176 #define VERSION "2.74"
177 #define IP_NAME_SZ 32
178 #define MAX_MPLS_LABELS 16
179 #define MPLS_STACK_BOTTOM htonl(0x00000100)
181 #define func_enter() pr_debug("entering %s\n", __func__);
184 #define F_IPSRC_RND (1<<0)
185 #define F_IPDST_RND (1<<1)
186 #define F_UDPSRC_RND (1<<2)
187 #define F_UDPDST_RND (1<<3)
188 #define F_MACSRC_RND (1<<4)
189 #define F_MACDST_RND (1<<5)
190 #define F_TXSIZE_RND (1<<6)
191 #define F_IPV6 (1<<7)
192 #define F_MPLS_RND (1<<8)
193 #define F_VID_RND (1<<9)
194 #define F_SVID_RND (1<<10)
195 #define F_FLOW_SEQ (1<<11)
196 #define F_IPSEC_ON (1<<12)
197 #define F_QUEUE_MAP_RND (1<<13)
198 #define F_QUEUE_MAP_CPU (1<<14)
199 #define F_NODE (1<<15)
202 #define T_STOP (1<<0)
204 #define T_REMDEVALL (1<<2)
205 #define T_REMDEV (1<<3)
208 #define if_lock(t) spin_lock(&(t->if_lock));
209 #define if_unlock(t) spin_unlock(&(t->if_lock));
212 #define PKTGEN_MAGIC 0xbe9be955
213 #define PG_PROC_DIR "pktgen"
214 #define PGCTRL "pgctrl"
217 #define MAX_CFLOWS 65536
219 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
220 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
232 #define F_INIT (1<<0)
422 static inline ktime_t ktime_now(
void)
427 return timespec_to_ktime(ts);
431 static inline int ktime_lt(
const ktime_t cmp1,
const ktime_t cmp2)
437 "Packet Generator for packet performance testing. "
441 static int pktgen_add_device(
struct pktgen_thread *
t,
const char *ifname);
443 const char *ifname,
bool exact);
444 static int pktgen_device_event(
struct notifier_block *,
unsigned long,
void *);
445 static void pktgen_run_all_threads(
void);
446 static void pktgen_reset_all_threads(
void);
447 static void pktgen_stop_all_threads_ifs(
void);
450 static void pktgen_clear_counters(
struct pktgen_dev *pkt_dev);
462 .notifier_call = pktgen_device_event,
470 static int pgctrl_show(
struct seq_file *seq,
void *
v)
477 size_t count, loff_t *ppos)
487 if (count >
sizeof(data))
488 count =
sizeof(
data);
496 if (!
strcmp(data,
"stop"))
497 pktgen_stop_all_threads_ifs();
499 else if (!
strcmp(data,
"start"))
500 pktgen_run_all_threads();
502 else if (!
strcmp(data,
"reset"))
503 pktgen_reset_all_threads();
514 static int pgctrl_open(
struct inode *
inode,
struct file *file)
516 return single_open(file, pgctrl_show, PDE(inode)->data);
524 .write = pgctrl_write,
528 static int pktgen_if_show(
struct seq_file *seq,
void *
v)
535 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
540 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
541 pkt_dev->
nfrags, (
unsigned long long) pkt_dev->
delay,
548 " queue_map_min: %u queue_map_max: %u\n",
558 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
559 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
566 " dst_min: %s dst_max: %s\n",
569 " src_min: %s src_max: %s\n",
576 is_zero_ether_addr(pkt_dev->
src_mac) ?
583 " udp_src_min: %d udp_src_max: %d"
584 " udp_dst_min: %d udp_dst_max: %d\n",
589 " src_mac_count: %d dst_mac_count: %d\n",
597 i == pkt_dev->
nr_labels-1 ?
"\n" :
", ");
600 if (pkt_dev->
vlan_id != 0xffff)
601 seq_printf(seq,
" vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
606 seq_printf(seq,
" svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
616 if (pkt_dev->
node >= 0)
683 "Current:\n pkts-sofar: %llu errors: %llu\n",
684 (
unsigned long long)pkt_dev->
sofar,
685 (
unsigned long long)pkt_dev->
errors);
688 " started: %lluus stopped: %lluus idle: %lluus\n",
689 (
unsigned long long) ktime_to_us(pkt_dev->
started_at),
690 (
unsigned long long) ktime_to_us(stopped),
691 (
unsigned long long) idle);
694 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
699 seq_printf(seq,
" cur_saddr: %pI6c cur_daddr: %pI6c\n",
703 seq_printf(seq,
" cur_saddr: %pI4 cur_daddr: %pI4\n",
706 seq_printf(seq,
" cur_udp_dst: %d cur_udp_src: %d\n",
722 static int hex32_arg(
const char __user *user_buffer,
unsigned long maxlen,
728 for (; i < maxlen; i++) {
743 static int count_trail_chars(
const char __user * user_buffer,
748 for (i = 0; i < maxlen; i++) {
768 static long num_arg(
const char __user *user_buffer,
unsigned long maxlen,
774 for (i = 0; i < maxlen; i++) {
778 if ((c >=
'0') && (c <=
'9')) {
787 static int strn_len(
const char __user * user_buffer,
unsigned int maxlen)
791 for (i = 0; i < maxlen; i++) {
821 len = hex32_arg(&buffer[i], 8, &tmp);
840 static ssize_t pktgen_if_write(
struct file *file,
841 const char __user * user_buffer,
size_t count,
847 char name[16], valstr[32];
848 unsigned long value = 0;
849 char *pg_result =
NULL;
853 pg_result = &(pkt_dev->
result[0]);
861 tmp = count_trail_chars(user_buffer, max);
870 len = strn_len(&user_buffer[i],
sizeof(name) - 1);
874 memset(name, 0,
sizeof(name));
880 len = count_trail_chars(&user_buffer[i], max);
887 size_t copy =
min_t(
size_t, count, 1023);
893 name, (
unsigned long)count, tb);
896 if (!
strcmp(name,
"min_pkt_size")) {
897 len = num_arg(&user_buffer[i], 10, &value);
902 if (value < 14 + 20 + 8)
908 sprintf(pg_result,
"OK: min_pkt_size=%u",
913 if (!
strcmp(name,
"max_pkt_size")) {
914 len = num_arg(&user_buffer[i], 10, &value);
919 if (value < 14 + 20 + 8)
925 sprintf(pg_result,
"OK: max_pkt_size=%u",
932 if (!
strcmp(name,
"pkt_size")) {
933 len = num_arg(&user_buffer[i], 10, &value);
938 if (value < 14 + 20 + 8)
949 if (!
strcmp(name,
"debug")) {
950 len = num_arg(&user_buffer[i], 10, &value);
960 if (!
strcmp(name,
"frags")) {
961 len = num_arg(&user_buffer[i], 10, &value);
970 if (!
strcmp(name,
"delay")) {
971 len = num_arg(&user_buffer[i], 10, &value);
976 if (value == 0x7FFFFFFF)
981 sprintf(pg_result,
"OK: delay=%llu",
982 (
unsigned long long) pkt_dev->
delay);
985 if (!
strcmp(name,
"rate")) {
986 len = num_arg(&user_buffer[i], 10, &value);
997 sprintf(pg_result,
"OK: rate=%lu", value);
1000 if (!
strcmp(name,
"ratep")) {
1001 len = num_arg(&user_buffer[i], 10, &value);
1012 sprintf(pg_result,
"OK: rate=%lu", value);
1015 if (!
strcmp(name,
"udp_src_min")) {
1016 len = num_arg(&user_buffer[i], 10, &value);
1028 if (!
strcmp(name,
"udp_dst_min")) {
1029 len = num_arg(&user_buffer[i], 10, &value);
1041 if (!
strcmp(name,
"udp_src_max")) {
1042 len = num_arg(&user_buffer[i], 10, &value);
1054 if (!
strcmp(name,
"udp_dst_max")) {
1055 len = num_arg(&user_buffer[i], 10, &value);
1067 if (!
strcmp(name,
"clone_skb")) {
1068 len = num_arg(&user_buffer[i], 10, &value);
1080 if (!
strcmp(name,
"count")) {
1081 len = num_arg(&user_buffer[i], 10, &value);
1087 sprintf(pg_result,
"OK: count=%llu",
1088 (
unsigned long long)pkt_dev->
count);
1091 if (!
strcmp(name,
"src_mac_count")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
1101 sprintf(pg_result,
"OK: src_mac_count=%d",
1105 if (!
strcmp(name,
"dst_mac_count")) {
1106 len = num_arg(&user_buffer[i], 10, &value);
1115 sprintf(pg_result,
"OK: dst_mac_count=%d",
1119 if (!
strcmp(name,
"node")) {
1120 len = num_arg(&user_buffer[i], 10, &value);
1129 if (pkt_dev->
page) {
1135 sprintf(pg_result,
"ERROR: node not possible");
1138 if (!
strcmp(name,
"flag")) {
1141 len = strn_len(&user_buffer[i],
sizeof(f) - 1);
1148 if (
strcmp(f,
"IPSRC_RND") == 0)
1151 else if (
strcmp(f,
"!IPSRC_RND") == 0)
1154 else if (
strcmp(f,
"TXSIZE_RND") == 0)
1157 else if (
strcmp(f,
"!TXSIZE_RND") == 0)
1160 else if (
strcmp(f,
"IPDST_RND") == 0)
1163 else if (
strcmp(f,
"!IPDST_RND") == 0)
1166 else if (
strcmp(f,
"UDPSRC_RND") == 0)
1169 else if (
strcmp(f,
"!UDPSRC_RND") == 0)
1172 else if (
strcmp(f,
"UDPDST_RND") == 0)
1175 else if (
strcmp(f,
"!UDPDST_RND") == 0)
1178 else if (
strcmp(f,
"MACSRC_RND") == 0)
1181 else if (
strcmp(f,
"!MACSRC_RND") == 0)
1184 else if (
strcmp(f,
"MACDST_RND") == 0)
1187 else if (
strcmp(f,
"!MACDST_RND") == 0)
1190 else if (
strcmp(f,
"MPLS_RND") == 0)
1193 else if (
strcmp(f,
"!MPLS_RND") == 0)
1196 else if (
strcmp(f,
"VID_RND") == 0)
1199 else if (
strcmp(f,
"!VID_RND") == 0)
1202 else if (
strcmp(f,
"SVID_RND") == 0)
1205 else if (
strcmp(f,
"!SVID_RND") == 0)
1208 else if (
strcmp(f,
"FLOW_SEQ") == 0)
1211 else if (
strcmp(f,
"QUEUE_MAP_RND") == 0)
1214 else if (
strcmp(f,
"!QUEUE_MAP_RND") == 0)
1217 else if (
strcmp(f,
"QUEUE_MAP_CPU") == 0)
1220 else if (
strcmp(f,
"!QUEUE_MAP_CPU") == 0)
1223 else if (
strcmp(f,
"IPSEC") == 0)
1227 else if (
strcmp(f,
"!IPV6") == 0)
1230 else if (
strcmp(f,
"NODE_ALLOC") == 0)
1233 else if (
strcmp(f,
"!NODE_ALLOC") == 0)
1238 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1240 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1241 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1247 if (!
strcmp(name,
"dst_min") || !
strcmp(name,
"dst")) {
1248 len = strn_len(&user_buffer[i],
sizeof(pkt_dev->
dst_min) - 1);
1267 if (!
strcmp(name,
"dst_max")) {
1268 len = strn_len(&user_buffer[i],
sizeof(pkt_dev->
dst_max) - 1);
1289 if (!
strcmp(name,
"dst6")) {
1290 len = strn_len(&user_buffer[i],
sizeof(buf) - 1);
1306 pr_debug(
"dst6 set to: %s\n", buf);
1309 sprintf(pg_result,
"OK: dst6=%s", buf);
1312 if (!
strcmp(name,
"dst6_min")) {
1313 len = strn_len(&user_buffer[i],
sizeof(buf) - 1);
1328 pr_debug(
"dst6_min set to: %s\n", buf);
1331 sprintf(pg_result,
"OK: dst6_min=%s", buf);
1334 if (!
strcmp(name,
"dst6_max")) {
1335 len = strn_len(&user_buffer[i],
sizeof(buf) - 1);
1349 pr_debug(
"dst6_max set to: %s\n", buf);
1352 sprintf(pg_result,
"OK: dst6_max=%s", buf);
1355 if (!
strcmp(name,
"src6")) {
1356 len = strn_len(&user_buffer[i],
sizeof(buf) - 1);
1372 pr_debug(
"src6 set to: %s\n", buf);
1375 sprintf(pg_result,
"OK: src6=%s", buf);
1378 if (!
strcmp(name,
"src_min")) {
1379 len = strn_len(&user_buffer[i],
sizeof(pkt_dev->
src_min) - 1);
1398 if (!
strcmp(name,
"src_max")) {
1399 len = strn_len(&user_buffer[i],
sizeof(pkt_dev->
src_max) - 1);
1418 if (!
strcmp(name,
"dst_mac")) {
1419 len = strn_len(&user_buffer[i],
sizeof(valstr) - 1);
1423 memset(valstr, 0,
sizeof(valstr));
1435 if (!
strcmp(name,
"src_mac")) {
1436 len = strn_len(&user_buffer[i],
sizeof(valstr) - 1);
1440 memset(valstr, 0,
sizeof(valstr));
1453 if (!
strcmp(name,
"clear_counters")) {
1454 pktgen_clear_counters(pkt_dev);
1455 sprintf(pg_result,
"OK: Clearing counters.\n");
1459 if (!
strcmp(name,
"flows")) {
1460 len = num_arg(&user_buffer[i], 10, &value);
1473 if (!
strcmp(name,
"flowlen")) {
1474 len = num_arg(&user_buffer[i], 10, &value);
1484 if (!
strcmp(name,
"queue_map_min")) {
1485 len = num_arg(&user_buffer[i], 5, &value);
1495 if (!
strcmp(name,
"queue_map_max")) {
1496 len = num_arg(&user_buffer[i], 5, &value);
1506 if (!
strcmp(name,
"mpls")) {
1507 unsigned int n,
cnt;
1509 len = get_labels(&user_buffer[i], pkt_dev);
1513 cnt =
sprintf(pg_result,
"OK: mpls=");
1514 for (n = 0; n < pkt_dev->
nr_labels; n++)
1515 cnt +=
sprintf(pg_result + cnt,
1524 pr_debug(
"VLAN/SVLAN auto turned off\n");
1529 if (!
strcmp(name,
"vlan_id")) {
1530 len = num_arg(&user_buffer[i], 4, &value);
1535 if (value <= 4095) {
1542 pr_debug(
"MPLS auto turned off\n");
1551 pr_debug(
"VLAN/SVLAN turned off\n");
1556 if (!
strcmp(name,
"vlan_p")) {
1557 len = num_arg(&user_buffer[i], 1, &value);
1562 if ((value <= 7) && (pkt_dev->
vlan_id != 0xffff)) {
1566 sprintf(pg_result,
"ERROR: vlan_p must be 0-7");
1571 if (!
strcmp(name,
"vlan_cfi")) {
1572 len = num_arg(&user_buffer[i], 1, &value);
1577 if ((value <= 1) && (pkt_dev->
vlan_id != 0xffff)) {
1581 sprintf(pg_result,
"ERROR: vlan_cfi must be 0-1");
1586 if (!
strcmp(name,
"svlan_id")) {
1587 len = num_arg(&user_buffer[i], 4, &value);
1592 if ((value <= 4095) && ((pkt_dev->
vlan_id != 0xffff))) {
1599 pr_debug(
"MPLS auto turned off\n");
1608 pr_debug(
"VLAN/SVLAN turned off\n");
1613 if (!
strcmp(name,
"svlan_p")) {
1614 len = num_arg(&user_buffer[i], 1, &value);
1619 if ((value <= 7) && (pkt_dev->
svlan_id != 0xffff)) {
1623 sprintf(pg_result,
"ERROR: svlan_p must be 0-7");
1628 if (!
strcmp(name,
"svlan_cfi")) {
1629 len = num_arg(&user_buffer[i], 1, &value);
1634 if ((value <= 1) && (pkt_dev->
svlan_id != 0xffff)) {
1638 sprintf(pg_result,
"ERROR: svlan_cfi must be 0-1");
1643 if (!
strcmp(name,
"tos")) {
1644 __u32 tmp_value = 0;
1645 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1651 pkt_dev->
tos = tmp_value;
1652 sprintf(pg_result,
"OK: tos=0x%02x", pkt_dev->
tos);
1654 sprintf(pg_result,
"ERROR: tos must be 00-ff");
1659 if (!
strcmp(name,
"traffic_class")) {
1660 __u32 tmp_value = 0;
1661 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1670 sprintf(pg_result,
"ERROR: traffic_class must be 00-ff");
1675 if (!
strcmp(name,
"skb_priority")) {
1676 len = num_arg(&user_buffer[i], 9, &value);
1682 sprintf(pg_result,
"OK: skb_priority=%i",
1691 static int pktgen_if_open(
struct inode *inode,
struct file *file)
1693 return single_open(file, pktgen_if_show, PDE(inode)->data);
1698 .open = pktgen_if_open,
1701 .write = pktgen_if_write,
1705 static int pktgen_thread_show(
struct seq_file *seq,
void *v)
1736 const char __user * user_buffer,
1737 size_t count, loff_t * offset)
1739 struct seq_file *seq = file->private_data;
1751 len = count_trail_chars(user_buffer, max);
1759 len = strn_len(&user_buffer[i],
sizeof(name) - 1);
1763 memset(name, 0,
sizeof(name));
1769 len = count_trail_chars(&user_buffer[i], max);
1776 pr_debug(
"t=%s, count=%lu\n", name, (
unsigned long)count);
1779 pr_err(
"ERROR: No thread\n");
1784 pg_result = &(t->
result[0]);
1786 if (!
strcmp(name,
"add_device")) {
1789 len = strn_len(&user_buffer[i],
sizeof(f) - 1);
1798 pktgen_add_device(t, f);
1801 sprintf(pg_result,
"OK: add_device=%s", f);
1805 if (!
strcmp(name,
"rem_device_all")) {
1811 sprintf(pg_result,
"OK: rem_device_all");
1815 if (!
strcmp(name,
"max_before_softirq")) {
1816 sprintf(pg_result,
"OK: Note! max_before_softirq is obsoleted -- Do not use");
1826 static int pktgen_thread_open(
struct inode *inode,
struct file *file)
1828 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1833 .open = pktgen_thread_open,
1836 .write = pktgen_thread_write,
1841 static struct pktgen_dev *__pktgen_NN_threads(
const char *ifname,
int remove)
1845 bool exact = (
remove ==
FIND);
1848 pkt_dev = pktgen_find_dev(t, ifname, exact);
1865 static void pktgen_mark_device(
const char *ifname)
1868 const int max_tries = 10, msec_per_try = 125;
1872 pr_debug(
"%s: marking %s for removal\n", __func__, ifname);
1876 pkt_dev = __pktgen_NN_threads(ifname,
REMOVE);
1877 if (pkt_dev ==
NULL)
1881 pr_debug(
"%s: waiting for %s to disappear....\n",
1886 if (++i >= max_tries) {
1887 pr_err(
"%s: timed out after waiting %d msec for device %s to be removed\n",
1888 __func__, msec_per_try * i, ifname);
1905 if (pkt_dev->
odev != dev)
1914 if (!pkt_dev->
entry)
1915 pr_err(
"can't move proc entry for '%s'\n",
1927 if (!net_eq(dev_net(dev), &
init_net) || pktgen_exiting)
1936 pktgen_change_name(dev);
1940 pktgen_mark_device(dev->
name);
1953 for (i = 0; ifname[
i] !=
'@'; i++) {
1967 static int pktgen_setup_dev(
struct pktgen_dev *pkt_dev,
const char *ifname)
1973 if (pkt_dev->
odev) {
1974 dev_put(pkt_dev->
odev);
1978 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
1980 pr_err(
"no such netdevice: \"%s\"\n", ifname);
1985 pr_err(
"not an ethernet device: \"%s\"\n", ifname);
1987 }
else if (!netif_running(odev)) {
1988 pr_err(
"device is down: \"%s\"\n", ifname);
1991 pkt_dev->
odev = odev;
2002 static void pktgen_setup_inject(
struct pktgen_dev *pkt_dev)
2006 if (!pkt_dev->
odev) {
2007 pr_err(
"ERROR: pkt_dev->odev == NULL in setup_inject\n");
2009 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2014 ntxq = pkt_dev->
odev->real_num_tx_queues;
2016 if (ntxq <= pkt_dev->queue_map_min) {
2017 pr_warning(
"WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2023 pr_warning(
"WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2031 if (is_zero_ether_addr(pkt_dev->
src_mac))
2038 int i,
set = 0, err = 1;
2045 + pkt_dev->pkt_overhead;
2063 idev = __in6_dev_get(pkt_dev->
odev);
2080 pr_err(
"ERROR: IPv6 link address not available\n");
2087 + pkt_dev->pkt_overhead;
2094 struct in_device *in_dev;
2097 in_dev = __in_dev_get_rcu(pkt_dev->
odev);
2099 if (in_dev->ifa_list) {
2101 in_dev->ifa_list->ifa_address;
2136 hrtimer_set_expires(&t.timer, spin_until);
2138 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2139 if (remaining <= 0) {
2144 start_time = ktime_now();
2145 if (remaining < 100000) {
2148 end_time = ktime_now();
2149 }
while (ktime_lt(end_time, spin_until));
2156 if (!hrtimer_active(&t.timer))
2163 }
while (t.task && pkt_dev->
running && !signal_pending(
current));
2165 end_time = ktime_now();
2168 pkt_dev->
idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2172 static inline void set_pkt_overhead(
struct pktgen_dev *pkt_dev)
2180 static inline int f_seen(
const struct pktgen_dev *pkt_dev,
int flow)
2185 static inline int f_pick(
struct pktgen_dev *pkt_dev)
2187 int flow = pkt_dev->
curfl;
2190 if (pkt_dev->
flows[flow].count >= pkt_dev->
lflow) {
2192 pkt_dev->
flows[flow].count = 0;
2193 pkt_dev->
flows[flow].flags = 0;
2194 pkt_dev->
curfl += 1;
2200 pkt_dev->
curfl = flow;
2202 if (pkt_dev->
flows[flow].count > pkt_dev->
lflow) {
2203 pkt_dev->
flows[flow].count = 0;
2204 pkt_dev->
flows[flow].flags = 0;
2208 return pkt_dev->
curfl;
2216 #define DUMMY_MARK 0
2217 static void get_ipsec_sa(
struct pktgen_dev *pkt_dev,
int flow)
2227 pkt_dev->ipsproto, 0);
2229 pkt_dev->
flows[flow].x =
x;
2230 set_pkt_overhead(pkt_dev);
2237 static void set_cur_queue_map(
struct pktgen_dev *pkt_dev)
2263 static void mod_cur_headers(
struct pktgen_dev *pkt_dev)
2270 flow = f_pick(pkt_dev);
2286 tmp = pkt_dev->
src_mac[5] + (mc & 0xFF);
2287 pkt_dev->
hh[11] =
tmp;
2288 tmp = (pkt_dev->
src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2289 pkt_dev->
hh[10] =
tmp;
2290 tmp = (pkt_dev->
src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2291 pkt_dev->
hh[9] =
tmp;
2292 tmp = (pkt_dev->
src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2293 pkt_dev->
hh[8] =
tmp;
2294 tmp = (pkt_dev->
src_mac[1] + (tmp >> 8));
2295 pkt_dev->
hh[7] =
tmp;
2314 tmp = pkt_dev->
dst_mac[5] + (mc & 0xFF);
2315 pkt_dev->
hh[5] =
tmp;
2316 tmp = (pkt_dev->
dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2317 pkt_dev->
hh[4] =
tmp;
2318 tmp = (pkt_dev->
dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2319 pkt_dev->
hh[3] =
tmp;
2320 tmp = (pkt_dev->
dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2321 pkt_dev->
hh[2] =
tmp;
2322 tmp = (pkt_dev->
dst_mac[1] + (tmp >> 8));
2323 pkt_dev->
hh[1] =
tmp;
2328 for (i = 0; i < pkt_dev->
nr_labels; i++)
2329 if (pkt_dev->
labels[i] & MPLS_STACK_BOTTOM)
2330 pkt_dev->
labels[
i] = MPLS_STACK_BOTTOM |
2375 t =
random32() % (imx - imn) + imn;
2386 if (pkt_dev->
cflows && f_seen(pkt_dev, flow)) {
2396 t =
random32() % (imx - imn) + imn;
2399 while (ipv4_is_loopback(s) ||
2400 ipv4_is_multicast(s) ||
2401 ipv4_is_lbcast(s) ||
2402 ipv4_is_zeronet(s) ||
2403 ipv4_is_local_multicast(s)) {
2404 t =
random32() % (imx - imn) + imn;
2419 pkt_dev->
flows[flow].cur_daddr =
2423 get_ipsec_sa(pkt_dev, flow);
2439 for (i = 0; i < 4; i++) {
2462 set_cur_queue_map(pkt_dev);
2464 pkt_dev->
flows[flow].count++;
2481 spin_lock(&x->
lock);
2486 err = x->
type->output(x, skb);
2493 spin_unlock(&x->
lock);
2497 static void free_SAs(
struct pktgen_dev *pkt_dev)
2502 for (i = 0; i < pkt_dev->
cflows; i++) {
2512 static int process_ipsec(
struct pktgen_dev *pkt_dev,
2521 nhead = x->
props.header_len - skb_headroom(skb);
2525 pr_err(
"Error expanding ipsec packet %d\n",
2533 ret = pktgen_output_ipsec(skb, pkt_dev);
2535 pr_err(
"Error creating ipsec packet %d\n", ret);
2541 *(
u16 *) ð[12] = protocol;
2554 for (i = 0; i < pkt_dev->
nr_labels; i++)
2555 *mpls++ = pkt_dev->
labels[i] & ~MPLS_STACK_BOTTOM;
2558 *mpls |= MPLS_STACK_BOTTOM;
2561 static inline __be16 build_tci(
unsigned int id,
unsigned int cfi,
2564 return htons(
id | (cfi << 12) | (prio << 13));
2567 static void pktgen_finalize_skb(
struct pktgen_dev *pkt_dev,
struct sk_buff *skb,
2574 datalen -=
sizeof(*pgh);
2576 if (pkt_dev->
nfrags <= 0) {
2579 int frags = pkt_dev->
nfrags;
2593 frag_len = (datalen/frags) < PAGE_SIZE ?
2595 while (datalen > 0) {
2600 node = pkt_dev->
node;
2605 get_page(pkt_dev->
page);
2606 skb_frag_set_page(skb, i, pkt_dev->
page);
2607 skb_shinfo(skb)->frags[
i].page_offset = 0;
2609 if (i == (frags - 1))
2610 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2611 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
2613 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2614 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2615 skb->
len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2616 skb->
data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2618 skb_shinfo(skb)->nr_frags =
i;
2652 if (pkt_dev->
vlan_id != 0xffff)
2658 mod_cur_headers(pkt_dev);
2666 if (pkt_dev->
node >= 0)
2667 node = pkt_dev->
node;
2689 skb_reserve(skb, datalen);
2695 mpls_push(mpls, pkt_dev);
2697 if (pkt_dev->
vlan_id != 0xffff) {
2700 *svlan_tci = build_tci(pkt_dev->
svlan_id,
2707 *vlan_tci = build_tci(pkt_dev->
vlan_id,
2717 skb_set_queue_mapping(skb, queue_map);
2721 udph = udp_hdr(skb);
2724 *(
__be16 *) & eth[12] = protocol;
2729 if (datalen < 0 || datalen <
sizeof(
struct pktgen_hdr))
2756 pktgen_finalize_skb(pkt_dev, skb, datalen);
2759 if (!process_ipsec(pkt_dev, skb, protocol))
2785 if (pkt_dev->
vlan_id != 0xffff)
2791 mod_cur_headers(pkt_dev);
2803 skb_reserve(skb, 16);
2809 mpls_push(mpls, pkt_dev);
2811 if (pkt_dev->
vlan_id != 0xffff) {
2814 *svlan_tci = build_tci(pkt_dev->
svlan_id,
2821 *vlan_tci = build_tci(pkt_dev->
vlan_id,
2831 skb_set_queue_mapping(skb, queue_map);
2833 iph = ipv6_hdr(skb);
2834 udph = udp_hdr(skb);
2837 *(
__be16 *) ð[12] = protocol;
2842 pkt_dev->pkt_overhead;
2844 if (datalen < 0 || datalen <
sizeof(
struct pktgen_hdr)) {
2875 pktgen_finalize_skb(pkt_dev, skb, datalen);
2884 return fill_packet_ipv6(odev, pkt_dev);
2886 return fill_packet_ipv4(odev, pkt_dev);
2889 static void pktgen_clear_counters(
struct pktgen_dev *pkt_dev)
2913 pktgen_setup_inject(pkt_dev);
2915 if (pkt_dev->
odev) {
2916 pktgen_clear_counters(pkt_dev);
2920 pkt_dev->
next_tx = ktime_now();
2922 set_pkt_overhead(pkt_dev);
2934 static void pktgen_stop_all_threads_ifs(
void)
2962 while (thread_is_running(t)) {
2978 static int pktgen_wait_all_threads_run(
void)
2986 sig = pktgen_wait_thread_run(t);
2999 static
void pktgen_run_all_threads(
void)
3015 pktgen_wait_all_threads_run();
3018 static
void pktgen_reset_all_threads(
void)
3034 pktgen_wait_all_threads_run();
3039 __u64 bps, mbps, pps;
3040 char *
p = pkt_dev->result;
3041 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3042 pkt_dev->started_at);
3043 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3045 p +=
sprintf(p,
"OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3046 (
unsigned long long)ktime_to_us(elapsed),
3047 (
unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3048 (
unsigned long long)ktime_to_us(idle),
3049 (
unsigned long long)pkt_dev->sofar,
3050 pkt_dev->cur_pkt_size, nr_frags);
3053 ktime_to_ns(elapsed));
3055 bps = pps * 8 * pkt_dev->cur_pkt_size;
3059 p +=
sprintf(p,
" %llupps %lluMb/sec (%llubps) errors: %llu",
3060 (
unsigned long long)pps,
3061 (
unsigned long long)mbps,
3062 (
unsigned long long)bps,
3063 (
unsigned long long)pkt_dev->errors);
3067 static int pktgen_stop_device(
struct pktgen_dev *pkt_dev)
3069 int nr_frags = pkt_dev->
skb ? skb_shinfo(pkt_dev->
skb)->nr_frags : -1;
3072 pr_warning(
"interface: %s is already stopped\n",
3082 show_results(pkt_dev, nr_frags);
3114 pktgen_stop_device(pkt_dev);
3142 pktgen_remove_device(t, cur);
3167 pktgen_remove_device(t, cur);
3181 static void pktgen_resched(
struct pktgen_dev *pkt_dev)
3183 ktime_t idle_start = ktime_now();
3185 pkt_dev->
idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3188 static void pktgen_wait_for_skb(
struct pktgen_dev *pkt_dev)
3190 ktime_t idle_start = ktime_now();
3197 pktgen_resched(pkt_dev);
3201 pkt_dev->
idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3204 static void pktgen_xmit(
struct pktgen_dev *pkt_dev)
3214 if (
unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3215 pktgen_stop_device(pkt_dev);
3233 pkt_dev->
skb = fill_packet(odev, pkt_dev);
3235 pr_err(
"ERROR: couldn't allocate skb in fill_packet\n");
3246 spin(pkt_dev, pkt_dev->
next_tx);
3248 queue_map = skb_get_queue_mapping(pkt_dev->
skb);
3249 txq = netdev_get_tx_queue(odev, queue_map);
3251 __netif_tx_lock_bh(txq);
3253 if (
unlikely(netif_xmit_frozen_or_stopped(txq))) {
3259 ret = (*xmit)(pkt_dev->
skb, odev);
3263 txq_trans_update(txq);
3287 __netif_tx_unlock_bh(txq);
3291 pktgen_wait_for_skb(pkt_dev);
3294 pktgen_stop_device(pkt_dev);
3302 static int pktgen_thread_worker(
void *
arg)
3321 pkt_dev = next_to_run(t);
3336 pktgen_xmit(pkt_dev);
3339 pktgen_resched(pkt_dev);
3354 if (t->
control & T_REMDEVALL) {
3355 pktgen_rem_all_ifs(t);
3360 pktgen_rem_one_if(t);
3369 pr_debug(
"%s stopping all device\n", t->
tsk->comm);
3372 pr_debug(
"%s removing all device\n", t->
tsk->comm);
3373 pktgen_rem_all_ifs(t);
3376 pktgen_rem_thread(t);
3389 const char *ifname,
bool exact)
3392 size_t len =
strlen(ifname);
3396 if (
strncmp(p->odevname, ifname, len) == 0) {
3398 if (exact || p->
odevname[len] !=
'@')
3406 pr_debug(
"find_dev(%s) returning %p\n", ifname, pkt_dev);
3422 pr_err(
"ERROR: already assigned to a thread\n");
3438 static int pktgen_add_device(
struct pktgen_thread *t,
const char *ifname)
3446 pkt_dev = __pktgen_NN_threads(ifname,
FIND);
3448 pr_err(
"ERROR: interface already used\n");
3466 pkt_dev->
delay = pg_delay_d;
3467 pkt_dev->
count = pg_count_d;
3481 err = pktgen_setup_dev(pkt_dev, ifname);
3488 &pktgen_if_fops, pkt_dev);
3489 if (!pkt_dev->
entry) {
3490 pr_err(
"cannot create %s/%s procfs entry\n",
3500 return add_dev_to_thread(t, pkt_dev);
3502 dev_put(pkt_dev->
odev);
3512 static int __init pktgen_create_thread(
int cpu)
3521 pr_err(
"ERROR: out of memory, can't create new thread\n");
3536 "kpktgend_%d", cpu);
3538 pr_err(
"kernel_thread() failed for cpu %d\n", t->
cpu);
3547 &pktgen_thread_fops, t);
3549 pr_err(
"cannot create %s/%s procfs entry\n",
3583 pr_debug(
"remove_device pkt_dev=%p\n", pkt_dev);
3586 pr_warning(
"WARNING: trying to remove a running interface, stopping it now\n");
3587 pktgen_stop_device(pkt_dev);
3592 if (pkt_dev->
odev) {
3593 dev_put(pkt_dev->
odev);
3599 _rem_dev_from_if_list(t, pkt_dev);
3614 static int __init pg_init(
void)
3626 pe = proc_create(
PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3628 pr_err(
"ERROR: cannot create %s procfs entry\n",
PGCTRL);
3638 err = pktgen_create_thread(cpu);
3640 pr_warning(
"WARNING: Cannot create thread for cpu %d (%d)\n",
3644 if (list_empty(&pktgen_threads)) {
3645 pr_err(
"ERROR: Initialization failed for all threads\n");
3660 static void __exit pg_cleanup(
void)
3667 pktgen_exiting =
true;
3670 list_splice_init(&pktgen_threads, &
list);
3698 MODULE_PARM_DESC(pg_delay_d,
"Default delay between packets (nanoseconds)");
3700 MODULE_PARM_DESC(pg_clone_skb_d,
"Default number of copies of the same packet");