Skip to content

Commit 39df232

Browse files
Stephen HemmingerDavid S. Miller
authored andcommitted
[PKTGEN]: fix device name handling
Since devices can change name and other wierdness, don't hold onto a copy of device name, instead use pointer to output device. Fix a couple of leaks in error handling path as well. Signed-off-by: Stephen Hemminger <shemminger@linux-foundation.org> Signed-off-by: Robert Olsson <robert.olsson@its.uu.se> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d5f1ce9 commit 39df232

1 file changed

Lines changed: 70 additions & 67 deletions

File tree

‎net/core/pktgen.c‎

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,11 @@ struct flow_state {
211211
};
212212

213213
struct pktgen_dev {
214-
215214
/*
216215
* Try to keep frequent/infrequent used vars. separated.
217216
*/
218-
219-
char ifname[IFNAMSIZ];
220-
char result[512];
221-
222-
struct pktgen_thread *pg_thread; /* the owner */
217+
struct proc_dir_entry *entry; /* proc file */
218+
struct pktgen_thread *pg_thread;/* the owner */
223219
struct list_head list; /* Used for chaining in the thread's run-queue */
224220

225221
int running; /* if this changes to false, the test will stop */
@@ -346,6 +342,8 @@ struct pktgen_dev {
346342
unsigned cflows; /* Concurrent flows (config) */
347343
unsigned lflow; /* Flow length (config) */
348344
unsigned nflows; /* accumulated flows (stats) */
345+
346+
char result[512];
349347
};
350348

351349
struct pktgen_hdr {
@@ -498,7 +496,7 @@ static void pktgen_stop_all_threads_ifs(void);
498496
static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
499497
static void pktgen_stop(struct pktgen_thread *t);
500498
static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
501-
static int pktgen_mark_device(const char *ifname);
499+
502500
static unsigned int scan_ip6(const char *s, char ip[16]);
503501
static unsigned int fmt_ip6(char *s, const char ip[16]);
504502

@@ -592,7 +590,7 @@ static int pktgen_if_show(struct seq_file *seq, void *v)
592590
" frags: %d delay: %u clone_skb: %d ifname: %s\n",
593591
pkt_dev->nfrags,
594592
1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
595-
pkt_dev->clone_skb, pkt_dev->ifname);
593+
pkt_dev->clone_skb, pkt_dev->odev->name);
596594

597595
seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
598596
pkt_dev->lflow);
@@ -1683,13 +1681,13 @@ static int pktgen_thread_show(struct seq_file *seq, void *v)
16831681
if_lock(t);
16841682
list_for_each_entry(pkt_dev, &t->if_list, list)
16851683
if (pkt_dev->running)
1686-
seq_printf(seq, "%s ", pkt_dev->ifname);
1684+
seq_printf(seq, "%s ", pkt_dev->odev->name);
16871685

16881686
seq_printf(seq, "\nStopped: ");
16891687

16901688
list_for_each_entry(pkt_dev, &t->if_list, list)
16911689
if (!pkt_dev->running)
1692-
seq_printf(seq, "%s ", pkt_dev->ifname);
1690+
seq_printf(seq, "%s ", pkt_dev->odev->name);
16931691

16941692
if (t->result[0])
16951693
seq_printf(seq, "\nResult: %s\n", t->result);
@@ -1835,12 +1833,11 @@ static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
18351833
/*
18361834
* mark a device for removal
18371835
*/
1838-
static int pktgen_mark_device(const char *ifname)
1836+
static void pktgen_mark_device(const char *ifname)
18391837
{
18401838
struct pktgen_dev *pkt_dev = NULL;
18411839
const int max_tries = 10, msec_per_try = 125;
18421840
int i = 0;
1843-
int ret = 0;
18441841

18451842
mutex_lock(&pktgen_thread_lock);
18461843
pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
@@ -1861,32 +1858,49 @@ static int pktgen_mark_device(const char *ifname)
18611858
printk("pktgen_mark_device: timed out after waiting "
18621859
"%d msec for device %s to be removed\n",
18631860
msec_per_try * i, ifname);
1864-
ret = 1;
18651861
break;
18661862
}
18671863

18681864
}
18691865

18701866
mutex_unlock(&pktgen_thread_lock);
1867+
}
18711868

1872-
return ret;
1869+
static void pktgen_change_name(struct net_device *dev)
1870+
{
1871+
struct pktgen_thread *t;
1872+
1873+
list_for_each_entry(t, &pktgen_threads, th_list) {
1874+
struct pktgen_dev *pkt_dev;
1875+
1876+
list_for_each_entry(pkt_dev, &t->if_list, list) {
1877+
if (pkt_dev->odev != dev)
1878+
continue;
1879+
1880+
remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1881+
1882+
pkt_dev->entry = create_proc_entry(dev->name, 0600,
1883+
pg_proc_dir);
1884+
if (!pkt_dev->entry)
1885+
printk(KERN_ERR "pktgen: can't move proc "
1886+
" entry for '%s'\n", dev->name);
1887+
break;
1888+
}
1889+
}
18731890
}
18741891

