implementing foundations of xeon phi daemon
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
#include <Daemon.h>
|
||||
#include <Logger.h>
|
||||
#include <Memory.h>
|
||||
|
||||
static int HandleHello(scif_epd_t endpoint, const PhiMessageHeader* header)
|
||||
{
|
||||
PhiHelloRequest request;
|
||||
PhiHelloReply reply = {
|
||||
.result = {
|
||||
.status = PHI_STATUS_OK,
|
||||
.reserved = 0,
|
||||
},
|
||||
.device_protocol_version = PHI_PROTOCOL_VERSION,
|
||||
.pointer_bits = (uint32_t)(sizeof(void *) * 8u),
|
||||
};
|
||||
|
||||
if(header->payload_size != sizeof(request))
|
||||
{
|
||||
if(DrainPayload(endpoint, header->payload_size) < 0)
|
||||
return -1;
|
||||
reply.result.status = PHI_STATUS_BAD_MESSAGE;
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
|
||||
if(ReadAll(endpoint, &request, sizeof(request)) < 0)
|
||||
return -1;
|
||||
|
||||
if(request.host_protocol_version != PHI_PROTOCOL_VERSION)
|
||||
reply.result.status = PHI_STATUS_UNSUPPORTED_VERSION;
|
||||
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
|
||||
scif_epd_t StartDaemon()
|
||||
{
|
||||
PhiLogInfo("Starting the daemon...");
|
||||
|
||||
scif_epd_t endpoint = scif_open();
|
||||
if(endpoint < 0)
|
||||
{
|
||||
PhiLogError("Failed to create SCIF endpoint");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(scif_bind(endpoint, PHI_SCIF_PORT) < 0)
|
||||
{
|
||||
PhiLogError("Failed to bind SCIF port");
|
||||
scif_close(endpoint);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(scif_listen(endpoint, 1) < 0)
|
||||
{
|
||||
PhiLogError("Could not listen to SCIF port");
|
||||
scif_close(endpoint);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
void ShutdownDaemon(scif_epd_t endpoint)
|
||||
{
|
||||
PhiLogInfo("Shuting down the daemon...");
|
||||
scif_close(endpoint);
|
||||
}
|
||||
|
||||
int HandlePacket(scif_epd_t endpoint)
|
||||
{
|
||||
for(;;)
|
||||
{
|
||||
PhiMessageHeader header;
|
||||
|
||||
if(ReadAll(endpoint, &header, sizeof(header)) < 0)
|
||||
return -1;
|
||||
|
||||
if(header.magic != PHI_PROTOCOL_MAGIC || header.version != PHI_PROTOCOL_VERSION)
|
||||
{
|
||||
if(DrainPayload(endpoint, header.payload_size) < 0)
|
||||
return -1;
|
||||
if(SendStatus(endpoint, &header, PHI_STATUS_BAD_MESSAGE) < 0)
|
||||
return -1;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch((PhiCommandType)header.type)
|
||||
{
|
||||
case PHI_COMMAND_HELLO:
|
||||
if(HandleHello(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_COMMAND_ALLOC_MEMORY:
|
||||
if(HandleAllocMemory(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_COMMAND_FREE_MEMORY:
|
||||
if(HandleFreeMemory(endpoint, &header) < 0)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case PHI_COMMAND_SHUTDOWN:
|
||||
if(DrainPayload(endpoint, header.payload_size) < 0)
|
||||
return -1;
|
||||
if(SendStatus(endpoint, &header, PHI_STATUS_OK) < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
|
||||
default:
|
||||
if(DrainPayload(endpoint, header.payload_size) < 0)
|
||||
return -1;
|
||||
if(SendStatus(endpoint, &header, PHI_STATUS_UNSUPPORTED_COMMAND) < 0)
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ReadAll(scif_epd_t endpoint, void* data, size_t size)
|
||||
{
|
||||
uint8_t* bytes = data;
|
||||
size_t offset = 0;
|
||||
|
||||
while(offset < size)
|
||||
{
|
||||
int got = scif_recv(endpoint, bytes + offset, size - offset, SCIF_RECV_BLOCK);
|
||||
if(got <= 0)
|
||||
return -1;
|
||||
offset += (size_t)got;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WriteAll(scif_epd_t endpoint, const void* data, size_t size)
|
||||
{
|
||||
const uint8_t* bytes = data;
|
||||
size_t offset = 0;
|
||||
|
||||
while(offset < size)
|
||||
{
|
||||
int sent = scif_send(endpoint, (void*)(bytes + offset), size - offset, SCIF_SEND_BLOCK);
|
||||
if(sent <= 0)
|
||||
return -1;
|
||||
offset += (size_t)sent;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendReply(scif_epd_t endpoint, const PhiMessageHeader* request, const void* payload, uint64_t payload_size)
|
||||
{
|
||||
PhiMessageHeader reply = {
|
||||
.magic = PHI_PROTOCOL_MAGIC,
|
||||
.version = PHI_PROTOCOL_VERSION,
|
||||
.type = request->type,
|
||||
.sequence = request->sequence,
|
||||
.payload_size = payload_size,
|
||||
};
|
||||
|
||||
if(WriteAll(endpoint, &reply, sizeof(reply)) < 0)
|
||||
return -1;
|
||||
|
||||
return WriteAll(endpoint, payload, (size_t)payload_size);
|
||||
}
|
||||
|
||||
int SendStatus(scif_epd_t endpoint, const PhiMessageHeader* request, PhiStatus status)
|
||||
{
|
||||
PhiFreeMemoryReply reply = {
|
||||
.result = {
|
||||
.status = status,
|
||||
.reserved = 0,
|
||||
},
|
||||
};
|
||||
|
||||
return SendReply(endpoint, request, &reply, sizeof(reply));
|
||||
}
|
||||
|
||||
int DrainPayload(scif_epd_t endpoint, uint64_t size)
|
||||
{
|
||||
uint8_t buffer[256];
|
||||
|
||||
while(size > 0)
|
||||
{
|
||||
size_t chunk = size < sizeof(buffer) ? (size_t)size : sizeof(buffer);
|
||||
if(ReadAll(endpoint, buffer, chunk) < 0)
|
||||
return -1;
|
||||
size -= chunk;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef APE_PHI_DAEMON_H
|
||||
#define APE_PHI_DAEMON_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <scif.h>
|
||||
|
||||
#include <Protocol.h>
|
||||
|
||||
scif_epd_t StartDaemon();
|
||||
void ShutdownDaemon(scif_epd_t endpoint);
|
||||
|
||||
int DrainPayload(scif_epd_t endpoint, uint64_t size);
|
||||
int HandlePacket(scif_epd_t endpoint);
|
||||
int ReadAll(scif_epd_t endpoint, void* data, size_t size);
|
||||
int SendReply(scif_epd_t endpoint, const PhiMessageHeader* request, const void* payload, uint64_t payload_size);
|
||||
int SendStatus(scif_epd_t endpoint, const PhiMessageHeader* request, PhiStatus status);
|
||||
int WriteAll(scif_epd_t endpoint, const void* data, size_t size);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,93 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <Logger.h>
|
||||
|
||||
#define RED 31
|
||||
#define GREEN 32
|
||||
#define BLUE 34
|
||||
#define DEF 0
|
||||
#define BLACK 30
|
||||
#define YELLOW 33
|
||||
#define MAGENTA 35
|
||||
#define CYAN 36
|
||||
#define WHITE 37
|
||||
#define BG_RED 41
|
||||
#define BG_GREEN 42
|
||||
#define BG_BLUE 44
|
||||
#define BG_DEF 0
|
||||
#define BG_BLACK 40
|
||||
#define BG_YELLOW 43
|
||||
#define BG_MAGENTA 45
|
||||
#define BG_CYAN 46
|
||||
#define BG_WHITE 47
|
||||
#define RESET 0
|
||||
#define BOLD 1
|
||||
#define UNDERLINE 4
|
||||
#define INVERSE 7
|
||||
#define BOLD_OFF 21
|
||||
#define UNDERLINE_OFF 24
|
||||
#define INVERSE_OFF 27
|
||||
|
||||
inline static void SetConsoleColor(FILE* file, int code)
|
||||
{
|
||||
fprintf(file, "\033[1;%dm", code);
|
||||
}
|
||||
|
||||
void PhiLog(PhiLogLevel level, const char* fmt, const char* file, const char* function, int line, ...)
|
||||
{
|
||||
time_t now = time(0);
|
||||
struct tm tstruct = *localtime(&now);
|
||||
char buffer[128];
|
||||
strftime(buffer, sizeof(buffer), "[%X] ", &tstruct);
|
||||
|
||||
FILE* out = stdout;
|
||||
|
||||
if(level != PHI_LOG_LEVEL_INFO)
|
||||
out = stderr;
|
||||
|
||||
// Way too much printf calls
|
||||
SetConsoleColor(out, MAGENTA);
|
||||
fprintf(out, "[Phi device ");
|
||||
SetConsoleColor(out, GREEN);
|
||||
fprintf(out, "Phi");
|
||||
SetConsoleColor(out, YELLOW);
|
||||
fprintf(out, "%s", buffer);
|
||||
SetConsoleColor(out, MAGENTA);
|
||||
fputc(']', out);
|
||||
|
||||
switch(level)
|
||||
{
|
||||
case PHI_LOG_LEVEL_INFO:
|
||||
SetConsoleColor(out, BLUE);
|
||||
fprintf(out, "[info] ");
|
||||
break;
|
||||
case PHI_LOG_LEVEL_WARN:
|
||||
SetConsoleColor(out, MAGENTA);
|
||||
fprintf(out, "[warn] ");
|
||||
break;
|
||||
case PHI_LOG_LEVEL_ERR:
|
||||
case PHI_LOG_LEVEL_FATAL:
|
||||
SetConsoleColor(out, RED);
|
||||
fprintf(out, "[err] ");
|
||||
break;
|
||||
}
|
||||
|
||||
va_list argptr;
|
||||
va_start(argptr, line);
|
||||
|
||||
SetConsoleColor(out, RESET);
|
||||
fprintf(out, fmt, argptr);
|
||||
fputc('\n', out);
|
||||
|
||||
if(level == PHI_LOG_LEVEL_FATAL)
|
||||
{
|
||||
SetConsoleColor(out, BG_RED);
|
||||
fprintf(out, "Fatal Error: emergency exit\n");
|
||||
SetConsoleColor(out, BG_DEF);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef APE_PHI_LOGGER_H
|
||||
#define APE_PHI_LOGGER_H
|
||||
|
||||
typedef enum PhiLogLevel
|
||||
{
|
||||
PHI_LOG_LEVEL_INFO = 0,
|
||||
PHI_LOG_LEVEL_WARN = 1,
|
||||
PHI_LOG_LEVEL_ERR = 2,
|
||||
PHI_LOG_LEVEL_FATAL = 3,
|
||||
} PhiLogLevel;
|
||||
|
||||
void PhiLog(PhiLogLevel level, const char* fmt, const char* file, const char* function, int line, ...);
|
||||
|
||||
#define PhiLogError(msg) PhiLog(PHI_LOG_LEVEL_ERR, msg, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define PhiLogWarning(msg) PhiLog(PHI_LOG_LEVEL_WARN, msg, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define PhiLogInfo(msg) PhiLog(PHI_LOG_LEVEL_INFO, msg, __FILE__, __FUNCTION__, __LINE__)
|
||||
#define PhiLogFatal(msg) PhiLog(PHI_LOG_LEVEL_FATAL, msg, __FILE__, __FUNCTION__, __LINE__)
|
||||
|
||||
#define PhiLogErrorFmt(msg, ...) PhiLog(PHI_LOG_LEVEL_ERR, msg, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
|
||||
#define PhiLogWarningFmt(msg, ...) PhiLog(PHI_LOG_LEVEL_WARN, msg, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
|
||||
#define PhiLogInfoFmt(msg, ...) PhiLog(PHI_LOG_LEVEL_INFO, msg, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
|
||||
#define PhiLogFatalFmt(msg, ...) PhiLog(PHI_LOG_LEVEL_FATAL, msg, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <Logger.h>
|
||||
#include <Memory.h>
|
||||
|
||||
int HandleAllocMemory(scif_epd_t endpoint, const PhiMessageHeader* header)
|
||||
{
|
||||
PhiAllocMemoryRequest request;
|
||||
PhiAllocMemoryReply reply = {
|
||||
.result = {
|
||||
.status = PHI_STATUS_OK,
|
||||
.reserved = 0,
|
||||
},
|
||||
.remote_handle = 0,
|
||||
.size = 0,
|
||||
};
|
||||
void* memory;
|
||||
|
||||
if(header->payload_size != sizeof(request))
|
||||
{
|
||||
if(DrainPayload(endpoint, header->payload_size) < 0)
|
||||
return -1;
|
||||
reply.result.status = PHI_STATUS_BAD_MESSAGE;
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
if(ReadAll(endpoint, &request, sizeof(request)) < 0)
|
||||
return -1;
|
||||
|
||||
memory = calloc(1, (size_t)request.size);
|
||||
|
||||
if(memory == NULL)
|
||||
reply.result.status = PHI_STATUS_OUT_OF_MEMORY;
|
||||
else
|
||||
{
|
||||
reply.remote_handle = (uint64_t)(uintptr_t)memory;
|
||||
reply.size = request.size;
|
||||
PhiLogInfoFmt("Allocated %zu bytes", (size_t)request.size);
|
||||
}
|
||||
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
|
||||
int HandleFreeMemory(scif_epd_t endpoint, const PhiMessageHeader* header)
|
||||
{
|
||||
PhiFreeMemoryRequest request;
|
||||
PhiFreeMemoryReply reply = {
|
||||
.result = {
|
||||
.status = PHI_STATUS_OK,
|
||||
.reserved = 0,
|
||||
},
|
||||
};
|
||||
|
||||
if(header->payload_size != sizeof(request))
|
||||
{
|
||||
if(DrainPayload(endpoint, header->payload_size) < 0)
|
||||
return -1;
|
||||
reply.result.status = PHI_STATUS_BAD_MESSAGE;
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
if(ReadAll(endpoint, &request, sizeof(request)) < 0)
|
||||
return -1;
|
||||
|
||||
if(request.remote_handle == 0)
|
||||
{
|
||||
reply.result.status = PHI_STATUS_INVALID_HANDLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
free((void*)(uintptr_t)request.remote_handle);
|
||||
}
|
||||
|
||||
return SendReply(endpoint, header, &reply, sizeof(reply));
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef APE_PHI_MEMORY_H
|
||||
#define APE_PHI_MEMORY_H
|
||||
|
||||
#include <Daemon.h>
|
||||
|
||||
int HandleAllocMemory(scif_epd_t endpoint, const PhiMessageHeader* header);
|
||||
int HandleFreeMemory(scif_epd_t endpoint, const PhiMessageHeader* header);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <Daemon.h>
|
||||
#include <Logger.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
scif_epd_t endpoint = StartDaemon();
|
||||
|
||||
if(endpoint == 0)
|
||||
return 1;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
struct scif_portID peer;
|
||||
scif_epd_t client;
|
||||
|
||||
if(scif_accept(endpoint, &peer, &client, SCIF_ACCEPT_SYNC) < 0)
|
||||
{
|
||||
if(errno == EINTR)
|
||||
continue;
|
||||
PhiLogError("Could not accept SCIF connection");
|
||||
break;
|
||||
}
|
||||
|
||||
(void)HandlePacket(client);
|
||||
|
||||
scif_close(client);
|
||||
}
|
||||
|
||||
ShutdownDaemon(endpoint);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user