summary refs log tree commit diff
path: root/nixos/modules/services/networking/ucarp.nix
blob: 189e4f99cefec554953b3589cee59500729d4d69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.networking.ucarp;

  ucarpExec = concatStringsSep " " (
    [
      "${cfg.package}/bin/ucarp"
      "--interface=${cfg.interface}"
      "--srcip=${cfg.srcIp}"
      "--vhid=${toString cfg.vhId}"
      "--passfile=${cfg.passwordFile}"
      "--addr=${cfg.addr}"
      "--advbase=${toString cfg.advBase}"
      "--advskew=${toString cfg.advSkew}"
      "--upscript=${cfg.upscript}"
      "--downscript=${cfg.downscript}"
      "--deadratio=${toString cfg.deadratio}"
    ]
    ++ (optional cfg.preempt "--preempt")
    ++ (optional cfg.neutral "--neutral")
    ++ (optional cfg.shutdown "--shutdown")
    ++ (optional cfg.ignoreIfState "--ignoreifstate")
    ++ (optional cfg.noMcast "--nomcast")
    ++ (optional (cfg.extraParam != null) "--xparam=${cfg.extraParam}")
  );
in {
  options.networking.ucarp = {
    enable = mkEnableOption "ucarp, userspace implementation of CARP";

    interface = mkOption {
      type = types.str;
      description = "Network interface to bind to.";
      example = "eth0";
    };

    srcIp = mkOption {
      type = types.str;
      description = "Source (real) IP address of this host.";
    };

    vhId = mkOption {
      type = types.ints.between 1 255;
      description = "Virtual IP identifier shared between CARP hosts.";
      example = 1;
    };

    passwordFile = mkOption {
      type = types.str;
      description = "File containing shared password between CARP hosts.";
      example = "/run/keys/ucarp-password";
    };

    preempt = mkOption {
      type = types.bool;
      description = ''
        Enable preemptive failover.
        Thus, this host becomes the CARP master as soon as possible.
      '';
      default = false;
    };

    neutral = mkOption {
      type = types.bool;
      description = "Do not run downscript at start if the host is the backup.";
      default = false;
    };

    addr = mkOption {
      type = types.str;
      description = "Virtual shared IP address.";
    };

    advBase = mkOption {
      type = types.ints.unsigned;
      description = "Advertisement frequency in seconds.";
      default = 1;
    };

    advSkew = mkOption {
      type = types.ints.unsigned;
      description = "Advertisement skew in seconds.";
      default = 0;
    };

    upscript = mkOption {
      type = types.path;
      description = ''
        Command to run after become master, the interface name, virtual address
        and optional extra parameters are passed as arguments.
      '';
      example = literalExpression ''
        pkgs.writeScript "upscript" '''
          #!/bin/sh
          ''${pkgs.iproute2}/bin/ip addr add "$2"/24 dev "$1"
        ''';
      '';
    };

    downscript = mkOption {
      type = types.path;
      description = ''
        Command to run after become backup, the interface name, virtual address
        and optional extra parameters are passed as arguments.
      '';
      example = literalExpression ''
        pkgs.writeScript "downscript" '''
          #!/bin/sh
          ''${pkgs.iproute2}/bin/ip addr del "$2"/24 dev "$1"
        ''';
      '';
    };

    deadratio = mkOption {
      type = types.ints.unsigned;
      description = "Ratio to consider a host as dead.";
      default = 3;
    };

    shutdown = mkOption {
      type = types.bool;
      description = "Call downscript at exit.";
      default = false;
    };

    ignoreIfState = mkOption {
      type = types.bool;
      description = "Ignore interface state, e.g., down or no carrier.";
      default = false;
    };

    noMcast = mkOption {
      type = types.bool;
      description = "Use broadcast instead of multicast advertisements.";
      default = false;
    };

    extraParam = mkOption {
      type = types.nullOr types.str;
      description = "Extra parameter to pass to the up/down scripts.";
      default = null;
    };

    package = mkOption {
      type = types.package;
      description = ''
        Package that should be used for ucarp.

        Please note that the default package, pkgs.ucarp, has not received any
        upstream updates for a long time and can be considered as unmaintained.
      '';
      default = pkgs.ucarp;
      defaultText = literalExpression "pkgs.ucarp";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.ucarp = {
      description = "ucarp, userspace implementation of CARP";

      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      serviceConfig = {
        Type = "exec";
        ExecStart = ucarpExec;

        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        ProtectClock = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;
        MemoryDenyWriteExecute = true;
        RestrictRealtime = true;
      };
    };
  };

  meta.maintainers = with lib.maintainers; [ oxzi ];
}