Skip to content

Commit d345876

Browse files
author
David Sommerseth
committed
Split out generic NETLINK functions from etherinfo.c to netlink.c
Primarily just to clean up the code Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent 3d7572b commit d345876

File tree

3 files changed

+114
-86
lines changed

3 files changed

+114
-86
lines changed

python-ethtool/etherinfo.c

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020
#include <stdio.h>
2121
#include <string.h>
2222
#include <sys/types.h>
23-
#include <unistd.h>
24-
#include <fcntl.h>
2523
#include <stdlib.h>
2624
#include <asm/types.h>
2725
#include <sys/socket.h>
2826
#include <arpa/inet.h>
29-
#include <netlink/netlink.h>
30-
#include <netlink/socket.h>
3127
#include <netlink/cache.h>
3228
#include <netlink/addr.h>
3329
#include <netlink/route/addr.h>
@@ -39,8 +35,6 @@
3935
#include "etherinfo_struct.h"
4036
#include "etherinfo.h"
4137

42-
pthread_mutex_t nlc_counter_mtx = PTHREAD_MUTEX_INITIALIZER;
43-
4438
/*
4539
*
4640
* Internal functions for working with struct etherinfo
@@ -287,83 +281,3 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
287281
}
288282
return ret;
289283
}
290-
291-
292-
/**
293-
* Connects to the NETLINK interface. This will be called
294-
* for each etherinfo object being generated, and it will
295-
* keep a separate file descriptor open for each object
296-
*
297-
* @param data etherinfo_obj_data structure
298-
*
299-
* @return Returns 1 on success, otherwise 0.
300-
*/
301-
int open_netlink(struct etherinfo_obj_data *data)
302-
{
303-
if( !data ) {
304-
return 0;
305-
}
306-
307-
/* Reuse already established NETLINK connection, if a connection exists */
308-
if( *data->nlc ) {
309-
/* If this object has not used NETLINK earlier, tag it as a user */
310-
if( !data->nlc_active ) {
311-
pthread_mutex_lock(&nlc_counter_mtx);
312-
(*data->nlc_users)++;
313-
pthread_mutex_unlock(&nlc_counter_mtx);
314-
}
315-
data->nlc_active = 1;
316-
return 1;
317-
}
318-
319-
/* No earlier connections exists, establish a new one */
320-
*data->nlc = nl_socket_alloc();
321-
nl_connect(*data->nlc, NETLINK_ROUTE);
322-
if( (*data->nlc != NULL) ) {
323-
/* Force O_CLOEXEC flag on the NETLINK socket */
324-
if( fcntl(nl_socket_get_fd(*data->nlc), F_SETFD, FD_CLOEXEC) == -1 ) {
325-
fprintf(stderr,
326-
"**WARNING** Failed to set O_CLOEXEC on NETLINK socket: %s\n",
327-
strerror(errno));
328-
}
329-
330-
/* Tag this object as an active user */
331-
pthread_mutex_lock(&nlc_counter_mtx);
332-
(*data->nlc_users)++;
333-
pthread_mutex_unlock(&nlc_counter_mtx);
334-
data->nlc_active = 1;
335-
return 1;
336-
} else {
337-
return 0;
338-
}
339-
}
340-
341-
342-
/**
343-
* Closes the NETLINK connection. This should be called automatically whenever
344-
* the corresponding etherinfo object is deleted.
345-
*
346-
* @param ptr Pointer to the pointer of struct nl_handle, which contains the NETLINK connection
347-
*/
348-
void close_netlink(struct etherinfo_obj_data *data)
349-
{
350-
if( !data || !(*data->nlc) ) {
351-
return;
352-
}
353-
354-
/* Untag this object as a NETLINK user */
355-
data->nlc_active = 0;
356-
pthread_mutex_lock(&nlc_counter_mtx);
357-
(*data->nlc_users)--;
358-
pthread_mutex_unlock(&nlc_counter_mtx);
359-
360-
/* Don't close the connection if there are more users */
361-
if( *data->nlc_users > 0) {
362-
return;
363-
}
364-
365-
/* Close NETLINK connection */
366-
nl_close(*data->nlc);
367-
nl_socket_free(*data->nlc);
368-
*data->nlc = NULL;
369-
}

