Skip to content

Commit

Permalink
FreeBSD Native Access (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarog committed Jan 17, 2025
1 parent c3911ed commit 6946869
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.nativeaccess;

import org.elasticsearch.nativeaccess.lib.BsdCLibrary;
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;

public class FreebsdNativeAccess extends PosixNativeAccess {

private final BsdCLibrary bsdLibc;
static final int RLIMIT_NPROC = 7;

// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/resource.h#L123
// https://man.freebsd.org/cgi/man.cgi?stat(2)
// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/stat.h#L159
// Offset of st_size: 112 bytes
// Offset of st_blocks: 120 bytes
FreebsdNativeAccess(NativeLibraryProvider libraryProvider) {
super("FreeBSD", libraryProvider,
new PosixConstants(
-1L,
10,
1,
6,
512,
144,
112,
120
)
);

this.bsdLibc = libraryProvider.getLibrary(BsdCLibrary.class);
}

// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/resource.h#L110
@Override
protected long getMaxThreads() {
return getRLimit(RLIMIT_NPROC, "max number of threads");
}

@Override
protected void logMemoryLimitInstructions() {
logger.warn("You can allow ElasticSearch to lock large amounts of RAM by setting the following in /etc/sysctl.conf:");
logger.warn("security.bsd.unprivileged_mlock=1\n");
logger.warn("You can also run the following command to modify the value immediately:");
logger.warn("sysctl security.bsd.unprivileged_mlock=1\n");
logger.warn("When running within a Jail, it's highly advisable to set:");
logger.warn("enforce_statfs = 1");
}

@Override
protected boolean nativePreallocate(int fd, long currentSize, long newSize) {
final int rc = bsdLibc.posix_fallocate(fd, currentSize, newSize - currentSize);
if (rc != 0) {
logger.warn("posix_fallocate failed: " + libc.strerror(libc.errno()));
return false;
}
return true;
}

// https://github.com/elastic/elasticsearch/blob/v8.15.5/server/src/main/java/org/elasticsearch/bootstrap/SystemCallFilter.java#L556
@Override
public void tryInstallExecSandbox() {
PosixCLibrary.RLimit limit = libc.newRLimit();
limit.rlim_cur(0);
limit.rlim_max(0);
if (libc.setrlimit(RLIMIT_NPROC, limit) != 0) {
throw new UnsupportedOperationException("RLIMIT_NPROC unavailable: " + libc.strerror(libc.errno()));
}

logger.debug("FreeBSD RLIMIT_NPROC initialization successful");

execSandboxState = ExecSandboxState.ALL_THREADS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class NativeAccessHolder {
inst = new MacNativeAccess(libProvider);
} else if (os.startsWith("Windows")) {
inst = new WindowsNativeAccess(libProvider);
} else if (os.startsWith("FreeBSD")) {
inst = new FreebsdNativeAccess(libProvider);
} else {
logger.warn("Unsupported OS [" + os + "]. Native methods will be disabled.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ String rlimitToString(long value) {
}

static boolean isNativeVectorLibSupported() {
return Runtime.version().feature() >= 21 && (isMacOrLinuxAarch64() || isLinuxAmd64()) && checkEnableSystemProperty();
return Runtime.version().feature() >= 21 && (isMacOrLinuxAarch64() || isLinuxAmd64() || isFreebsdAmd64()) &&
checkEnableSystemProperty();
}

/**
Expand All @@ -217,6 +218,12 @@ static boolean isMacOrLinuxAarch64() {
return (name.startsWith("Mac") || name.startsWith("Linux")) && System.getProperty("os.arch").equals("aarch64");
}

/** Returns true if the OS is FreeBSD, and the architecture is x64. */
static boolean isFreebsdAmd64() {
String name = System.getProperty("os.name");
return (name.startsWith("FreeBSD")) && System.getProperty("os.arch").equals("amd64");
}

/** -Dorg.elasticsearch.nativeaccess.enableVectorLibrary=false to disable.*/
static final String ENABLE_JDK_VECTOR_LIBRARY = "org.elasticsearch.nativeaccess.enableVectorLibrary";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.nativeaccess.lib;

public non-sealed interface BsdCLibrary extends NativeLibrary {

int posix_fallocate(int fd, long offset, long length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private static Path findPlatformLibDir() {
os = "linux";
} else if (osname.startsWith("Mac OS")) {
os = "darwin";
} else if (osname.startsWith("FreeBSD")) {
os = "freebsd";
} else {
os = "unsupported_os[" + osname + "]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

/** A marker interface for libraries that can be loaded by {@link org.elasticsearch.nativeaccess.lib.NativeLibraryProvider} */
public sealed interface NativeLibrary permits JavaLibrary, PosixCLibrary, LinuxCLibrary, MacCLibrary, Kernel32Library, VectorLibrary,
ZstdLibrary {}
ZstdLibrary, BsdCLibrary {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.nativeaccess.jdk;

import org.elasticsearch.nativeaccess.lib.BsdCLibrary;

import java.lang.foreign.FunctionDescriptor;
import java.lang.invoke.MethodHandle;

import static java.lang.foreign.ValueLayout.JAVA_INT;
import static java.lang.foreign.ValueLayout.JAVA_LONG;
import static org.elasticsearch.nativeaccess.jdk.JdkPosixCLibrary.downcallHandleWithErrno;
import static org.elasticsearch.nativeaccess.jdk.JdkPosixCLibrary.errnoState;

public class JdkFreebsdCLibrary implements BsdCLibrary {

private static final MethodHandle posix_allocate$mh = downcallHandleWithErrno(
"posix_fallocate",
FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_LONG, JAVA_LONG)
);

// https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate
// https://github.com/freebsd/freebsd-src/blob/release/14.2.0/sys/sys/fcntl.h#L390
@Override
public int posix_fallocate(int fd, long offset, long length) {
try {
return (int) posix_allocate$mh.invokeExact(errnoState, fd, offset, length);
} catch (Throwable t) {
throw new AssertionError(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.nativeaccess.jdk;

import org.elasticsearch.nativeaccess.lib.BsdCLibrary;
import org.elasticsearch.nativeaccess.lib.JavaLibrary;
import org.elasticsearch.nativeaccess.lib.Kernel32Library;
import org.elasticsearch.nativeaccess.lib.LinuxCLibrary;
Expand Down Expand Up @@ -39,7 +40,9 @@ public JdkNativeLibraryProvider() {
ZstdLibrary.class,
JdkZstdLibrary::new,
VectorLibrary.class,
JdkVectorLibrary::new
JdkVectorLibrary::new,
BsdCLibrary.class,
JdkFreebsdCLibrary::new
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ class JdkPosixCLibrary implements PosixCLibrary {
private static final MethodHandle fstat$mh;
static {
MethodHandle fstat;
String fstatFunc = "fstat64";
try {
fstat = downcallHandleWithErrno("fstat64", FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS));
if (System.getProperty("os.name").equals("FreeBSD")) {
fstatFunc = "fstat";
}
fstat = downcallHandleWithErrno(fstatFunc, FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS));
} catch (LinkageError e) {
// Due to different sizes of the stat structure for 32 vs 64 bit machines, on some systems fstat actually points to
// an internal symbol. So we fall back to looking for that symbol.
Expand Down

0 comments on commit 6946869

Please sign in to comment.