#define VERSION "2.75" #define IP_NAME_SZ 32 #define MAX_MPLS_LABELS 16/* This is the max label stack depth */ #define MPLS_STACK_BOTTOM htonl(0x00000100) /* Max number of internet mix entries that can be specified in imix_weights. */ #define MAX_IMIX_ENTRIES 20 #define IMIX_PRECISION 100/* Precision of IMIX distribution */
/* flow flag bits */ #define F_INIT (1<<0) /* flow has been initialized */
struct pktgen_dev { /* *Trytokeepfrequent/infrequentusedvars.separated.
*/ struct proc_dir_entry *entry; /* proc file */ struct pktgen_thread *pg_thread;/* the owner */ struct list_head list; /* chaining in the thread's run-queue */ struct rcu_head rcu; /* freed by RCU */
int running; /* if false, the test will stop */
/* If min != max, then we will either do a linear iteration, or *wewilldoarandomselectionfromwithintherange.
*/
__u32 flags; int xmit_mode; int min_pkt_size; int max_pkt_size; int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */ int nfrags; int removal_mark; /* non-zero => the device is marked for *removalbyworkerthread
*/
struct page *page;
u64 delay; /* nano-seconds */
__u64 count; /* Default No packets to send */
__u64 sofar; /* How many pkts we've sent so far */
__u64 tx_bytes; /* How many bytes we've transmitted */
__u64 errors; /* Errors when trying to transmit, */
/* runtime counters relating to clone_skb */
__u32 clone_count; int last_ok; /* Was last skb sent? *Orafailedtransmitofsomesort? *Thiswillkeepsequencenumbersinorder
*/
ktime_t next_tx;
ktime_t started_at;
ktime_t stopped_at;
u64 idle_acc; /* nano-seconds */
/* If we're doing ranges, random or incremental, then this *definesthemin/maxforthoseranges.
*/
__be32 saddr_min; /* inclusive, source IP address */
__be32 saddr_max; /* exclusive, source IP address */
__be32 daddr_min; /* inclusive, dest IP address */
__be32 daddr_max; /* exclusive, dest IP address */
__u16 udp_src_min; /* inclusive, source UDP port */
__u16 udp_src_max; /* exclusive, source UDP port */
__u16 udp_dst_min; /* inclusive, dest UDP port */
__u16 udp_dst_max; /* exclusive, dest UDP port */
/* DSCP + ECN */
__u8 tos; /* six MSB of (former) IPv4 TOS *arefordscpcodepoint
*/
__u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 *(seeRFC3260,sec.4)
*/
/* IMIX */ unsignedint n_imix_entries; struct imix_pkt imix_entries[MAX_IMIX_ENTRIES]; /* Maps 0-IMIX_PRECISION range to imix_entry based on probability*/
__u8 imix_distribution[IMIX_PRECISION];
/* MPLS */ unsignedint nr_labels; /* Depth of stack, 0 = no MPLS */
__be32 labels[MAX_MPLS_LABELS];
/* VLAN/SVLAN (802.1Q/Q-in-Q) */
__u8 vlan_p;
__u8 vlan_cfi;
__u16 vlan_id; /* 0xffff means no vlan tag */
__u8 svlan_p;
__u8 svlan_cfi;
__u16 svlan_id; /* 0xffff means no svlan tag */
__u32 src_mac_count; /* How many MACs to iterate through */
__u32 dst_mac_count; /* How many MACs to iterate through */
__u8 hh[14]; /* = { *0x00,0x80,0xC8,0x79,0xB3,0xCB, * *WefillinSRCaddresslater *0x00,0x00,0x00,0x00,0x00,0x00, *0x08,0x00 *};
*/
__u16 pad; /* pad out the hh struct to an even 16 bytes */
struct sk_buff *skb; /* skb we are to transmit next, used for when we *aretransmittingthesameonemultipletimes
*/ struct net_device *odev; /* The out-going device. *Notethatthedeviceshouldhaveit's *pg_infopointerpointingbacktothis *device. *Setwhentheuserspecifiestheout-going *devicename(notwhentheinjectis *startedasitusedtodo.)
*/
netdevice_tracker dev_tracker; char odevname[32]; struct flow_state *flows; unsignedint cflows; /* Concurrent flows (config) */ unsignedint lflow; /* Flow length (config) */ unsignedint nflows; /* accumulated flows (stats) */ unsignedint curfl; /* current sequenced flow (state)*/
u16 queue_map_min;
u16 queue_map_max;
__u32 skb_priority; /* skb priority field */ unsignedint burst; /* number of duplicated packets to burst */ int node; /* Memory node */
if (pkt_dev->n_imix_entries >= MAX_IMIX_ENTRIES) return -E2BIG;
if (i >= maxlen) return -EINVAL;
max = min(10, maxlen - i);
len = num_arg(&buffer[i], max, &size); if (len < 0) return len;
i += len; if (i >= maxlen) return -EINVAL; if (get_user(c, &buffer[i])) return -EFAULT; /* Check for comma between size_i and weight_i */ if (c != ',') return -EINVAL;
i++; if (i >= maxlen) return -EINVAL;
if (size < 14 + 20 + 8)
size = 14 + 20 + 8;
max = min(10, maxlen - i);
len = num_arg(&buffer[i], max, &weight); if (len < 0) return len; if (weight <= 0) return -EINVAL;
max = min(8, maxlen - i);
len = hex32_arg(&buffer[i], max, &tmp); if (len < 0) return len;
/* return empty list in case of invalid input or zero value */ if (len == 0 || tmp == 0) return maxlen;
pkt_dev->labels[n] = htonl(tmp); if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
pkt_dev->flags |= F_MPLS_RND;
i += len;
n++; if (i >= maxlen) break; if (get_user(c, &buffer[i])) return -EFAULT;
i++;
} while (c == ',');
sprintf(pg_result, "OK: delay=%llu",
(unsignedlonglong) pkt_dev->delay); return count;
} if (!strcmp(name, "rate")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (!value) return -EINVAL;
pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value; if (debug)
pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
sprintf(pg_result, "OK: rate=%lu", value); return count;
} if (!strcmp(name, "ratep")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (!value) return -EINVAL;
pkt_dev->delay = NSEC_PER_SEC/value; if (debug)
pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
sprintf(pg_result, "OK: rate=%lu", value); return count;
} if (!strcmp(name, "udp_src_min")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (value != pkt_dev->udp_src_min) {
pkt_dev->udp_src_min = value;
pkt_dev->cur_udp_src = value;
}
sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); return count;
} if (!strcmp(name, "udp_dst_min")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (value != pkt_dev->udp_dst_min) {
pkt_dev->udp_dst_min = value;
pkt_dev->cur_udp_dst = value;
}
sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); return count;
} if (!strcmp(name, "udp_src_max")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (value != pkt_dev->udp_src_max) {
pkt_dev->udp_src_max = value;
pkt_dev->cur_udp_src = value;
}
sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); return count;
} if (!strcmp(name, "udp_dst_max")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (value != pkt_dev->udp_dst_max) {
pkt_dev->udp_dst_max = value;
pkt_dev->cur_udp_dst = value;
}
sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); return count;
} if (!strcmp(name, "clone_skb")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len; /* clone_skb is not supported for netif_receive xmit_mode and *IMIXmode.
*/ if ((value > 0) &&
((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING))) return -EOPNOTSUPP; if (value > 0 && (pkt_dev->n_imix_entries > 0 ||
!(pkt_dev->flags & F_SHARED))) return -EINVAL;
pkt_dev->clone_skb = value;
sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); return count;
} if (!strcmp(name, "count")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
pkt_dev->count = value;
sprintf(pg_result, "OK: count=%llu",
(unsignedlonglong)pkt_dev->count); return count;
} if (!strcmp(name, "src_mac_count")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (pkt_dev->src_mac_count != value) {
pkt_dev->src_mac_count = value;
pkt_dev->cur_src_mac_offset = 0;
}
sprintf(pg_result, "OK: src_mac_count=%d",
pkt_dev->src_mac_count); return count;
} if (!strcmp(name, "dst_mac_count")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
if (pkt_dev->dst_mac_count != value) {
pkt_dev->dst_mac_count = value;
pkt_dev->cur_dst_mac_offset = 0;
}
sprintf(pg_result, "OK: dst_mac_count=%d",
pkt_dev->dst_mac_count); return count;
} if (!strcmp(name, "burst")) {
max = min(10, count - i);
len = num_arg(&user_buffer[i], max, &value); if (len < 0) return len;
/* Unknown flag */
end = pkt_dev->result + sizeof(pkt_dev->result);
pg_result += sprintf(pg_result, "Flag -:%s:- unknown\n" "Available flags, (prepend ! to un-set flag):\n", f);
for (int n = 0; n < NR_PKT_FLAGS && pg_result < end; n++) { if (!IS_ENABLED(CONFIG_XFRM) && n == IPSEC_SHIFT) continue;
pg_result += snprintf(pg_result, end - pg_result, "%s, ", pkt_flag_names[n]);
} if (!WARN_ON_ONCE(pg_result >= end)) { /* Remove the comma and whitespace at the end */
*(pg_result - 2) = '\0';
}
return count;
} if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
max = min(sizeof(pkt_dev->dst_min) - 1, count - i);
len = strn_len(&user_buffer[i], max); if (len < 0) return len;
if (copy_from_user(buf, &user_buffer[i], len)) return -EFAULT;
buf[len] = 0; if (strcmp(buf, pkt_dev->dst_min) != 0) {
strscpy_pad(pkt_dev->dst_min, buf);
pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
pkt_dev->cur_daddr = pkt_dev->daddr_min;
} if (debug)
pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); return count;
} if (!strcmp(name, "dst_max")) {
max = min(sizeof(pkt_dev->dst_max) - 1, count - i);
len = strn_len(&user_buffer[i], max); if (len < 0) return len;
if (copy_from_user(buf, &user_buffer[i], len)) return -EFAULT;
buf[len] = 0; if (strcmp(buf, pkt_dev->dst_max) != 0) {
strscpy_pad(pkt_dev->dst_max, buf);
pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
pkt_dev->cur_daddr = pkt_dev->daddr_max;
} if (debug)
pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); return count;
} if (!strcmp(name, "dst6")) {
max = min(sizeof(buf) - 1, count - i);
len = strn_len(&user_buffer[i], max); if (len < 0) return len;
pkt_dev->flags |= F_IPV6;
if (copy_from_user(buf, &user_buffer[i], len)) return -EFAULT;
buf[len] = 0;
/* Read pkt_dev from the interface and set up internal pktgen_dev *structuretohavetherightinformationtocreate/sendpackets
*/ staticvoid pktgen_setup_inject(struct pktgen_dev *pkt_dev)
{ int ntxq;
if (!pkt_dev->odev) {
pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
sprintf(pkt_dev->result, "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); return;
}
/* make sure that we don't pick a non-existing transmit queue */
ntxq = pkt_dev->odev->real_num_tx_queues;
if (ntxq <= pkt_dev->queue_map_min) {
pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
pkt_dev->odevname);
pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
} if (pkt_dev->queue_map_max >= ntxq) {
pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
pkt_dev->odevname);
pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
}
/* Default to the interface's mac if not explicitly set. */
if (is_zero_ether_addr(pkt_dev->src_mac))
ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
/* Set up Dest MAC */
ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
if (pkt_dev->flags & F_IPV6) { int i, set = 0, err = 1; struct inet6_dev *idev;
remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer)); if (remaining <= 0) goto out;
start_time = ktime_get(); if (remaining < 100000) { /* for small delays (<100us), just loop until limit is reached */ do {
end_time = ktime_get();
} while (ktime_compare(end_time, spin_until) < 0);
} else { do {
set_current_state(TASK_INTERRUPTIBLE);
hrtimer_sleeper_start_expires(&t, HRTIMER_MODE_ABS);
/* If there was already an IPSEC SA, we keep it as is, else *wegolookforit...
*/ #define DUMMY_MARK 0 staticvoid get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
{ #ifdef CONFIG_XFRM struct xfrm_state *x = pkt_dev->flows[flow].x; struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
if (!x) {
if (pkt_dev->spi) { /* We need as quick as possible to find the right SA *Searchingwithminimumcriteriatoachieve,this.
*/
x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
} else { /* slow path: we don't already have xfrm_state */
x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
(xfrm_address_t *)&pkt_dev->cur_daddr,
(xfrm_address_t *)&pkt_dev->cur_saddr,
AF_INET,
pkt_dev->ipsmode,
pkt_dev->ipsproto, 0);
} if (x) {
pkt_dev->flows[flow].x = x;
set_pkt_overhead(pkt_dev);
pkt_dev->pkt_overhead += x->props.header_len;
}
if (pkt_dev->flags & F_QUEUE_MAP_RND) {
t = get_random_u32_inclusive(pkt_dev->queue_map_min,
pkt_dev->queue_map_max);
} else {
t = pkt_dev->cur_queue_map + 1; if (t > pkt_dev->queue_map_max)
t = pkt_dev->queue_map_min;
}
pkt_dev->cur_queue_map = t;
}
pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
}
/* Increment/randomize headers according to flags and current values *forIPsrc/dest,UDPsrc/dstport,MAC-Addrsrc/dst
*/ staticvoid mod_cur_headers(struct pktgen_dev *pkt_dev)
{
__u32 imn;
__u32 imx; int flow = 0;
if (pkt_dev->cflows)
flow = f_pick(pkt_dev);
/* Deal with source MAC */ if (pkt_dev->src_mac_count > 1) {
__u32 mc;
__u32 tmp;
if (pkt_dev->flags & F_MACSRC_RND)
mc = get_random_u32_below(pkt_dev->src_mac_count); else {
mc = pkt_dev->cur_src_mac_offset++; if (pkt_dev->cur_src_mac_offset >=
pkt_dev->src_mac_count)
pkt_dev->cur_src_mac_offset = 0;
}
staticvoid fill_imix_distribution(struct pktgen_dev *pkt_dev)
{ int cumulative_probabilites[MAX_IMIX_ENTRIES]; int j = 0;
__u64 cumulative_prob = 0;
__u64 total_weight = 0; int i = 0;
for (i = 0; i < pkt_dev->n_imix_entries; i++)
total_weight += pkt_dev->imix_entries[i].weight;
/* Fill cumulative_probabilites with sum of normalized probabilities */ for (i = 0; i < pkt_dev->n_imix_entries - 1; i++) {
cumulative_prob += div64_u64(pkt_dev->imix_entries[i].weight *
IMIX_PRECISION,
total_weight);
cumulative_probabilites[i] = cumulative_prob;
}
cumulative_probabilites[pkt_dev->n_imix_entries - 1] = 100;
for (i = 0; i < IMIX_PRECISION; i++) { if (i == cumulative_probabilites[j])
j++;
pkt_dev->imix_distribution[i] = j;
}
}
if (!x) return0; /* XXX: we dont support tunnel mode for now until *weresolvethedstissue
*/ if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0)) return0;
/* But when user specify an valid SPI, transformation *supportsbothtransport/tunnelmode+ESP/AHtype.
*/ if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
skb->_skb_refdst = (unsignedlong)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
/* Stamp the time, and sequence number, *convertthemtonetworkbyteorder
*/
pgh->pgh_magic = htonl(PKTGEN_MAGIC);
pgh->seq_num = htonl(pkt_dev->seq_num);
/* the caller pre-fetches from skb->data and reserves for the mac hdr */ if (likely(skb))
skb_reserve(skb, extralen - 16);
return skb;
}
staticstruct sk_buff *fill_packet_ipv4(struct net_device *odev, struct pktgen_dev *pkt_dev)
{ struct sk_buff *skb = NULL;
__u8 *eth; struct udphdr *udph; int datalen, iplen; struct iphdr *iph;
__be16 protocol = htons(ETH_P_IP);
__be32 *mpls;
__be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
__be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
__be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
u16 queue_map;
if (pkt_dev->nr_labels)
protocol = htons(ETH_P_MPLS_UC);
if (pkt_dev->vlan_id != 0xffff)
protocol = htons(ETH_P_8021Q);
/* Update any of the values, used when we're incrementing various *fields.
*/
mod_cur_headers(pkt_dev);
queue_map = pkt_dev->cur_queue_map;
skb = pktgen_alloc_skb(odev, pkt_dev); if (!skb) {
sprintf(pkt_dev->result, "No memory"); return NULL;
}
prefetchw(skb->data);
skb_reserve(skb, 16);
/* Reserve for ethernet and IP header */
eth = skb_push(skb, 14);
mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32)); if (pkt_dev->nr_labels)
mpls_push(mpls, pkt_dev);
if (udph->check == 0)
udph->check = CSUM_MANGLED_0;
}
#ifdef CONFIG_XFRM if (!process_ipsec(pkt_dev, skb, protocol)) return NULL; #endif
return skb;
}
staticstruct sk_buff *fill_packet_ipv6(struct net_device *odev, struct pktgen_dev *pkt_dev)
{ struct sk_buff *skb = NULL;
__u8 *eth; struct udphdr *udph; int datalen, udplen; struct ipv6hdr *iph;
__be16 protocol = htons(ETH_P_IPV6);
__be32 *mpls;
__be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
__be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
__be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
__be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
u16 queue_map;
if (pkt_dev->nr_labels)
protocol = htons(ETH_P_MPLS_UC);
if (pkt_dev->vlan_id != 0xffff)
protocol = htons(ETH_P_8021Q);
/* Update any of the values, used when we're incrementing various *fields.
*/
mod_cur_headers(pkt_dev);
queue_map = pkt_dev->cur_queue_map;
skb = pktgen_alloc_skb(odev, pkt_dev); if (!skb) {
sprintf(pkt_dev->result, "No memory"); return NULL;
}
prefetchw(skb->data);
skb_reserve(skb, 16);
/* Reserve for ethernet and IP header */
eth = skb_push(skb, 14);
mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32)); if (pkt_dev->nr_labels)
mpls_push(mpls, pkt_dev);
staticint pktgen_wait_thread_run(struct pktgen_thread *t)
{ while (thread_is_running(t)) {
/* note: 't' will still be around even after the unlock/lock *cyclebecausepktgen_threadthreadsareonlyclearedat *netexit
*/
mutex_unlock(&pktgen_thread_lock);
msleep_interruptible(100);
mutex_lock(&pktgen_thread_lock);
if (signal_pending(current)) goto signal;
} return1;
signal: return0;
}
staticint pktgen_wait_all_threads_run(struct pktgen_net *pn)
{ struct pktgen_thread *t; int sig = 1;
/* prevent from racing with rmmod */ if (!try_module_get(THIS_MODULE)) return sig;
mutex_lock(&pktgen_thread_lock);
list_for_each_entry(t, &pn->pktgen_threads, th_list) {
sig = pktgen_wait_thread_run(t); if (sig == 0) break;
}
if (sig == 0)
list_for_each_entry(t, &pn->pktgen_threads, th_list)
t->control |= (T_STOP);
/* If 'skb_shared' is false, the read of possible *newvalues(ifany)for'burst'and'clone_skb'willbeskippedto *preventsomeconcurrentchangesfromslippingin.Andthestabilized *configwillbereadinduringthenextrunofpktgen_xmit.
*/ if (skb_shared) {
burst = READ_ONCE(pkt_dev->burst);
clone_skb = READ_ONCE(pkt_dev->clone_skb);
}
/* If device is offline, then don't send */ if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
pktgen_stop_device(pkt_dev); return;
}
/* This is max DELAY, this has special meaning of *"nevertransmit"
*/ if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX); return;
}
/* If no skb or clone count exhausted then get new one */ if (!pkt_dev->skb || (pkt_dev->last_ok &&
++pkt_dev->clone_count >= clone_skb)) { /* build a new pkt */
kfree_skb(pkt_dev->skb);
pkt_dev->skb = fill_packet(odev, pkt_dev); if (pkt_dev->skb == NULL) {
pr_err("ERROR: couldn't allocate skb in fill_packet\n");
schedule();
pkt_dev->clone_count--; /* back out increment, OOM */ return;
}
pkt_dev->last_pkt_size = pkt_dev->skb->len;
pkt_dev->clone_count = 0; /* reset counter */
}
if (pkt_dev->delay && pkt_dev->last_ok)
spin(pkt_dev, pkt_dev->next_tx);
if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
skb = pkt_dev->skb;
skb->protocol = eth_type_trans(skb, skb->dev); if (skb_shared)
refcount_add(burst, &skb->users);
local_bh_disable(); do {
ret = netif_receive_skb(skb); if (ret == NET_RX_DROP)
pkt_dev->errors++;
pkt_dev->sofar++;
pkt_dev->seq_num++; if (unlikely(!skb_shared)) {
pkt_dev->skb = NULL; break;
} if (refcount_read(&skb->users) != burst) { /* skb was queued by rps/rfs or taps, *socannotreusethisskb
*/
WARN_ON(refcount_sub_and_test(burst - 1, &skb->users)); /* get out of the loop and wait *untilskbisconsumed
*/ break;
} /* skb was 'freed' by stack, so clean few *bitsandreuseit
*/
skb_reset_redirect(skb);
} while (--burst > 0); goto out; /* Skips xmit_mode M_START_XMIT */
} elseif (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
local_bh_disable(); if (skb_shared)
refcount_inc(&pkt_dev->skb->users);
ret = dev_queue_xmit(pkt_dev->skb);
if (!skb_shared && dev_xmit_complete(ret))
pkt_dev->skb = NULL;
switch (ret) { case NET_XMIT_SUCCESS:
pkt_dev->sofar++;
pkt_dev->seq_num++;
pkt_dev->tx_bytes += pkt_dev->last_pkt_size; break; case NET_XMIT_DROP: case NET_XMIT_CN: /* These are all valid return codes for a qdisc but *indicatepacketsarebeingdroppedorwilllikely *bedroppedsoon.
*/ case NETDEV_TX_BUSY: /* qdisc may call dev_hard_start_xmit directly in cases *wherenoqueuesexiste.g.loopbackdevice,virtual *devices,etc.Inthiscaseweneedtohandle *NETDEV_TX_codes.
*/ default:
pkt_dev->errors++;
net_info_ratelimited("%s xmit error: %d\n",
pkt_dev->odevname, ret); break;
} goto out;
}
txq = skb_get_tx_queue(odev, pkt_dev->skb);
local_bh_disable();
HARD_TX_LOCK(odev, txq, smp_processor_id());
if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
pkt_dev->last_ok = 0; goto unlock;
} if (skb_shared)
refcount_add(burst, &pkt_dev->skb->users);
xmit_more:
ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
if (!skb_shared && dev_xmit_complete(ret))
pkt_dev->skb = NULL;
switch (ret) { case NETDEV_TX_OK:
pkt_dev->last_ok = 1;
pkt_dev->sofar++;
pkt_dev->seq_num++;
pkt_dev->tx_bytes += pkt_dev->last_pkt_size; if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq)) goto xmit_more; break; case NET_XMIT_DROP: case NET_XMIT_CN: /* skb has been consumed */
pkt_dev->errors++; break; default: /* Drivers are not supposed to return other values! */
net_info_ratelimited("%s xmit error: %d\n",
pkt_dev->odevname, ret);
pkt_dev->errors++;
fallthrough; case NETDEV_TX_BUSY: /* Retry it next time */ if (skb_shared)
refcount_dec(&pkt_dev->skb->users);
pkt_dev->last_ok = 0;
} if (unlikely(burst))
WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
unlock:
HARD_TX_UNLOCK(odev, txq);
out:
local_bh_enable();
/* If pkt_dev->count is zero, then run forever */ if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { if (pkt_dev->skb)
pktgen_wait_for_skb(pkt_dev);
/* Done with this */
pktgen_stop_device(pkt_dev);
}
}
/* *Mainloopofthethreadgoeshere
*/
staticint pktgen_thread_worker(void *arg)
{ struct pktgen_thread *t = arg; struct pktgen_dev *pkt_dev = NULL; int cpu = t->cpu;
/* This function cannot be called concurrently, as its called *underpktgen_thread_lockmutex,butitcanrunfrom *userspaceonanotherCPUthanthekthread.Theif_lock() *isusedheretosyncwithconcurrentinstancesof *_rem_dev_from_if_list()invokedviakthread,whichisalso *updatingtheif_list
*/
if_lock(t);
if (pkt_dev->pg_thread) {
pr_err("ERROR: already assigned to a thread\n");
rv = -EBUSY; goto out;
}
t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
cpu_to_node(cpu)); if (!t) {
pr_err("ERROR: out of memory, can't create new thread\n"); return -ENOMEM;
}
p = kthread_create_on_cpu(pktgen_thread_worker, t, cpu, "kpktgend_%d"); if (IS_ERR(p)) {
pr_err("kthread_create_on_node() failed for cpu %d\n", t->cpu);
list_del(&t->th_list);
kfree(t); return PTR_ERR(p);
}
pr_info("%s", version);
ret = register_pernet_subsys(&pg_net_ops); if (ret) return ret;
ret = register_netdevice_notifier(&pktgen_notifier_block); if (ret)
unregister_pernet_subsys(&pg_net_ops);
return ret;
}
staticvoid __exit pg_cleanup(void)
{
unregister_netdevice_notifier(&pktgen_notifier_block);
unregister_pernet_subsys(&pg_net_ops); /* Don't need rcu_barrier() due to use of kfree_rcu() */
}
module_init(pg_init);
module_exit(pg_cleanup);
MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
MODULE_DESCRIPTION("Packet Generator tool");
MODULE_LICENSE("GPL");
MODULE_VERSION(VERSION);
module_param(pg_count_d, int, 0);
MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
module_param(pg_delay_d, int, 0);
MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
module_param(pg_clone_skb_d, int, 0);
MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.150Bemerkung:
(vorverarbeitet am 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.