2014-06-12 14:16:38 +01:00
|
|
|
# Systemd services for openvswitch
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2014-06-12 15:08:49 +01:00
|
|
|
cfg = config.virtualisation.vswitch;
|
2014-06-12 14:16:38 +01:00
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
in {
|
2014-06-12 14:16:38 +01:00
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
options.virtualisation.vswitch = {
|
|
|
|
enable = mkOption {
|
2014-06-12 15:08:49 +01:00
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2015-04-25 14:35:02 +01:00
|
|
|
description = ''
|
|
|
|
Whether to enable Open vSwitch. A configuration daemon (ovs-server)
|
|
|
|
will be started.
|
2014-06-12 15:08:49 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-09-24 11:44:07 +01:00
|
|
|
resetOnStart = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to reset the Open vSwitch configuration database to a default
|
|
|
|
configuration on every start of the systemd <literal>ovsdb.service</literal>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
package = mkOption {
|
2014-06-12 15:08:49 +01:00
|
|
|
type = types.package;
|
|
|
|
default = pkgs.openvswitch;
|
2016-01-17 18:34:55 +00:00
|
|
|
defaultText = "pkgs.openvswitch";
|
2015-04-25 14:35:02 +01:00
|
|
|
description = ''
|
2014-06-12 15:08:49 +01:00
|
|
|
Open vSwitch package to use.
|
2015-04-25 14:35:02 +01:00
|
|
|
'';
|
2014-06-12 15:08:49 +01:00
|
|
|
};
|
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
ipsec = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to start racoon service for openvswitch.
|
2018-12-29 10:10:02 +00:00
|
|
|
Supported only if openvswitch version is less than 2.6.0.
|
|
|
|
Use <literal>virtualisation.vswitch.package = pkgs.openvswitch-lts</literal>
|
|
|
|
for a version that supports ipsec over GRE.
|
2015-04-25 14:35:02 +01:00
|
|
|
'';
|
|
|
|
};
|
2014-06-12 15:08:49 +01:00
|
|
|
};
|
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
config = mkIf cfg.enable (let
|
2014-06-12 15:08:49 +01:00
|
|
|
|
|
|
|
# Where the communication sockets live
|
2018-12-19 21:45:15 +00:00
|
|
|
runDir = "/run/openvswitch";
|
2014-06-12 15:08:49 +01:00
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
# The path to the an initialized version of the database
|
2014-06-12 15:08:49 +01:00
|
|
|
db = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "vswitch.db";
|
2019-06-19 16:45:34 +01:00
|
|
|
dontUnpack = true;
|
2014-06-12 15:08:49 +01:00
|
|
|
buildPhase = "true";
|
|
|
|
buildInputs = with pkgs; [
|
|
|
|
cfg.package
|
|
|
|
];
|
2015-04-25 14:35:02 +01:00
|
|
|
installPhase = "mkdir -p $out";
|
2014-06-12 15:08:49 +01:00
|
|
|
};
|
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
in (mkMerge [{
|
2014-06-12 15:08:49 +01:00
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
environment.systemPackages = [ cfg.package pkgs.ipsecTools ];
|
2014-06-12 15:08:49 +01:00
|
|
|
|
|
|
|
boot.kernelModules = [ "tun" "openvswitch" ];
|
|
|
|
|
|
|
|
boot.extraModulePackages = [ cfg.package ];
|
|
|
|
|
|
|
|
systemd.services.ovsdb = {
|
|
|
|
description = "Open_vSwitch Database Server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "systemd-udev-settle.service" ];
|
|
|
|
path = [ cfg.package ];
|
|
|
|
restartTriggers = [ db cfg.package ];
|
|
|
|
# Create the config database
|
2015-04-25 14:35:02 +01:00
|
|
|
preStart =
|
2014-06-12 15:08:49 +01:00
|
|
|
''
|
|
|
|
mkdir -p ${runDir}
|
|
|
|
mkdir -p /var/db/openvswitch
|
|
|
|
chmod +w /var/db/openvswitch
|
2015-09-24 11:44:07 +01:00
|
|
|
${optionalString cfg.resetOnStart "rm -f /var/db/openvswitch/conf.db"}
|
2014-06-12 15:08:49 +01:00
|
|
|
if [[ ! -e /var/db/openvswitch/conf.db ]]; then
|
|
|
|
${cfg.package}/bin/ovsdb-tool create \
|
|
|
|
"/var/db/openvswitch/conf.db" \
|
|
|
|
"${cfg.package}/share/openvswitch/vswitch.ovsschema"
|
|
|
|
fi
|
|
|
|
chmod -R +w /var/db/openvswitch
|
2018-12-29 10:10:02 +00:00
|
|
|
if ${cfg.package}/bin/ovsdb-tool needs-conversion /var/db/openvswitch/conf.db | grep -q "yes"
|
|
|
|
then
|
|
|
|
echo "Performing database upgrade"
|
|
|
|
${cfg.package}/bin/ovsdb-tool convert /var/db/openvswitch/conf.db
|
|
|
|
else
|
|
|
|
echo "Database already up to date"
|
|
|
|
fi
|
2014-06-12 15:08:49 +01:00
|
|
|
'';
|
2015-04-25 14:35:02 +01:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart =
|
|
|
|
''
|
|
|
|
${cfg.package}/bin/ovsdb-server \
|
|
|
|
--remote=punix:${runDir}/db.sock \
|
|
|
|
--private-key=db:Open_vSwitch,SSL,private_key \
|
|
|
|
--certificate=db:Open_vSwitch,SSL,certificate \
|
|
|
|
--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \
|
|
|
|
--unixctl=ovsdb.ctl.sock \
|
2018-12-19 21:45:15 +00:00
|
|
|
--pidfile=/run/openvswitch/ovsdb.pid \
|
2015-09-24 13:51:44 +01:00
|
|
|
--detach \
|
2015-04-25 14:35:02 +01:00
|
|
|
/var/db/openvswitch/conf.db
|
|
|
|
'';
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 3;
|
2018-12-19 21:45:15 +00:00
|
|
|
PIDFile = "/run/openvswitch/ovsdb.pid";
|
2015-09-24 13:51:44 +01:00
|
|
|
# Use service type 'forking' to correctly determine when ovsdb-server is ready.
|
|
|
|
Type = "forking";
|
2015-04-25 14:35:02 +01:00
|
|
|
};
|
|
|
|
postStart = ''
|
2014-06-12 15:08:49 +01:00
|
|
|
${cfg.package}/bin/ovs-vsctl --timeout 3 --retry --no-wait init
|
2015-04-25 14:35:02 +01:00
|
|
|
'';
|
2014-06-12 15:08:49 +01:00
|
|
|
};
|
|
|
|
|
2018-12-29 10:16:51 +00:00
|
|
|
systemd.services.ovs-vswitchd = {
|
2014-06-12 15:08:49 +01:00
|
|
|
description = "Open_vSwitch Daemon";
|
2015-08-29 17:21:43 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2014-06-12 15:08:49 +01:00
|
|
|
bindsTo = [ "ovsdb.service" ];
|
|
|
|
after = [ "ovsdb.service" ];
|
|
|
|
path = [ cfg.package ];
|
2015-04-25 14:35:02 +01:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = ''
|
|
|
|
${cfg.package}/bin/ovs-vswitchd \
|
2018-12-19 21:45:15 +00:00
|
|
|
--pidfile=/run/openvswitch/ovs-vswitchd.pid \
|
2015-09-24 13:51:44 +01:00
|
|
|
--detach
|
2015-04-25 14:35:02 +01:00
|
|
|
'';
|
2018-12-19 21:45:15 +00:00
|
|
|
PIDFile = "/run/openvswitch/ovs-vswitchd.pid";
|
2015-09-24 13:51:44 +01:00
|
|
|
# Use service type 'forking' to correctly determine when vswitchd is ready.
|
|
|
|
Type = "forking";
|
2018-12-29 10:16:51 +00:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 3;
|
2015-04-25 14:35:02 +01:00
|
|
|
};
|
2014-06-12 14:16:38 +01:00
|
|
|
};
|
|
|
|
|
2015-04-25 14:35:02 +01:00
|
|
|
}
|
2018-12-29 10:10:02 +00:00
|
|
|
(mkIf (cfg.ipsec && (versionOlder cfg.package.version "2.6.0")) {
|
2015-04-25 14:35:02 +01:00
|
|
|
services.racoon.enable = true;
|
|
|
|
services.racoon.configPath = "${runDir}/ipsec/etc/racoon/racoon.conf";
|
|
|
|
|
|
|
|
networking.firewall.extraCommands = ''
|
|
|
|
iptables -I INPUT -t mangle -p esp -j MARK --set-mark 1/1
|
|
|
|
iptables -I INPUT -t mangle -p udp --dport 4500 -j MARK --set-mark 1/1
|
|
|
|
'';
|
|
|
|
|
|
|
|
systemd.services.ovs-monitor-ipsec = {
|
|
|
|
description = "Open_vSwitch Ipsec Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2015-08-29 17:21:43 +01:00
|
|
|
requires = [ "ovsdb.service" ];
|
|
|
|
before = [ "vswitchd.service" "racoon.service" ];
|
2015-04-25 14:35:02 +01:00
|
|
|
environment.UNIXCTLPATH = "/tmp/ovsdb.ctl.sock";
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = ''
|
|
|
|
${cfg.package}/bin/ovs-monitor-ipsec \
|
|
|
|
--root-prefix ${runDir}/ipsec \
|
2018-12-19 21:45:15 +00:00
|
|
|
--pidfile /run/openvswitch/ovs-monitor-ipsec.pid \
|
2015-09-24 13:51:44 +01:00
|
|
|
--monitor --detach \
|
2018-12-19 21:45:15 +00:00
|
|
|
unix:/run/openvswitch/db.sock
|
2015-04-25 14:35:02 +01:00
|
|
|
'';
|
2018-12-19 21:45:15 +00:00
|
|
|
PIDFile = "/run/openvswitch/ovs-monitor-ipsec.pid";
|
2015-09-24 13:51:44 +01:00
|
|
|
# Use service type 'forking' to correctly determine when ovs-monitor-ipsec is ready.
|
|
|
|
Type = "forking";
|
2015-04-25 14:35:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
rm -r ${runDir}/ipsec/etc/racoon/certs || true
|
|
|
|
mkdir -p ${runDir}/ipsec/{etc/racoon,etc/init.d/,usr/sbin/}
|
|
|
|
ln -fs ${pkgs.ipsecTools}/bin/setkey ${runDir}/ipsec/usr/sbin/setkey
|
|
|
|
ln -fs ${pkgs.writeScript "racoon-restart" ''
|
2018-03-01 19:38:53 +00:00
|
|
|
#!${pkgs.runtimeShell}
|
2018-12-19 21:45:15 +00:00
|
|
|
/run/current-system/sw/bin/systemctl $1 racoon
|
2015-04-25 14:35:02 +01:00
|
|
|
''} ${runDir}/ipsec/etc/init.d/racoon
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})]));
|
2018-12-29 10:16:51 +00:00
|
|
|
|
|
|
|
meta.maintainers = with maintainers; [ netixx ];
|
|
|
|
|
2014-06-12 14:16:38 +01:00
|
|
|
}
|