Network stats gatherer (loadable kernel module) isn't working? - device-driver

I'm kinda new to linux kernel modules and I'm trying to write my own module that gives me some statistics about a device ( the NIC here).
although I'm using kernel 2.6.35 and I've included linux/netdevices, compiling keeps saying that the function ndo_get_stats is implicitly declared. can anyone tell me what's going on ?
here's the module, simple I know,
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/netdevice.h> /* Needed for netdevice*/
static int __init hello_start(void)
{
struct net_device *dev;
struct net_device_stats *devstats;
printk(KERN_INFO "Loading Stats module...\n");
printk(KERN_ALERT "Hello world\n");
dev = first_net_device(&init_net);
while (dev)
{
printk(KERN_INFO "found [%s] and it's [%d]\n", dev->name, dev->flags & IFF_UP);
printk(KERN_INFO "End of dev struct ... now starts the get_stats struct\n");
devstats = ndo_get_stats(dev);
printk(KERN_INFO "recive errors: [%li]\n transmission errors: [%li]\n number of collisions: [%li]", devstats->rx_errors , devstats->tx_errors, devstats->collisions);
dev = next_net_device(dev);
}
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_ALERT "Goodbye.\n");
}
module_init(hello_start);
module_exit(hello_end);

Related

Is there a simple way to convert a lua table to a C++ array or vector?

