Initial RPS CUDA implementation
This commit is contained in:
commit
8d583036d0
33 changed files with 2968 additions and 0 deletions
101
common/config.hpp
Normal file
101
common/config.hpp
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
#pragma once
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
using u32 = uint32_t;
|
||||
using u64 = uint64_t;
|
||||
using Json = nlohmann::json;
|
||||
inline void require(bool ok, const char *why) {
|
||||
if (!ok)
|
||||
throw std::runtime_error(why);
|
||||
}
|
||||
struct Config {
|
||||
u32 w, h, n, g, rate, x, y, rw, rh, k;
|
||||
u64 seed;
|
||||
u32 aw() const { return rw / k; }
|
||||
u32 ah() const { return rh / k; }
|
||||
size_t bytes() const { return size_t(aw()) * ah() * 4; }
|
||||
};
|
||||
inline std::string readConfig(const std::string &path) {
|
||||
std::ifstream in(path, std::ios::binary | std::ios::ate);
|
||||
auto n = in.tellg();
|
||||
require(n > 0 && n <= 65536, "config file missing, empty or oversized");
|
||||
in.seekg(0);
|
||||
std::string bytes(size_t(n), '\0');
|
||||
require(bool(in.read(bytes.data(), n)), "config read failed");
|
||||
return bytes;
|
||||
}
|
||||
inline Config parse(const std::string &bytes) {
|
||||
require(!bytes.empty() && bytes.size() <= 65536, "JSON length");
|
||||
require(bytes.find(char(0)) == bytes.npos, "embedded NUL in config");
|
||||
std::vector<std::set<std::string>> scopes;
|
||||
auto root =
|
||||
Json::parse(bytes, [&](int depth, Json::parse_event_t e, Json &v) {
|
||||
require(depth <= 4, "unexpected JSON nesting");
|
||||
using E = Json::parse_event_t;
|
||||
if (e == E::object_start)
|
||||
scopes.emplace_back();
|
||||
if (e == E::key)
|
||||
require(scopes.back().insert(v.get<std::string>()).second,
|
||||
"duplicate key");
|
||||
if (e == E::object_end)
|
||||
scopes.pop_back();
|
||||
return true;
|
||||
});
|
||||
auto keys = [](const Json &j, std::initializer_list<const char *> names) {
|
||||
require(j.is_object() && j.size() == names.size(),
|
||||
"unexpected object fields");
|
||||
for (auto name : names)
|
||||
require(j.contains(name), "missing field");
|
||||
};
|
||||
auto num = [](const Json &v) -> u32 {
|
||||
require(v.is_number_unsigned() && v.get<u64>() <= UINT32_MAX,
|
||||
"expected uint32");
|
||||
return v.get<u32>();
|
||||
};
|
||||
keys(root, {"protocol_version", "simulation", "view", "snapshots"});
|
||||
auto &s = root.at("simulation");
|
||||
auto &v = root.at("view");
|
||||
keys(s, {"width", "height", "neighbors", "seed", "generations",
|
||||
"max_generations_per_second", "topology"});
|
||||
keys(v, {"roi", "aggregation_side", "palette"});
|
||||
auto &r = v.at("roi");
|
||||
keys(r, {"x", "y", "width", "height"});
|
||||
keys(root.at("snapshots"), {"mode"});
|
||||
require(num(root.at("protocol_version")) == 1 &&
|
||||
s.at("topology") == "torus" && v.at("palette") == "rps-v1" &&
|
||||
root.at("snapshots").at("mode") == "auto",
|
||||
"unsupported configuration");
|
||||
auto seed = s.at("seed").get<std::string>();
|
||||
Config c{num(s.at("width")),
|
||||
num(s.at("height")),
|
||||
num(s.at("neighbors")),
|
||||
num(s.at("generations")),
|
||||
num(s.at("max_generations_per_second")),
|
||||
num(r.at("x")),
|
||||
num(r.at("y")),
|
||||
num(r.at("width")),
|
||||
num(r.at("height")),
|
||||
num(v.at("aggregation_side")),
|
||||
0};
|
||||
auto end = seed.data() + seed.size();
|
||||
auto result = std::from_chars(seed.data(), end, c.seed);
|
||||
require(result.ec == std::errc() && result.ptr == end &&
|
||||
seed == std::to_string(c.seed),
|
||||
"invalid seed");
|
||||
require(c.w && c.h && c.w % 1024 == 0 && c.h % 48 == 0 &&
|
||||
u64(c.w) * c.h / 32 <= UINT32_MAX,
|
||||
"grid shape");
|
||||
require((c.n == 4 || c.n == 8) && c.g >= 1 && c.g <= 1000000000 &&
|
||||
c.rate >= 1 && c.rate <= 240,
|
||||
"simulation bounds");
|
||||
require(c.k && c.k <= 16 && !(c.k & (c.k - 1)), "aggregation side");
|
||||
require(c.rw && c.rh && u64(c.x) + c.rw <= c.w && u64(c.y) + c.rh <= c.h &&
|
||||
!(c.x % c.k || c.y % c.k || c.rw % c.k || c.rh % c.k),
|
||||
"ROI");
|
||||
require(u64(c.aw()) * c.ah() <= 8388608, "frame size");
|
||||
return c;
|
||||
}
|
||||
52
common/socks.hpp
Normal file
52
common/socks.hpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#pragma once
|
||||
#include "wire.hpp"
|
||||
#include <cerrno>
|
||||
inline void rawIO(int fd, void *data, size_t n, bool write) {
|
||||
auto p = static_cast<unsigned char *>(data);
|
||||
while (n) {
|
||||
auto m = write ? send(fd, p, n, 0) : recv(fd, p, n, 0);
|
||||
if (m < 0 && errno == EINTR)
|
||||
continue;
|
||||
require(m > 0, "SOCKS5 I/O failed");
|
||||
p += m;
|
||||
n -= m;
|
||||
}
|
||||
}
|
||||
// A domain is passed to the SOCKS proxy so the SSH server resolves private DNS.
|
||||
inline void socks(int fd, unsigned proxyPort, unsigned serverPort,
|
||||
const std::string &destination = "127.0.0.1") {
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = htons(proxyPort);
|
||||
require(connect(fd, (sockaddr *)&addr, sizeof(addr)) == 0,
|
||||
"local SSH SOCKS5 unavailable");
|
||||
unsigned char hello[] = {5, 1, 0}, reply[260]{};
|
||||
rawIO(fd, hello, 3, true);
|
||||
rawIO(fd, reply, 2, false);
|
||||
require(reply[0] == 5 && reply[1] == 0, "SOCKS5 negotiation failed");
|
||||
require(!destination.empty() && destination.size() <= 253, "SOCKS5 destination length");
|
||||
Bytes request{5, 1, 0, 3, static_cast<unsigned char>(destination.size())};
|
||||
request.insert(request.end(), destination.begin(), destination.end());
|
||||
put(request, serverPort, 2);
|
||||
rawIO(fd, request.data(), request.size(), true);
|
||||
rawIO(fd, reply, 4, false);
|
||||
require(reply[0] == 5 && !reply[1] && !reply[2],
|
||||
"SSH proxy could not reach RPS listener");
|
||||
size_t n;
|
||||
switch (reply[3]) {
|
||||
case 1:
|
||||
n = 4;
|
||||
break;
|
||||
case 4:
|
||||
n = 16;
|
||||
break;
|
||||
case 3:
|
||||
rawIO(fd, reply, 1, false);
|
||||
n = *reply;
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("SOCKS5 address type");
|
||||
}
|
||||
rawIO(fd, reply, n + 2, false);
|
||||
}
|
||||
122
common/wire.hpp
Normal file
122
common/wire.hpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#pragma once
|
||||
#include "config.hpp"
|
||||
#include <arpa/inet.h>
|
||||
#include <memory>
|
||||
#include <netinet/in.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
using Bytes = std::vector<unsigned char>;
|
||||
using Context = std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>;
|
||||
using Session = std::unique_ptr<SSL, decltype(&SSL_free)>;
|
||||
struct FD {
|
||||
int fd;
|
||||
explicit FD(int s) : fd(s) { require(s >= 0, "socket failed"); }
|
||||
FD(const FD &) = delete;
|
||||
~FD() { close(fd); }
|
||||
};
|
||||
inline void sslok(int rc, const char *op) {
|
||||
if (rc != 1)
|
||||
throw std::runtime_error(std::string(op) + " / " +
|
||||
ERR_error_string(ERR_get_error(), nullptr));
|
||||
}
|
||||
inline Context context(bool client) {
|
||||
Context ctx(SSL_CTX_new(client ? TLS_client_method() : TLS_server_method()),
|
||||
SSL_CTX_free);
|
||||
require(bool(ctx), "TLS context");
|
||||
sslok(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_3_VERSION),
|
||||
"TLS minimum");
|
||||
sslok(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_3_VERSION),
|
||||
"TLS maximum");
|
||||
return ctx;
|
||||
}
|
||||
inline Session session(SSL_CTX *ctx, int fd) {
|
||||
Session s(SSL_new(ctx), SSL_free);
|
||||
require(bool(s), "TLS allocation");
|
||||
sslok(SSL_set_fd(s.get(), fd), "TLS socket");
|
||||
return s;
|
||||
}
|
||||
enum Type { CONFIG = 1, ACCEPTED, FRAME, ACK, FINISHED, ERROR_MSG };
|
||||
inline void put(Bytes &b, u64 v, int width) {
|
||||
for (int i = width - 1; i >= 0; --i)
|
||||
b.push_back(v >> (8 * i));
|
||||
}
|
||||
inline u64 get(const unsigned char *p, int width) {
|
||||
u64 v = 0;
|
||||
while (width--)
|
||||
v = (v << 8) | *p++;
|
||||
return v;
|
||||
}
|
||||
inline void sendBytes(SSL *s, const void *data, size_t n) {
|
||||
auto p = static_cast<const unsigned char *>(data);
|
||||
while (n) {
|
||||
size_t m = 0;
|
||||
sslok(SSL_write_ex(s, p, n, &m), "TLS write");
|
||||
p += m;
|
||||
n -= m;
|
||||
}
|
||||
}
|
||||
inline void recvBytes(SSL *s, void *data, size_t n) {
|
||||
auto p = static_cast<unsigned char *>(data);
|
||||
while (n) {
|
||||
size_t m = 0;
|
||||
sslok(SSL_read_ex(s, p, n, &m), "TLS read");
|
||||
p += m;
|
||||
n -= m;
|
||||
}
|
||||
}
|
||||
inline void header(SSL *s, int type, u64 n) {
|
||||
Bytes b;
|
||||
put(b, 0x52505331, 4);
|
||||
put(b, 1, 2);
|
||||
put(b, type, 2);
|
||||
put(b, n, 8);
|
||||
sendBytes(s, b.data(), 16);
|
||||
}
|
||||
inline void sendMsg(SSL *s, int type, const Bytes &b) {
|
||||
header(s, type, b.size());
|
||||
sendBytes(s, b.data(), b.size());
|
||||
}
|
||||
inline std::pair<int, Bytes> receive(SSL *s,
|
||||
std::initializer_list<int> allowed) {
|
||||
unsigned char h[16];
|
||||
recvBytes(s, h, 16);
|
||||
int t = get(h + 6, 2);
|
||||
u64 n = get(h + 8, 8);
|
||||
require(get(h, 4) == 0x52505331 && get(h + 4, 2) == 1 &&
|
||||
std::find(allowed.begin(), allowed.end(), t) != allowed.end(),
|
||||
"unexpected header");
|
||||
require(((t == CONFIG || t == ACCEPTED) && n && n <= 65536) ||
|
||||
(t == FRAME && n >= 36 && n <= 33554464) ||
|
||||
(t == ACK && n == 8) || (t == FINISHED && n == 12) ||
|
||||
(t == ERROR_MSG && n >= 4 && n <= 4096),
|
||||
"invalid payload length");
|
||||
Bytes b(n);
|
||||
recvBytes(s, b.data(), n);
|
||||
if (t == ERROR_MSG)
|
||||
throw std::runtime_error(std::string(b.begin() + 4, b.end()));
|
||||
return {t, std::move(b)};
|
||||
}
|
||||
struct HashException : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
inline Bytes digest(const Bytes &bytes) {
|
||||
Bytes out(32);
|
||||
unsigned len = 0;
|
||||
if (EVP_Digest(bytes.data(), bytes.size(), out.data(), &len, EVP_sha256(),
|
||||
nullptr) != 1 ||
|
||||
len != 32)
|
||||
throw HashException("SHA256 failed");
|
||||
return out;
|
||||
}
|
||||
inline u64 hash64(u64 seed) {
|
||||
const char prefix[] = "RPS-PRIORITY-v1";
|
||||
Bytes msg(prefix, prefix + sizeof(prefix));
|
||||
put(msg, seed, 8);
|
||||
return get(digest(msg).data(), 8);
|
||||
}
|
||||
|
||||
inline int priority(u64 seed) { return hash64(seed) % 6; }
|
||||
Loading…
Add table
Add a link
Reference in a new issue