Skip to content

Commit

Permalink
Clean netlink includes (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Mar 12, 2020
1 parent 8b54767 commit e6d5c1a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 99 deletions.
1 change: 0 additions & 1 deletion common/linkcache.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <string.h>
#include <errno.h>
#include <system_error>
#include <netlink/route/link.h>
#include "common/logger.h"
#include "common/linkcache.h"

Expand Down
10 changes: 2 additions & 8 deletions common/linkcache.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#ifndef __LINKCACHE__
#define __LINKCACHE__
#pragma once

#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>
#include <netlink/data.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/link.h>

#include <string>

namespace swss {
Expand All @@ -28,5 +24,3 @@ class LinkCache {
};

}

#endif
4 changes: 2 additions & 2 deletions common/netdispatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <map>
#include "common/netdispatcher.h"
#include "common/netmsg.h"

#include <map>

using namespace swss;

Expand Down
84 changes: 36 additions & 48 deletions common/netdispatcher.h
Original file line number Diff line number Diff line change
@@ -1,66 +1,54 @@
#ifndef __NETDISPATCHER__
#define __NETDISPATCHER__

#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>
#include <netlink/data.h>
#include <netlink/route/rtnl.h>

#include <map>
#include <mutex>
#pragma once

#include "netmsg.h"

namespace swss {
#include <netlink/msg.h>

class NetDispatcher {
public:
#include <map>
#include <mutex>

/*
* Get singlton instance*/
static NetDispatcher& getInstance();
namespace swss
{
class NetDispatcher
{
public:

/*
* Register callback class according to message-type.
*
* Throw exception if callback is already registered.
*/
void registerMessageHandler(int nlmsg_type, NetMsg *callback);
/** Get singlton instance. */
static NetDispatcher& getInstance();

/*
* Called by NetLink or FpmLink classes as indication of new packet arrival
*/
void onNetlinkMessage(struct nl_msg *msg);
/**
* Register callback class according to message-type.
*
* Throw exception if callback is already registered.
*/
void registerMessageHandler(int nlmsg_type, NetMsg *callback);

/*
* Unregister callback according to message-type.
*
* Throw exception if callback is not registered.
*/
void unregisterMessageHandler(int nlmsg_type);
/** Called by NetLink or FpmLink classes as indication of new packet arrival. */
void onNetlinkMessage(struct nl_msg *msg);

private:
/**
* Unregister callback according to message-type.
*
* Throw exception if callback is not registered.
*/
void unregisterMessageHandler(int nlmsg_type);

NetDispatcher() = default;
private:

NetDispatcher(const NetDispatcher&) = delete;
NetDispatcher() = default;

NetMsg* getCallback(int nlmsg_type);
NetDispatcher(const NetDispatcher&) = delete;

private:
NetMsg* getCallback(int nlmsg_type);

/* nl_msg_parse callback API */
static void nlCallback(struct nl_object *obj, void *context);
private:

std::map<int, NetMsg*> m_handlers;
/** nl_msg_parse callback API */
static void nlCallback(struct nl_object *obj, void *context);

/*
* Mutex protecting register, unregister and get callback methods.
*/
std::mutex m_mutex;
};
std::map<int, NetMsg*> m_handlers;

/** Mutex protecting register, unregister and get callback methods. */
std::mutex m_mutex;
};
}

#endif
11 changes: 6 additions & 5 deletions common/netlink.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <string.h>
#include <errno.h>
#include <system_error>
#include "common/logger.h"
#include "common/netmsg.h"
#include "common/netdispatcher.h"
#include "common/netlink.h"
#include "common/netdispatcher.h"

#include <errno.h>

#include <system_error>
#include <cstring>

using namespace swss;
using namespace std;
Expand Down
39 changes: 20 additions & 19 deletions common/netlink.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#ifndef __NETLINK__
#define __NETLINK__
#pragma once

#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include "selectable.h"

namespace swss {
#include <netlink/netlink.h>
#include <netlink/route/rtnl.h>

class NetLink : public Selectable {
public:
NetLink(int pri = 0);
~NetLink() override;
namespace swss
{
class NetLink :
public Selectable
{
public:

void registerGroup(int rtnlGroup);
void dumpRequest(int rtmGetCommand);
NetLink(int pri = 0);
virtual ~NetLink();

int getFd() override;
uint64_t readData() override;
void registerGroup(int rtnlGroup);
void dumpRequest(int rtmGetCommand);

private:
static int onNetlinkMsg(struct nl_msg *msg, void *arg);
int getFd() override;
uint64_t readData() override;

nl_sock *m_socket;
};
private:

}
static int onNetlinkMsg(struct nl_msg *msg, void *arg);

#endif
struct nl_sock *m_socket;
};
}
25 changes: 9 additions & 16 deletions common/netmsg.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#ifndef __NETMSG__
#define __NETMSG__
#pragma once

#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>
#include <netlink/data.h>
#include <netlink/route/rtnl.h>

namespace swss {

class NetMsg {
public:
/* Called by NetDispatcher when netmsg matches filters */
virtual void onMsg(int nlmsg_type, struct nl_object *obj) = 0;
};

namespace swss
{
class NetMsg
{
public:
/* Called by NetDispatcher when netmsg matches filters */
virtual void onMsg(int nlmsg_type, struct nl_object *obj) = 0;
};
}

#endif

0 comments on commit e6d5c1a

Please sign in to comment.