I am starting to make my own package manager and am starting to develop a dependency system.
The builfiles are written in lua, they look something like this:
package = {
name = "pfetch",
version = "0.6.0",
source = "https://github.com/dylanaraps/pfetch/archive/0.6.0.tar.gz",
git = false
}
dependencies = {
"some_dep",
"some_dep2"
}
function install()
quantum_install("pfetch", false)
end
Only problem,I have no idea how to convert
dependencies = {
"some_dep",
"some_dep2"
}
To a global c++ array: ["some_dep", "some_dep2"]
Anything in the list that's not valid as a string should be ignored.
Any good way to do this?
Thanks in advance
Note: I am using the C api to interface with lua in C++. I don't know whether Lua's errors use longjmp or C++ exceptions.
Based on the clarification in your comment, something like this will work for you:
#include <iostream>
#include <string>
#include <vector>
#include <lua5.3/lua.hpp>
std::vector<std::string> dependencies;
static int q64795651_set_dependencies(lua_State *L) {
dependencies.clear();
lua_settop(L, 1);
for(lua_Integer i = 1; lua_geti(L, 1, i) != LUA_TNIL; ++i) {
size_t len;
const char *str = lua_tolstring(L, 2, &len);
if(str) {
dependencies.push_back(std::string{str, len});
}
lua_settop(L, 1);
}
return 0;
}
static int q64795651_print_dependencies(lua_State *) {
for(const auto &dep : dependencies) {
std::cout << dep << std::endl;
}
return 0;
}
static const luaL_Reg q64795651lib[] = {
{"set_dependencies", q64795651_set_dependencies},
{"print_dependencies", q64795651_print_dependencies},
{nullptr, nullptr}
};
extern "C"
int luaopen_q64795651(lua_State *L) {
luaL_newlib(L, q64795651lib);
return 1;
}
Demo:
$ g++ -fPIC -shared q64795651.cpp -o q64795651.so
$ lua5.3
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> q64795651 = require('q64795651')
> dependencies = {
>> "some_dep",
>> "some_dep2"
>> }
> q64795651.set_dependencies(dependencies)
> q64795651.print_dependencies()
some_dep
some_dep2
>
One important pitfall: since you're not sure if Lua is compiled to use longjmp or exceptions for its errors, you need to make sure that you don't have any automatic variables with destructors anywhere that a Lua error could happen. (This is already the case in the code in my answer; just make sure you don't accidentally add any such places when incorporating this into your program.)

Why does NodeHandle hang?

I'm having an issue trying to create a subscriber in Indigo. I have a shared_ptr within a class to hold the NodeHandle object. I do this so that NodeHandle can be used in other class members. The problem is when the thread starts, it seems to hang on the make_shared call to the NodeHandle object within the MyClass constructor as it never reaches the next line after.
class MyClass
{
private:
std::shared_ptr<ros::NodeHandle> nh;
std::map<std::string, std::string> remap;
// ...
};
MyClass::MyClass()
{
// remap is empty
ros::init(remap, "my_node");
nh = make_shared<ros::NodeHandle>();
cout << "does not reach this line" << endl;
}
int MyClass::run()
{
// ...
}
I start the thread liks so ...
{
// ...
myobj = make_shared<MyClass>();
my_thread = thread(&MyClass::run, myobj);
// ...
}
Thoughts?
It appears that the problem was due to having my own boost logging system in place and not using the ROS logger (which made it difficult to find as it seemingly has nothing to do with ros::NodeHandle, but probably does underneath). I would comment out my entire code base and start adding to see when ros::NodeHandle would run and at the point of removing my logger and adding it back, I would see the difference between it running and hanging.
Well, here's an example of using Boost::make_shared for a nodehandle.
Note that it makes use of ros::NodeHandlePtr, an already existant Boost shared pointer not using the "std::make_shared" one.
This maybe does not really answer the question but I am suggesting another way around using the boost library.
#include <ros/ros.h>
#include <std_msgs/Empty.h>
#include <boost/thread/thread.hpp>
void do_stuff(int* publish_rate)
{
ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
ros::Publisher pub_b = node->advertise<std_msgs::Empty>("topic_b", 10);
ros::Rate loop_rate(*publish_rate);
while (ros::ok())
{
std_msgs::Empty msg;
pub_b.publish(msg);
loop_rate.sleep();
}
}
int main(int argc, char** argv)
{
int rate_b = 1; // 1 Hz
ros::init(argc, argv, "mt_node");
// spawn another thread
boost::thread thread_b(do_stuff, &rate_b);
ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
ros::Publisher pub_a = node->advertise<std_msgs::Empty>("topic_a", 10);
ros::Rate loop_rate(10); // 10 Hz
while (ros::ok())
{
std_msgs::Empty msg;
pub_a.publish(msg);
loop_rate.sleep();
// process any incoming messages in this thread
ros::spinOnce();
}
// wait the second thread to finish
thread_b.join();
return 0;
}
In case you get trouble with the CMakeLists, here it is :
cmake_minimum_required(VERSION 2.8.3)
project(test_thread)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
)
find_package(Boost COMPONENTS thread REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
catkin_package(CATKIN_DEPENDS roscpp rospy std_msgs)
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(thread src/thread_test.cpp)
target_link_libraries(thread ${catkin_LIBRARIES} ${BOOST_LIBRARIES})
Hope that helps !
Cheers,

Creating topology with routers in ns-3

I am trying to create a very simple topology containing a server and client connected via a router. The following code I got through a GUI. I am trying to run it but it throws me a long error I am new to ns-3, so kindly bear with me.
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/core-module.h"
#include "ns3/common-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/helper-module.h"
#include "ns3/bridge-module.h"
using namespace ns3;
int main(int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
/* Configuration. */
/* Build nodes. */
NodeContainer term_0;
term_0.Create (1);
NodeContainer term_1;
term_1.Create (1);
NodeContainer router_0;
router_0.Create (1);
/* Build link. */
CsmaHelper csma_hub_0;
csma_hub_0.SetChannelAttribute ("DataRate", DataRateValue (100000000));
csma_hub_0.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000)));
CsmaHelper csma_hub_1;
csma_hub_1.SetChannelAttribute ("DataRate", DataRateValue (100000000));
csma_hub_1.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000)));
/* Build link net device container. */
NodeContainer all_hub_0;
all_hub_0.Add (router_0);
all_hub_0.Add (term_0);
NetDeviceContainer ndc_hub_0 = csma_hub_0.Install (all_hub_0);
NodeContainer all_hub_1;
all_hub_1.Add (router_0);
all_hub_1.Add (term_1);
NetDeviceContainer ndc_hub_1 = csma_hub_1.Install (all_hub_1);
/* Install the IP stack. */
InternetStackHelper internetStackH;
internetStackH.Install (term_0);
internetStackH.Install (term_1);
internetStackH.Install (router_0);
/* IP assign. */
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0);
ipv4.SetBase ("10.0.1.0", "255.255.255.0");
Ipv4InterfaceContainer iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1);
/* Generate Route. */
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
/* Generate Application. */
uint16_t port_udpEcho_0 = 9;
UdpEchoServerHelper server_udpEcho_0 (port_udpEcho_0);
ApplicationContainer apps_udpEcho_0 = server_udpEcho_0.Install (term_1.Get(0));
apps_udpEcho_0.Start (Seconds (0.0));
apps_udpEcho_0.Stop (Seconds (2.0));
Time interPacketInterval_udpEcho_0 = Seconds (1.0);
UdpEchoClientHelper client_udpEcho_0 (iface_ndc_hub_1.GetAddress(1), 9);
client_udpEcho_0.SetAttribute ("MaxPackets", UintegerValue (1));
client_udpEcho_0.SetAttribute ("Interval", TimeValue (interPacketInterval_udpEcho_0));
client_udpEcho_0.SetAttribute ("PacketSize", UintegerValue (1024));
apps_udpEcho_0 = client_udpEcho_0.Install (term_0.Get (0));
apps_udpEcho_0.Start (Seconds (0.1));
apps_udpEcho_0.Stop (Seconds (2.0));
/* Simulation. */
/* Pcap output. */
/* Stop the simulation after x seconds. */
uint32_t stopTime = 3;
Simulator::Stop (Seconds (stopTime));
/* Start and clean simulation. */
Simulator::Run ();
Simulator::Destroy ();
}
If you are new, you should start your program by looking the examples already provided by ns-3. Like this: https://www.nsnam.org/doxygen/simple-routing-ping6_8cc_source.html. Looking the network topology, it is what you want.
Trying to run your code, I had to change a lot of headers to make it compile (maybe different versions, given the time - or your GUI is outdated). Remove yours and put these:
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/csma-helper.h"
And finally, you'd printed nothing, so I am not sure of what you were trying to do. As you are using, UdpEchoClientHelper and UdpEchoServerHelper, I would recommend to enable their logs. You can do this with these two lines:
/* Configuration. */
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
I am new too and I do not know what you are trying to do. So I will stop my answer here. Good luck.

