Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
roccrtx committed Oct 22, 2018
0 parents commit 09ec7b3
Show file tree
Hide file tree
Showing 21 changed files with 4,157 additions and 0 deletions.
89 changes: 89 additions & 0 deletions common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#pragma once

#include <cstdint>

#include "logging.hpp"
#include "rnic.hpp"
#include "mr.hpp"

namespace rdmaio {

// connection status
enum ConnStatus {
SUCC = 0,
TIMEOUT,
WRONG_ARG,
ERR,
NOT_READY
};

/**
* The connection information exchanged between different QPs.
* RC/UC QPs uses lid & addr to conncet to remote QPs, while qpn is used upon send requests.
* node_id & port_id is used for UD QP to create addresses.
*/
struct QPAttr {
address_t addr;
uint16_t lid;
uint32_t qpn;
uint32_t psn;
uint16_t node_id;
uint16_t port_id;
};

/**
* The QP connection requests sent to remote.
* from_node & from_worker identifies which QP it shall connect to
*/
struct QPConnArg {
uint16_t from_node;
uint8_t from_worker;
uint8_t qp_type; // RC QP or UD QP
};

/**
* The MR connection requests sent to remote.
*/
struct MRConnArg {
uint16_t mr_id;
};

struct ConnArg {
enum { MR, QP } type;
union {
QPConnArg qp;
MRConnArg mr;
} payload;
};

struct ConnReply {
ConnStatus ack;
union {
QPAttr qp;
MemoryAttr mr;
} payload;
};

inline int convert_mtu(ibv_mtu type) {
int mtu = 0;
switch(type) {
case IBV_MTU_256:
mtu = 256;
break;
case IBV_MTU_512:
mtu = 512;
break;
case IBV_MTU_1024:
mtu = 1024;
break;
case IBV_MTU_2048:
mtu = 2048;
break;
case IBV_MTU_4096:
mtu = 4096;
break;
}
return mtu;
}

} // namespace rdmaio
120 changes: 120 additions & 0 deletions logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* The logging utilities used in libRDMA.
*/

#pragma once

#include <iostream>
#include <sstream>

namespace rdmaio {

/**
* \def LOG_FATAL
* Used for fatal and probably irrecoverable conditions
* \def LOG_ERROR
* Used for errors which are recoverable within the scope of the function
* \def LOG_WARNING
* Logs interesting conditions which are probably not fatal
* \def LOG_EMPH
* Outputs as LOG_INFO, but in LOG_WARNING colors. Useful for
* outputting information you want to emphasize.
* \def LOG_INFO
* Used for providing general useful information
* \def LOG_DEBUG
* Debugging purposes only
* \def LOG_EVERYTHING
* Log everything
*/

#define LOG_NONE 7
#define LOG_FATAL 6
#define LOG_ERROR 5
#define LOG_WARNING 4
#define LOG_EMPH 3
#define LOG_INFO 2
#define LOG_DEBUG 1
#define LOG_EVERYTHING 0

#define unlikely(x) __builtin_expect(!!(x), 0)

#ifndef RDMA_LOG_LEVEL
#define RDMA_LOG_LEVEL LOG_INFO
#endif

// logging macro definiations
// default log
#define RDMA_LOG(n) \
if (n >= RDMA_LOG_LEVEL) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, n).stream()

// log with tag
#define RDMA_TLOG(n,t) \
if(n >= RDMA_LOG_LEVEL) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, n).stream() \
<< "[" << (t) << "]"

#define RDMA_LOG_IF(n,condition) \
if(n >= RDMA_LOG_LEVEL && (condition)) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, n).stream()

#define RDMA_ASSERT(condition) \
if(unlikely(!(condition))) \
::rdmaio::MessageLogger((char*)__FILE__, __LINE__, LOG_FATAL + 1).stream() << "Assertion! "

#define RDMA_VERIFY(n,condition) RDMA_LOG_IF(n,(!(condition)))