18751892
static int pktgen_device_event(struct notifier_block *unused,
18761893
unsigned long event, void *ptr)
18771894
{
1878-
struct net_device *dev = (struct net_device *)(ptr);
1895+
struct net_device *dev = ptr;
18791896

18801897
/* It is OK that we do not hold the group lock right now,
18811898
* as we run under the RTNL lock.
18821899
*/
18831900

18841901
switch (event) {
1885-
case NETDEV_CHANGEADDR:
1886-
case NETDEV_GOING_DOWN:
1887-
case NETDEV_DOWN:
1888-
case NETDEV_UP:
1889-
/* Ignore for now */
1902+
case NETDEV_CHANGENAME:
1903+
pktgen_change_name(dev);
18901904
break;
18911905

18921906
case NETDEV_UNREGISTER:
@@ -1899,52 +1913,43 @@ static int pktgen_device_event(struct notifier_block *unused,
18991913

19001914
/* Associate pktgen_dev with a device. */
19011915

1902-
static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1916+
static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
19031917
{
19041918
struct net_device *odev;
1919+
int err;
19051920

19061921
/* Clean old setups */
1907-
19081922
if (pkt_dev->odev) {
19091923
dev_put(pkt_dev->odev);
19101924
pkt_dev->odev = NULL;
19111925
}
19121926

1913-
odev = dev_get_by_name(pkt_dev->ifname);
1914-
1927+
odev = dev_get_by_name(ifname);
19151928
if (!odev) {
1916-
printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1917-
goto out;
1929+
printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1930+
return -ENODEV;
19181931
}
1932+
19191933
if (odev->type != ARPHRD_ETHER) {
1920-
printk("pktgen: not an ethernet device: \"%s\"\n",
1921-
pkt_dev->ifname);
1922-
goto out_put;
1923-
}
1924-
if (!netif_running(odev)) {
1925-
printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1926-
goto out_put;
1934+
printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1935+
err = -EINVAL;
1936+
} else if (!netif_running(odev)) {
1937+
printk("pktgen: device is down: \"%s\"\n", ifname);
1938+
err = -ENETDOWN;
1939+
} else {
1940+
pkt_dev->odev = odev;
1941+
return 0;
19271942
}
1928-
pkt_dev->odev = odev;
19291943

1930-
return pkt_dev->odev;
1931-
1932-
out_put:
19331944
dev_put(odev);
1934-
out:
1935-
return NULL;
1936-
1945+
return err;
19371946
}
19381947

19391948
/* Read pkt_dev from the interface and set up internal pktgen_dev
19401949
* structure to have the right information to create/send packets
19411950
*/
19421951
static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
19431952
{
1944-
/* Try once more, just in case it works now. */
1945-
if (!pkt_dev->odev)
1946-
pktgen_setup_dev(pkt_dev);
1947-
19481953
if (!pkt_dev->odev) {
19491954
printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
19501955
sprintf(pkt_dev->result,
@@ -2988,7 +2993,7 @@ static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
29882993

29892994
if (!pkt_dev->running) {
29902995
printk("pktgen: interface: %s is already stopped\n",
2991-
pkt_dev->ifname);
2996+
pkt_dev->odev->name);
29922997
return -EINVAL;
29932998
}
29942999

@@ -3340,7 +3345,7 @@ static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
33403345
if_lock(t);
33413346

33423347
list_for_each_entry(p, &t->if_list, list)
3343-
if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
3348+
if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
33443349
pkt_dev = p;
33453350
break;
33463351
}
@@ -3381,7 +3386,7 @@ static int add_dev_to_thread(struct pktgen_thread *t,
33813386
static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
33823387
{
33833388
struct pktgen_dev *pkt_dev;
3384-
struct proc_dir_entry *pe;
3389+
int err;
33853390

33863391
/* We don't allow a device to be on several threads */
33873392

@@ -3423,29 +3428,28 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
34233428
pkt_dev->svlan_cfi = 0;
34243429
pkt_dev->svlan_id = 0xffff;
34253430

3426-
strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3427-
3428-
if (!pktgen_setup_dev(pkt_dev)) {
3429-
printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3430-
if (pkt_dev->flows)
3431-
vfree(pkt_dev->flows);
3432-
kfree(pkt_dev);
3433-
return -ENODEV;
3434-
}
3431+
err = pktgen_setup_dev(pkt_dev, ifname);
3432+
if (err)
3433+
goto out1;
34353434

3436-
pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3437-
if (!pe) {
3435+
pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3436+
if (!pkt_dev->entry) {
34383437
printk("pktgen: cannot create %s/%s procfs entry.\n",
34393438
PG_PROC_DIR, ifname);
3440-
if (pkt_dev->flows)
3441-
vfree(pkt_dev->flows);
3442-
kfree(pkt_dev);
3443-
return -EINVAL;
3439+
err = -EINVAL;
3440+
goto out2;
34443441
}
3445-
pe->proc_fops = &pktgen_if_fops;
3446-
pe->data = pkt_dev;
3442+
pkt_dev->entry->proc_fops = &pktgen_if_fops;
3443+
pkt_dev->entry->data = pkt_dev;
34473444

34483445
return add_dev_to_thread(t, pkt_dev);
3446+
out2:
3447+
dev_put(pkt_dev->odev);
3448+
out1:
3449+
if (pkt_dev->flows)
3450+
vfree(pkt_dev->flows);
3451+
kfree(pkt_dev);
3452+
return err;
34493453
}
34503454

34513455
static int __init pktgen_create_thread(int cpu)
@@ -3533,9 +3537,8 @@ static int pktgen_remove_device(struct pktgen_thread *t,
35333537

35343538
_rem_dev_from_if_list(t, pkt_dev);
35353539

3536-
/* Clean up proc file system */
3537-
3538-
remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
3540+
if (pkt_dev->entry)
3541+
remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
35393542

35403543
if (pkt_dev->flows)
35413544
vfree(pkt_dev->flows);

0 commit comments

Comments
 (0)