My lua script load .so library, how can I write the host program with Lua 5.2?

I searched and tried for days. The problem is this:
I wrote a script which load a shared library locker.so, it runs well with lua interpretor, but I can not write out the correct host program.
My lua script load_so.lua is very simple:
locker = require("locker")
print(type(locker))
for k, v in pairs(locker) do
print(k, v)
end
My host program is:
int main(int argc, const char *argv[])
{
lua_State * L = luaL_newstate();
luaL_openlibs(L);
if (luaL_dofile(L, "load_so.lua") != 0) {
fprintf(stderr, "luaL_dofile error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
When I run my host program, error print out:
luaL_dofile error: error loading module 'locker' from file './locker.so':
./locker.so: undefined symbol: lua_pushstring
And the locker.c:
static int elock_get(lua_State * L) {...}
static int elock_set(lua_State * L) {...}
static const struct luaL_Reg lockerlib[] = {
{"get", elock_get},
{"set", elock_set},
{NULL, NULL}
};
int luaopen_locker(lua_State *L)
{
//luaL_newlib(L, lockerlib);
//lua_pushvalue(L, -1);
//lua_setglobal(L, LOCKER_LIBNAME);
//set_info(L);
luaL_newlibtable(L, lockerlib);
luaL_setfuncs(L, lockerlib, 0);
return 1;
}
Most articles, books, questions shows how to do it in Lua 5.1, and yes, the program runs correctly in Lua 5.1. But how can I make it support Lua 5.2, and why?
P.S: I don't want to load the library in my C host program like luaL_requiref(L, "locker", luaopen_locker, 1), because I don't know which .so library will load in Lua script.
Thanks.
In Linux, if you're linking liblua.a statically into your main program, you need to use -Wl,-E when linking to export the Lua API symbols; this is how the standard command line interpreter is built.

Open CV Image editing library for windows 8 and windows phone 8

Is there any support of OpenCV graphics library is available for Windows Phone 8 and Windows 8. I made a search on Google but didn't find any resource related with OpenCV to connect with Windows Phone 8 / Windows 8. If any of you know more about this please help me, and provide some link to reach the library.
This is the latest information what I get from OpenCV team.
OpenCV development team is working on port for Windows RT. Here is current development branch for WinRT(https://github.com/asmorkalov/opencv/tree/winrt). You can build it for ARM using Visual Studio Express for Windows 8 and Platform SDK.
Open Visual Studio development console.
Setup environment for cross compilation by command "C:\Program Files(x86)\Microsoft
Visual Studio 11.0\VC\bin\x86_arm\vcvarsx86_arm.bat"
cd <opencv_source_dir>/platforms/winrt/
run scripts/cmake_winrt.cmd
run ninja
Alternatively you can use nmake instead ninja. You need to edit cmake_winrt.cmd and change project generator fro -GNinja to -G "NMake Makefiles". Only algorithmic part of the library is supported now, no tbb, no UI, no video IO.
Please check the below given URL from more details.
http://answers.opencv.org/question/9847/opencv-for-windows-8-tablet/?answer=9851#post-id-9851
By windows-8, I guess you mean winRT ? AFAIK, there is no official port to winRT. You need to compile it by yourself as a Win8 Store DLL for instance, so that you can reference it from a Win8 Store Application.
Just start by opencv-core, then add the lib you need, one by one, because all the components will not be able to compile (for instance, opencv-highgui is highly dependant on Windows API which is not fully compatible with Win8 Store Apps).
You'll also need to code by yourself some Win32 methods used by OpenCV and not accessible from Win8 App like GetSystemInfo(), GetTempPathA(), GetTempFileNameA() and all methods related to thread local storage (TLS).
I've been able to use a small subset of OpenCV in WinRT by compiling opencv_core, opencv_imgproc and zlib, as 3 seperate static libs. I've added one another, called opencv_winrt, that contains only the two following files:
opencv_winrt.h
#pragma once
#include "combaseapi.h"
void WINAPI GetSystemInfo(
_Out_ LPSYSTEM_INFO lpSystemInfo
);
DWORD WINAPI GetTempPathA(
_In_ DWORD nBufferLength,
_Out_ char* lpBuffer
);
UINT WINAPI GetTempFileNameA(
_In_ const char* lpPathName,
_In_ const char* lpPrefixString,
_In_ UINT uUnique,
_Out_ char* lpTempFileName
);
DWORD WINAPI TlsAlloc();
BOOL WINAPI TlsFree(
_In_ DWORD dwTlsIndex
);
LPVOID WINAPI TlsGetValue(
_In_ DWORD dwTlsIndex
);
BOOL WINAPI TlsSetValue(
_In_ DWORD dwTlsIndex,
_In_opt_ LPVOID lpTlsValue
);
void WINAPI TlsShutdown();
# define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
opencv_winrt.cpp
#include "opencv_winrt.h"
#include <vector>
#include <set>
#include <mutex>
#include "assert.h"
void WINAPI GetSystemInfo(LPSYSTEM_INFO lpSystemInfo)
{
GetNativeSystemInfo(lpSystemInfo);
}
DWORD WINAPI GetTempPathA(DWORD nBufferLength, char* lpBuffer)
{
return 0;
}
UINT WINAPI GetTempFileNameA(const char* lpPathName, const char* lpPrefixString, UINT uUnique, char* lpTempFileName)
{
return 0;
}
// Thread local storage.
typedef std::vector<void*> ThreadLocalData;
static __declspec(thread) ThreadLocalData* currentThreadData = nullptr;
static std::set<ThreadLocalData*> allThreadData;
static DWORD nextTlsIndex = 0;
static std::vector<DWORD> freeTlsIndices;
static std::mutex tlsAllocationLock;
DWORD WINAPI TlsAlloc()
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
// Can we reuse a previously freed TLS slot?
if (!freeTlsIndices.empty())
{
DWORD result = freeTlsIndices.back();
freeTlsIndices.pop_back();
return result;
}
// Allocate a new TLS slot.
return nextTlsIndex++;
}
_Use_decl_annotations_ BOOL WINAPI TlsFree(DWORD dwTlsIndex)
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
assert(dwTlsIndex < nextTlsIndex);
assert(find(freeTlsIndices.begin(), freeTlsIndices.end(), dwTlsIndex) == freeTlsIndices.end());
// Store this slot for reuse by TlsAlloc.
try
{
freeTlsIndices.push_back(dwTlsIndex);
}
catch (...)
{
return false;
}
// Zero the value for all threads that might be using this now freed slot.
for each (auto threadData in allThreadData)
{
if (threadData->size() > dwTlsIndex)
{
threadData->at(dwTlsIndex) = nullptr;
}
}
return true;
}
_Use_decl_annotations_ LPVOID WINAPI TlsGetValue(DWORD dwTlsIndex)
{
ThreadLocalData* threadData = currentThreadData;
if (threadData && threadData->size() > dwTlsIndex)
{
// Return the value of an allocated TLS slot.
return threadData->at(dwTlsIndex);
}
else
{
// Default value for unallocated slots.
return nullptr;
}
}
_Use_decl_annotations_ BOOL WINAPI TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue)
{
ThreadLocalData* threadData = currentThreadData;
if (!threadData)
{
// First time allocation of TLS data for this thread.
try
{
threadData = new ThreadLocalData(dwTlsIndex + 1, nullptr);
std::lock_guard<std::mutex> lock(tlsAllocationLock);
allThreadData.insert(threadData);
currentThreadData = threadData;
}
catch (...)
{
if (threadData)
delete threadData;
return false;
}
}
else if (threadData->size() <= dwTlsIndex)
{
// This thread already has a TLS data block, but it must be expanded to fit the specified slot.
try
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
threadData->resize(dwTlsIndex + 1, nullptr);
}
catch (...)
{
return false;
}
}
// Store the new value for this slot.
threadData->at(dwTlsIndex) = lpTlsValue;
return true;
}
// Called at thread exit to clean up TLS allocations.
void WINAPI TlsShutdown()
{
ThreadLocalData* threadData = currentThreadData;
if (threadData)
{
{
std::lock_guard<std::mutex> lock(tlsAllocationLock);
allThreadData.erase(threadData);
}
currentThreadData = nullptr;
delete threadData;
}
}
And I modify the file cvconfig.h: I've commented out every #define, except PACKAGE* and VERSION, and I added #include "opencv_winrt.h" at the end.
Just a hint - there is a C# wrapper for OpenCV called EmguCV (http://www.emgu.com/wiki/index.php/Main_Page), by looking at the forum posts I see that there is some activity towards using it on Windows 8 but it's hard to tell if it's now working since the posts claiming issues are quite old. I'd suggest you just give it a try and see if this C# wrapper is able to run on Windows Phone 8, I think it should definitely run on Windows 8.

Resources