class MessageLogger {
public:
MessageLogger(const char *file, int line, int level) :level_(level) {
if(level_ < RDMA_LOG_LEVEL)
return;
stream_ << "[" << StripBasename(std::string(file)) << ":" << line << "] ";
}

~MessageLogger() {
if(level_ >= RDMA_LOG_LEVEL) {
stream_ << "\n";
std::cout << "\033[" << RDMA_DEBUG_LEVEL_COLOR[std::min(level_,6)] << "m"
<< stream_.str() << EndcolorFlag();
if(level_ >= LOG_FATAL)
abort();
}
}

// Return the stream associated with the logger object.
std::stringstream &stream() { return stream_; }
private:
std::stringstream stream_;
int level_;

// control flags for color
#define R_BLACK 39
#define R_RED 31
#define R_GREEN 32
#define R_YELLOW 33
#define R_BLUE 34
#define R_MAGENTA 35
#define R_CYAN 36
#define R_WHITE 37

const int RDMA_DEBUG_LEVEL_COLOR[7] = {R_BLACK,R_YELLOW,R_BLACK,R_GREEN,R_MAGENTA,R_RED,R_RED};

static std::string StripBasename(const std::string &full_path) {
const char kSeparator = '/';
size_t pos = full_path.rfind(kSeparator);
if (pos != std::string::npos) {
return full_path.substr(pos + 1, std::string::npos);
} else {
return full_path;
}
}

static std::string EndcolorFlag() {
char flag[7];
snprintf(flag,7, "%c[0m", 0x1B);
return std::string(flag);
}
};

};
54 changes: 54 additions & 0 deletions mr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <infiniband/verbs.h>
#include "logging.hpp"

namespace rdmaio {

struct MemoryAttr {
uintptr_t buf;
uint32_t key;
};

struct Memory {

/**
* The default protection flag of a memory region.
* In default, the memory can be read/write by local and remote RNIC operations.
*/
static const int DEFAULT_PROTECTION_FLAG = (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | \
IBV_ACCESS_REMOTE_WRITE | IBV_ACCESS_REMOTE_ATOMIC);


Memory(char *addr,uint64_t len,ibv_pd *pd,int flag):
addr(addr),len(len){

mr = ibv_reg_mr(pd,addr,len,flag);
if(mr == nullptr) {

} else {
rattr.buf = (uintptr_t)addr;
rattr.key = mr->rkey;
}
}

~Memory() {
if(mr != NULL) {
int rc = ibv_dereg_mr(mr);
RDMA_LOG_IF(LOG_ERROR,rc != 0) << "dereg mr error: " << strerror(errno);
}
}

bool valid() {
return mr != nullptr;
}

char *addr;
uint64_t len;

MemoryAttr rattr; // RDMA registered attr
ibv_mr *mr = NULL; // mr in the driver
};


}; // namespace rdmaio
93 changes: 93 additions & 0 deletions msg_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma once

#include <string>
#include <set>
#include <functional>

#include "common.hpp"

namespace rdmaio {

typedef std::function<void(char *,int,int)> msg_callback_t_;

/**
* An abstract message interface
* Assumption: one per thread
*/
class MsgAdapter {
public:

MsgAdapter(msg_callback_t_ callback)
: callback_(callback) {
}
virtual ConnStatus connect(std::string ip,int port) = 0;

/**
* Basic send interfaces
*/
virtual ConnStatus send_to(int node_id,char *msg,int len) = 0;

virtual ConnStatus send_to(int node_id,int tid,char *msg,int len) {
return send_to(node_id,msg,len);
}

/**
* Interfaces which allow batching at the sender's side
*/
virtual void prepare_pending() {
}

virtual ConnStatus send_pending(int node_id,char *msg,int len) {
return send_to(node_id,msg,len);
}

virtual ConnStatus send_pending(int node_id,int tid,char *msg,int len) {
return send_to(node_id,tid,msg,len);
}

/**
* Flush all the currently pended message
*/
virtual ConnStatus flush_pending() {
return SUCC;
}

/**
* Examples to use batching at the sender side
* Broadcast the message to a set of servers
*/
virtual ConnStatus broadcast_to(const std::set<int> &nodes, char *msg,int len) {
prepare_pending();
for(auto it = nodes.begin(); it != nodes.end(); ++it) {
send_pending(*it,msg,len);
}
flush_pending();
return SUCC; // TODO
}

virtual ConnStatus broadcast_to(int *nodes,int num, char *msg,int len) {
prepare_pending();
for(int i = 0;i < num;++i) {
send_pending(nodes[i],msg,len);
}
flush_pending();
return SUCC; // TODO
}

/**
* The receive function
*/
virtual void poll_comps() = 0;

/**
* The size of meta data used by the MsgAdapter for each message
*/
virtual int msg_meta_len() {
return 0;
}

protected:
msg_callback_t_ callback_;
};

};
Loading

0 comments on commit 09ec7b3

Please sign in to comment.