python-ethtool/netlink.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* netlink.c - Generic NETLINK API functions
2+
*
3+
* Copyright (C) 2009-2013 Red Hat Inc.
4+
*
5+
* David Sommerseth <davids@redhat.com>
6+
*
7+
* This application is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU General Public License as published by the Free
9+
* Software Foundation; version 2.
10+
*
11+
* This application is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* General Public License for more details.
15+
*/
16+
17+
#include <Python.h>
18+
#include <pthread.h>
19+
#include <unistd.h>
20+
#include <fcntl.h>
21+
#include <netlink/netlink.h>
22+
#include <netlink/socket.h>
23+
24+
#include "etherinfo_struct.h"
25+
26+
pthread_mutex_t nlc_counter_mtx = PTHREAD_MUTEX_INITIALIZER;
27+
28+
29+
/**
30+
* Connects to the NETLINK interface. This will be called
31+
* for each etherinfo object being generated, and it will
32+
* keep a separate file descriptor open for each object
33+
*
34+
* @param data etherinfo_obj_data structure
35+
*
36+
* @return Returns 1 on success, otherwise 0.
37+
*/
38+
int open_netlink(struct etherinfo_obj_data *data)
39+
{
40+
if( !data ) {
41+
return 0;
42+
}
43+
44+
/* Reuse already established NETLINK connection, if a connection exists */
45+
if( *data->nlc ) {
46+
/* If this object has not used NETLINK earlier, tag it as a user */
47+
if( !data->nlc_active ) {
48+
pthread_mutex_lock(&nlc_counter_mtx);
49+
(*data->nlc_users)++;
50+
pthread_mutex_unlock(&nlc_counter_mtx);
51+
}
52+
data->nlc_active = 1;
53+
return 1;
54+
}
55+
56+
/* No earlier connections exists, establish a new one */
57+
*data->nlc = nl_socket_alloc();
58+
nl_connect(*data->nlc, NETLINK_ROUTE);
59+
if( (*data->nlc != NULL) ) {
60+
/* Force O_CLOEXEC flag on the NETLINK socket */
61+
if( fcntl(nl_socket_get_fd(*data->nlc), F_SETFD, FD_CLOEXEC) == -1 ) {
62+
fprintf(stderr,
63+
"**WARNING** Failed to set O_CLOEXEC on NETLINK socket: %s\n",
64+
strerror(errno));
65+
}
66+
67+
/* Tag this object as an active user */
68+
pthread_mutex_lock(&nlc_counter_mtx);
69+
(*data->nlc_users)++;
70+
pthread_mutex_unlock(&nlc_counter_mtx);
71+
data->nlc_active = 1;
72+
return 1;
73+
} else {
74+
return 0;
75+
}
76+
}
77+
78+
79+
/**
80+
* Closes the NETLINK connection. This should be called automatically whenever
81+
* the corresponding etherinfo object is deleted.
82+
*
83+
* @param ptr Pointer to the pointer of struct nl_handle, which contains the NETLINK connection
84+
*/
85+
void close_netlink(struct etherinfo_obj_data *data)
86+
{
87+
if( !data || !(*data->nlc) ) {
88+
return;
89+
}
90+
91+
/* Untag this object as a NETLINK user */
92+
data->nlc_active = 0;
93+
pthread_mutex_lock(&nlc_counter_mtx);
94+
(*data->nlc_users)--;
95+
pthread_mutex_unlock(&nlc_counter_mtx);
96+
97+
/* Don't close the connection if there are more users */
98+
if( *data->nlc_users > 0) {
99+
return;
100+
}
101+
102+
/* Close NETLINK connection */
103+
nl_close(*data->nlc);
104+
nl_socket_free(*data->nlc);
105+
*data->nlc = NULL;
106+
}
107+
108+
/*
109+
Local variables:
110+
c-basic-offset: 8
111+
indent-tabs-mode: y
112+
End:
113+
*/

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def _str2list(pkgstr, onlystr):
6060
'python-ethtool/ethtool.c',
6161
'python-ethtool/etherinfo.c',
6262
'python-ethtool/etherinfo_obj.c',
63+
'python-ethtool/netlink.c',
6364
'python-ethtool/netlink-address.c'],
6465
include_dirs = libnl['include'],
6566
library_dirs = libnl['libdirs'],

0 commit comments

Comments
 (0)