Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): Add C ZeroMQ server example #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/zmq-server-c/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM debian:bookworm AS build

RUN set -ex; \
apt-get update; \
apt-get install -y build-essential libzmq3-dev; \
ldconfig

WORKDIR /src

COPY main.c .

RUN gcc main.c -o zmq-server -Wall -Os -lzmq

FROM scratch

COPY --from=build /src/zmq-server /usr/bin/zmq-server
COPY --from=build /lib/x86_64-linux-gnu/libzmq.so.5 /lib/x86_64-linux-gnu/libzmq.so.5
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
COPY --from=build /lib/x86_64-linux-gnu/libbsd.so.0 /lib/x86_64-linux-gnu/libbsd.so.0
COPY --from=build /lib/x86_64-linux-gnu/libsodium.so.23 /lib/x86_64-linux-gnu/libsodium.so.23
COPY --from=build /lib/x86_64-linux-gnu/libpgm-5.3.so.0 /lib/x86_64-linux-gnu/libpgm-5.3.so.0
COPY --from=build /lib/x86_64-linux-gnu/libnorm.so.1 /lib/x86_64-linux-gnu/libnorm.so.1
COPY --from=build /lib/x86_64-linux-gnu/libgssapi_krb5.so.2 /lib/x86_64-linux-gnu/libgssapi_krb5.so.2
COPY --from=build /lib/x86_64-linux-gnu/libstdc++.so.6 /lib/x86_64-linux-gnu/libstdc++.so.6
COPY --from=build /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/libgcc_s.so.1
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
COPY --from=build /lib/x86_64-linux-gnu/libmd.so.0 /lib/x86_64-linux-gnu/libmd.so.0
COPY --from=build /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0
COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6
COPY --from=build /lib/x86_64-linux-gnu/libkrb5.so.3 /lib/x86_64-linux-gnu/libkrb5.so.3
COPY --from=build /lib/x86_64-linux-gnu/libk5crypto.so.3 /lib/x86_64-linux-gnu/libk5crypto.so.3
COPY --from=build /lib/x86_64-linux-gnu/libcom_err.so.2 /lib/x86_64-linux-gnu/libcom_err.so.2
COPY --from=build /lib/x86_64-linux-gnu/libkrb5support.so.0 /lib/x86_64-linux-gnu/libkrb5support.so.0
COPY --from=build /lib/x86_64-linux-gnu/libkeyutils.so.1 /lib/x86_64-linux-gnu/libkeyutils.so.1
COPY --from=build /lib/x86_64-linux-gnu/libresolv.so.2 /lib/x86_64-linux-gnu/libresolv.so.2
7 changes: 7 additions & 0 deletions examples/zmq-server-c/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spec: v0.6

runtime: base:latest

rootfs: ./Dockerfile

cmd: ["/usr/bin/zmq-server"]
72 changes: 72 additions & 0 deletions examples/zmq-server-c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# C ZeroMQ Request/Reply Server

This directory contains a C ZeroMQ Request/Reply server running on Unikraft.

## Set Up

To run this example, [install Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli), clone this repository and `cd` into this directory.

## Run and Use

Use `kraft` to run the image and start a Unikraft instance:

```bash
kraft run --rm -p 5000:5000 --plat qemu --arch x86_64 .
```

If the `--plat` argument is left out, it defaults to `qemu`.
If the `--arch` argument is left out, it defaults to your system's CPU architecture.

Once executed, it will open port `5000` and wait for connections. To test it you
can install the `pyzmq` Pythoon package and use the following Python code to
connect:

```python
import zmq

ctx = zmq.Context()
sock = ctx.socket(zmq.REQ)
sock.connect("tcp://localhost:5000")
sock.send(b'Hello, World!")
print(sock.recv())
```

You should see a "Hello, World!" message in the ZeroMQ console and an "ACK"
message in your Python script output.

## Inspect and Close

To list information about the Unikraft instance, use:

```bash
kraft ps
```

```text
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
infallible_massa oci://unikraft.org/base:latest /usr/bin/zmq-server 1 seconds ago running 64M 0.0.0.0:5000->5000/tcp qemu/x86_64
```

The instance name is `infallible_massa`.
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run:

```bash
kraft rm infallible_massa
```

Note that depending on how you modify this example your instance **may** need more memory to run.
To do so, use the `kraft run`'s `-M` flag, for example:

```bash
kraft run --rm -p 5000:5000 --plat qemu --arch x86_64 -M 256M .
```

## `kraft` and `sudo`

Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior.
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless).

## Learn More

- [How to run unikernels locally](https://unikraft.org/docs/cli/running)
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit)
28 changes: 28 additions & 0 deletions examples/zmq-server-c/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <zmq.h>

#define BUF_SIZE 256

int main()
{
void *ctx, *sock;
char buff[BUF_SIZE];

ctx = zmq_ctx_new();
sock = zmq_socket(ctx, ZMQ_REP);

if (zmq_bind(sock, "tcp://*:5000")) {
fputs("could not bind to port", stderr);
return -1;
}

while (1) {
memset(buff, 0, BUF_SIZE);
zmq_recv(sock, buff, BUF_SIZE - 1, 0);
printf("Received message: %s\n", buff);
zmq_send(sock, "ACK", 3, 0);
}

return 0;
}