650 Commits

Author SHA1 Message Date
Patrick J Cherry
102738d9ad Updated logging output during readloop() and writeloop() failures
There's a handy SHOW_ERRNO macro we can use to get consistent logging
for system call failures from readloop() and writeloop().
2018-04-27 10:45:42 +01:00
James Carter
3e0d30f6b9 Merge branch 'reinstate-sync-after-every-write' into 'develop'
Reinstate sync after every write

See merge request open-source/flexnbd-c!51
2018-04-24 12:02:53 +01:00
Patrick J Cherry
3b1a150315 Updated changelgo 2018-04-24 10:27:46 +01:00
Patrick J Cherry
ead6328d80 Force sync after every write 2018-04-24 10:27:02 +01:00
James Carter
20b4f069c8 Merge branch 'release-to-master' into 'develop'
Merge back to develop

See merge request open-source/flexnbd-c!49
2018-02-20 11:52:37 +00:00
Patrick J Cherry
331ca4be14 Updated changelog for release 2018-02-20 11:45:42 +00:00
James Carter
fb5714765c Merge branch 'fix-formatting' into 'develop'
Formatted all code using `indent`

See merge request open-source/flexnbd-c!47
2018-02-20 11:42:25 +00:00
Patrick J Cherry
af3bb16ff7 Merge branch 'develop' into fix-formatting 2018-02-20 11:06:58 +00:00
Patrick J Cherry
9cbcc7c95a Added note about the test file formatting 2018-02-20 11:05:36 +00:00
Patrick J Cherry
8893cd06c4 Re-formatted tests with a bit of tinkering by hand 2018-02-20 11:02:33 +00:00
James Carter
166db9b1f7 Merge branch 'enable-flags-test' into 'develop'
Enable request flags test

See merge request open-source/flexnbd-c!48
2018-02-20 10:23:42 +00:00
Patrick J Cherry
103bd7ad5b Undo formatting on test suite -- it wasn't right 2018-02-20 10:13:42 +00:00
Patrick J Cherry
7bee1aadfe Enable request flags test
Missed this out when I wrote the test!
2018-02-20 10:11:38 +00:00
Patrick J Cherry
f47f56d4c4 Formatted all code using indent 2018-02-20 10:05:35 +00:00
James Carter
19a1127bde Merge branch 'fix-correct-num-clients-status' into 'develop'
Call the thread cleanup code when requesting `status`

See merge request open-source/flexnbd-c!46
2018-02-20 09:51:37 +00:00
James Carter
073a4ac0fa Merge branch '35-incorrect-struct-type-used-in-readwrite-c' into 'develop'
Resolve "Incorrect struct type used in readwrite.c"

Closes #35

See merge request open-source/flexnbd-c!41
2018-02-20 09:50:25 +00:00
Patrick J Cherry
623007bfff Remove last reference to removed test_gets_num_clients 2018-02-19 10:22:01 +00:00
Patrick J Cherry
27a94a807e Remove the test_gets_num_clients test from the C unit tests
This test was causing problems by using dummy pointers to simulate
connections.  When calling the cleanup code, these pointers were
thought to be real, and the code attemtped to clean up threads
referenced by those pointers, causing a segfault.

I've reimplemented the test in the ruby acceptance suite.
2018-02-16 13:46:31 +00:00
Patrick J Cherry
1407407ff4 Updated changelog 2018-02-16 13:00:31 +00:00
Patrick J Cherry
d0439dab88 Call the thread cleanup code when requesting status
This ensures the correct number of connected clients is returned when
the status command is issued.

Previously the thread pool would only be cleaned up on a new connection.
2018-02-16 12:58:03 +00:00
James F. Carter
9f56f38f42 Merge branch 'rationalise-ld-preload-tests' into develop 2018-02-14 16:48:57 +00:00
Chris Elsworth
370d04d971 Merge branch 'take-request-response-size-into-malloc' into 'develop'
Update proxy malloc to add the struct size onto the request/response buffer

See merge request open-source/flexnbd-c!45
2018-02-14 05:28:24 +00:00
Patrick J Cherry
099e29de91 Merge branch 'develop' into 'take-request-response-size-into-malloc'
# Conflicts:
#   debian/changelog
2018-02-13 17:06:41 +00:00
Patrick J Cherry
2e17e8955f Added tests for NBD_MAX_SIZE
This constant is only used in the proxy, so the tests only cover proxy
mode.
2018-02-13 17:04:51 +00:00
Patrick J Cherry
bb1f6ecdf5 Updated changelog 2018-02-13 15:51:09 +00:00
Patrick J Cherry
158379ba7a Use correct constant name. 2018-02-12 19:11:24 +00:00
Patrick J Cherry
1c66b56af1 Update proxy malloc to add the struct size onto the request/response buffer
This alters the meaning of NBD_MAX_SIZE to be the actual max request size
we'll accept over nbd.  Previously it was *nearly* the max size we'd
accept depending on the size of the struct.
2018-02-12 19:04:29 +00:00
Ian Chilton
03d9eb01b5 Merge branch 'increase-log-level-for-readloop-failures' into 'develop'
Increase log level for readloop failures, which might help with diagnosis

See merge request open-source/flexnbd-c!44
2018-02-09 15:38:48 +00:00
Patrick J Cherry
cdcd527544 Refactored read_reply to compare the network-byte-ordered handle 2018-02-09 12:18:34 +00:00
Patrick J Cherry
169d40f575 Increase log level for readloop failures, which might help with diagnosis 2018-02-09 11:57:07 +00:00
Patrick J Cherry
21f384e343 Updated changelog 2018-02-09 11:44:28 +00:00
Patrick J Cherry
9817fd7b0a Final tidies, comments etc. 2018-02-09 11:42:25 +00:00
Patrick J Cherry
195de41d86 Remove extra line 2018-02-09 11:32:26 +00:00
Patrick J Cherry
5b350e10e5 Merge branch 'develop' into '35-incorrect-struct-type-used-in-readwrite-c'
# Conflicts:
#   debian/changelog
2018-02-09 11:29:48 +00:00
Patrick J Cherry
b75a6529d0 Move LdPreload include to correct place 2018-02-09 10:41:24 +00:00
Patrick J Cherry
8e67180999 Check that TCP_NODELAY is set on upstream sockets on reconnection
Also rationalize the test to see if a function has been called.  Still
not great, but getting there :)
2018-02-09 10:26:08 +00:00
Patrick J Cherry
c053a54faa Added test to cover setsockopt for tcpkeepalive 2018-02-08 23:07:17 +00:00
Patrick J Cherry
ebacf738bc Tidy up ld preload hacks 2018-02-08 22:28:34 +00:00
James Carter
c4bab3f81f Merge branch 'truncate-odd-sized-discs' into 'develop'
Discs must be sized in multiples of 512 bytes or odd things happen

See merge request open-source/flexnbd-c!42
2018-02-08 16:49:36 +00:00
Patrick J Cherry
a19267b377 Adjust block-rounding line to match in serve.c 2018-02-08 16:37:36 +00:00
Patrick J Cherry
23d9ff587e Updated changelog 2018-02-08 16:36:20 +00:00
Patrick J Cherry
347b7978e4 Discs must be sized in multiples of 512 bytes or odd things happen
In #36 some of the odd errors were due to seeks beyond the end of the
disc.  This was because the disc was "specially crafted" to be 25GB + 1
byte, which doesn't fit into the normal 512 byte sectors expected of a
disc.  This lead to reads going beyond the end of the disc etc.

If a similarly evil disc is used with `losetup`, it just ignores the
last bytes of the disc that don't fit into 512 chunks.  This is what
that patch does, logging an error at the same time.
2018-02-08 16:31:28 +00:00
Patrick J Cherry
f8fec5f57e Alter struct types to reflect reality, avoiding mixing "host" and "raw" structs 2018-02-08 15:46:34 +00:00
James Carter
1672b4b88b Merge branch '36-breaks-when-trying-to-install-debian-from-cd' into 'develop'
Resolve "breaks when trying to install debian from CD"

Closes #36

See merge request open-source/flexnbd-c!40
2018-02-08 13:59:12 +00:00
Patrick J Cherry
5e9dbbd626 Updated changelgo 2018-02-08 13:32:10 +00:00
Patrick J Cherry
8beb3f0af6 Allow proxy to pass NBD protocol errors downstream; server returns EINVAL/ENOSPC appropriately
Previously the proxy would just disconnect when it saw an NBD protocol
error, and retry the operation it was in the middle of.

Additionally, the server needs to return the correct error types when
this happens.
2018-02-08 13:19:51 +00:00
James Carter
806de13024 Merge branch 'try-flags' into 'develop'
Set flags to show we can accept FUA and FLUSH commands

See merge request open-source/flexnbd-c!38
2018-02-08 11:18:31 +00:00
Patrick J Cherry
f71b872622 Only set up LD_PRELOAD for tests that actually need it. 2018-02-07 22:05:07 +00:00
Patrick J Cherry
79181b3153 Added LD_PRELOAD library to monitor msync calls in testing 2018-02-07 21:45:20 +00:00
Patrick J Cherry
55548cc969 Change ordering of @env configuration/start so we can alter the blocksize.
argh.
2018-02-06 10:24:54 +00:00
Patrick J Cherry
9bf3b52d54 Call proxy_finish_connect_to_upstream when reconnecting, setting
TCP_NODELAY
2018-02-06 10:02:16 +00:00
Patrick J Cherry
da35187af0 Allow blocksize to be changed in Environment
This number is peppered all over the test suite, so changing @blocksize
for everything is not a goer, when we really only need to change it for
one test.
2018-02-06 09:55:32 +00:00
Patrick J Cherry
7704f9e5c8 Fix tests to reflect new filesize. 2018-02-06 07:57:40 +00:00
Patrick J Cherry
3a86870c9f Use sysconf to determine actual page size for msync
Also added comments in tests around testing for msync offsets/lengths.
2018-02-06 07:32:58 +00:00
Patrick J Cherry
6d6948af09 Fix offset calculation for partial msyncs to go to nearest 4k block
Previously they were always set to zero.
2018-02-05 23:05:00 +00:00
Patrick J Cherry
c423900f02 Fix typo 2018-02-05 17:04:23 +00:00
Patrick J Cherry
afa1bb0efb Use msync rather than fsync to flush the entire disc
This involves storing the size of the mapped disc in the client struct,
and then supplying that to the msync command.
2018-02-05 17:01:32 +00:00
Patrick J Cherry
ad2014ac9d Fixed long-standing bug with h2r functions being back to front
h2r seemd to be using beXXtoh functions instead of htobeXX.  Foruntately
ROT13 works symmetrically on our systems..!
2018-02-05 16:16:17 +00:00
Patrick J Cherry
d1dc7392c2 Open file with O_NOATIME, not O_SYNC
O_SYNC is not necessary as we're not doing direct writes to the file.
O_NOATIME might give some speed boost.
2018-02-05 16:15:36 +00:00
Patrick J Cherry
ba59a4c03f Updated changelog 2018-02-05 08:15:56 +00:00
Patrick J Cherry
2b58468800 Added test for FUA acceptance.
Although I think this might be a bit useless as servers normally just
ingore flags.
2018-02-03 20:29:15 +00:00
Patrick J Cherry
4d9db4d6e9 Added basic FLUSH test 2018-02-03 20:10:47 +00:00
Patrick J Cherry
d6057a4244 Use 'English' in ruby 2018-02-02 21:41:07 +00:00
Patrick J Cherry
1d98ba1d3e Further rubocopping 2018-02-02 21:36:30 +00:00
Patrick J Cherry
9c48da82cc Rubocop 2018-02-02 21:34:14 +00:00
Patrick J Cherry
1b7b688f7a Tidied up nbd init test 2018-02-02 21:30:55 +00:00
Patrick J Cherry
3410ccd4c5 Fixed up commenting around our advertised flags. 2018-02-02 20:50:48 +00:00
Patrick J Cherry
051576df6d Remove warnings about Object#timeout 2018-02-02 20:46:46 +00:00
Patrick J Cherry
9eb7072f49 Removed some extra spaces I'd added 2018-02-02 20:46:25 +00:00
Patrick J Cherry
6aa5907f5e Tidied constants up a bit 2018-02-02 20:34:49 +00:00
Patrick J Cherry
72c8c6f757 Altered test to check for type as a 16-bit uint; added flags test 2018-02-02 20:30:39 +00:00
Patrick J Cherry
b22b99d9b9 Fix fill_request to set flags as well as type. 2018-02-02 20:28:00 +00:00
Patrick J Cherry
ad001cb83c Tidy comments 2018-02-02 16:17:01 +00:00
Patrick J Cherry
f37e4438c8 Merge branch 'develop' into try-flags 2018-02-02 16:05:57 +00:00
Chris Elsworth
084d429961 Merge branch 'update-changelog-for-mr35' into 'develop'
Updated changelog for !35

See merge request open-source/flexnbd-c!39
2018-02-02 14:57:58 +00:00
Patrick J Cherry
1883bee43c Updated changelog for !35 2018-02-02 14:52:26 +00:00
Patrick J Cherry
68a196e93d Allow the proxy connection to pass through flags from upstream. 2018-02-02 10:30:40 +00:00
Patrick J Cherry
1f0ef0aad6 Implement FLUSH command and honour FUA flag
I changed the request struct to break the 32 bits reserved for the
request type into two.  The first part of this is used for the flags
(such as FUA), and the second part for the command type.  Previously
we'd masked the top two bytes, thus ignoring any flags.
2018-02-01 22:13:59 +00:00
Patrick J Cherry
25cc084108 First steps towards implementing flags as part of oldstyle negotiation 2018-02-01 19:25:36 +00:00
Patrick J Cherry
f2fa00260b Merge branch 'avoid-crash-on-timeout' into 'develop'
avoid fatal error on client connection timeout

See merge request open-source/flexnbd-c!36
2018-01-26 16:04:51 +00:00
James F. Carter
b2007c9dad debian: uodate changelog 2018-01-26 15:06:26 +00:00
James F. Carter
9b1781164a avoid fatal error on client connection timeout 2018-01-26 15:03:44 +00:00
Ian Chilton
1f99929589 Merge branch 'develop' into 'develop'
Develop

See merge request open-source/flexnbd-c!35
2018-01-24 12:42:49 +00:00
Chris Cottam
c37627a5b9 not high enough, trying 32MB 2018-01-18 17:08:32 +00:00
Chris Cottam
ceb3328261 increasing the NBD max size to see if it fixes an issue with qemu-2.11.0 2018-01-18 16:52:24 +00:00
Patrick J Cherry
61940bdfc5 Merge branch '34-logging-should-include-the-id-of-the-disc-that-is-being-served' into 'develop'
add a log_context, a string output as part of any log message

Closes #34

See merge request open-source/flexnbd-c!34
2018-01-11 10:35:45 +00:00
James F. Carter
6d96d751d8 debian: update changelog 2018-01-11 10:06:03 +00:00
James F. Carter
fa75de0a8b proxy sets the upstream address and port as its log context 2018-01-11 10:04:18 +00:00
James F. Carter
1cb11bfd38 serve sets the disc's backing file as its log context 2018-01-11 10:03:16 +00:00
James F. Carter
2702e73a26 add a log_context, a string output as part of any log message 2018-01-11 10:01:42 +00:00
Patrick J Cherry
dbf50046a8 Merge branch '33-tcp-keepalive-should-be-applied-to-connection-so-that-dead-connections-can-be-properly-reaped' into 'develop'
apply tcp keepalive to serving sockets

Closes #33

See merge request open-source/flexnbd-c!33
2018-01-10 17:51:02 +00:00
James F. Carter
d62b069ce4 debian: update changelog 2018-01-10 13:58:11 +00:00
James F. Carter
884a714744 whitespace fix 2018-01-10 13:55:05 +00:00
James F. Carter
0c668f1776 remember how || works in C 2018-01-10 13:54:26 +00:00
James F. Carter
1d5b315f17 apply tcp keepalive to serving sockets 2018-01-10 13:49:22 +00:00
Patrick J Cherry
24f1e62a73 Merge branch 'release' into 'develop'
Merge changelog back to develop

See merge request !32
2017-07-14 17:41:51 +01:00
Chris Elsworth
5c37cba39b New release 2017-07-14 17:03:56 +01:00
James F. Carter
59f264184b Merge pull request #1 from BytemarkHosting/better-stats
Calculate and return bytes_left in migration statistics
2017-07-14 16:36:50 +01:00
Chris Elsworth
42d206cfb7 Update test 2017-07-14 16:26:25 +01:00
Chris Elsworth
ab3106202a Also return migration_bytes_left 2017-07-14 16:18:34 +01:00
James Carter
e04dead5ce Merge branch 'update-changelog' into 'develop'
Updated changelog.

See merge request !30
2017-04-13 12:52:00 +01:00
Patrick J Cherry
88bc5f0643 Updated changelog. 2017-04-13 12:49:55 +01:00
James Carter
e89c87e2b9 Merge branch 'fix-compiler-flags' into 'develop'
Remove lots of per-cpu compiler flags.

See merge request !28
2017-02-23 12:11:25 +00:00
Patrick J Cherry
9d2ac3f403 Remove lots of per-cpu compiler flags.
These flags appear to cause SIGILL when flexnbd starts on some CPUs.
2017-02-22 17:52:52 +00:00
James Carter
67823bf85b Merge branch '32-package-and-publish-in-gitlab-ci-retire-maker2-job' into 'master'
Resolve "package and publish in gitlab-ci - retire maker2 job"

Closes #32 and #21

See merge request !27
2017-01-23 14:04:43 +00:00
Patrick J Cherry
17d30b86ad Updated build-deps to have libsubunit and ruby-test-unit 2017-01-23 14:00:09 +00:00
Patrick J Cherry
b97bcd6f51 Don't test separately from packaging. Also use correct source "format" 2017-01-23 13:58:04 +00:00
Patrick J Cherry
4d3c15a4d0 Switch to native from quilted packaging 2017-01-23 13:52:22 +00:00
Patrick J Cherry
83d6872a8d Add ruby test dependency 2017-01-23 13:48:19 +00:00
Patrick J Cherry
ab8470aef3 Modernise gitlab-ci 2017-01-23 13:46:42 +00:00
Patrick J Cherry
716df32fd6 Merge remote-tracking branch 'origin/debian' into 32-package-and-publish-in-gitlab-ci-retire-maker2-job 2017-01-23 13:44:44 +00:00
Michel Pollet
1a768d5e9c Merge branch '29-fix-linker-issue' into 'master'
Link against subunit for testing.

This fixes the problems in Debian stretch+.

Closes #29

See merge request !26
2016-10-13 16:47:37 +01:00
Patrick J Cherry
72992c76ac Added libsubunit to the gitlab-ci 2016-10-13 16:42:21 +01:00
Patrick J Cherry
cace8123f4 Link against subunit for testing.
This fixes the problems in Debian stretch+.
2016-10-13 16:39:20 +01:00
Patrick J Cherry
c3b241464a Updated changelog 2016-10-07 12:26:52 +01:00
Patrick J Cherry
4f956e4b9d Merge branch 'master' of gitlab.bytemark.co.uk:open-source/flexnbd-c into debian 2016-10-07 12:24:51 +01:00
James Carter
b4cb2d9240 Merge branch 'fix-wrong-handle-type' into 'master'
Fix up "wrong" handle type from char* to uint64_t

Following from the NBD handle comparison simplifications.

See merge request !25
2016-10-07 10:20:35 +01:00
James Carter
1efb7bada6 Merge branch 'fix-unsigned-longs-in-bitset-test' into 'master'
fix check_bitset test on 32-bit platforms

The use of `unsigned long` and `UL` suffices caused this test to fail
on 32 bit platforms, where these are just 4, not 8 bits long.

```
tests/unit/check_bitset.c:73:F:bit:test_bit_ranges:0: longs[32] = 0 SHOULD BE ffffffff
```

See merge request !24
2016-10-07 10:20:08 +01:00
James Carter
6bc2a4c0b9 Merge branch 'fix-cast-from-pointer-to-wrong-size-integer-in-serve' into 'master'
This fixes the compiler warning pointer-to-int-cast in serve.c

```
In file included from src/server/bitset.h:4:0,
                 from src/server/mirror.h:8,
                 from src/server/flexnbd.h:5,
                 from src/server/serve.h:8,
                 from src/server/serve.c:1:
src/server/serve.c: In function 'tryjoin_client_thread':
src/server/serve.c:258:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
      (uint64_t)status);
      ^
```

See merge request !23
2016-10-07 09:59:24 +01:00
James Carter
59de76c50c Merge branch 'skip-large-file-test-on-i386' into 'master'
Skip large file test on 32-bit platforms

This test cannot run on 32-bit machines as they cannot access files
large than 2G.  Makes flexnbd on 32-bit a bit useless really..

See merge request !22
2016-10-07 09:57:09 +01:00
Patrick J Cherry
209da655b3 Skip large file test on 32-bit platforms
This test cannot run on 32-bit machines as they cannot access files
large than 2G.  Makes flexnbd on 32-bit a bit useless really..
2016-10-06 21:42:52 +01:00
Patrick J Cherry
52b45e6b40 fix check_bitset test on 32-bit platforms
The use of `unsigned long` and `UL` suffices caused this test to fail
on 32 bit platforms, where these are just 4, not 8 bits long.

```
tests/unit/check_bitset.c:73:F:bit:test_bit_ranges:0: longs[32] = 0 SHOULD BE ffffffff
```
2016-10-06 21:22:53 +01:00
Patrick J Cherry
d279eb7570 Fix up "wrong" handle type from char* to uint64_t
Following from the NBD handle comparison simplifications.
2016-10-06 21:19:15 +01:00
Patrick J Cherry
c07df76ede This fixes the compiler warning pointer-to-int-cast in serve.c
```
In file included from src/server/bitset.h:4:0,
                 from src/server/mirror.h:8,
                 from src/server/flexnbd.h:5,
                 from src/server/serve.h:8,
                 from src/server/serve.c:1:
src/server/serve.c: In function 'tryjoin_client_thread':
src/server/serve.c:258:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
      (uint64_t)status);
      ^
```
2016-10-06 21:16:07 +01:00
Patrick J Cherry
e7e99b099c Updated debian packaging, adding in new build-deps. 2016-10-06 16:02:15 +01:00
Patrick J Cherry
b2edd0734a Merge branch 'master' of gitlab.bytemark.co.uk:open-source/flexnbd-c into debian 2016-10-06 16:00:14 +01:00
James Carter
e19d005636 Merge branch '26-fix-function-definition' into 'master'
OK removed the cast and fixed the function def in the test

This should definitely clear the warning.

Closes #26

See merge request !21
2016-10-06 15:59:57 +01:00
Patrick J Cherry
d1e6e835c4 OK removed the cast and fixed the function def in the test
This should definitely clear the warning.
2016-10-06 15:56:57 +01:00
Patrick J Cherry
8fed794fe7 Merge branch 'master' of gitlab.bytemark.co.uk:open-source/flexnbd-c into debian 2016-10-06 15:47:25 +01:00
James Carter
e24efa9864 Merge branch '26-fix-compiler-warning' into 'master'
Resolve "tests/unit/check_readwrite.c causes compiler warnings"

Closes #26

See merge request !20
2016-10-06 15:46:41 +01:00
James Carter
3134d619ef Merge branch '27-fix-make-test' into 'master'
Update Makefile to specify dependencies properly for tests

Closes #27

See merge request !19
2016-10-06 15:44:46 +01:00
Patrick J Cherry
898f3f6c7e Reinstated char * cast to remove compiler warning 2016-10-06 15:43:20 +01:00
Patrick J Cherry
5a1bc21088 Update Makefile to specify dependencies properly for tests 2016-10-06 15:40:15 +01:00
James Carter
deb8f2c53b Merge branch 'fix-check-nbdtypes' into 'master'
Fix up nbdtypes test to correctly use htobe64

Previous change hadn't taken this into account, and hopefully this makes
our test a little clearer.

See merge request !18
2016-10-06 14:50:03 +01:00
Patrick J Cherry
1338d9e910 Fix up nbdtypes test to correctly use htobe64
Previous change hadn't taken this into account, and hopefully this makes
our test a little clearer.
2016-10-06 14:46:29 +01:00
James Carter
47c05174b6 Merge branch 'fix-check-readwrite' into 'master'
Fix check readwrite segfault

Little slip corrected :)

See merge request !17
2016-10-06 14:10:22 +01:00
Patrick J Cherry
191b3bc72c Merge branch 'master' of gitlab.bytemark.co.uk:open-source/flexnbd-c into fix-check-readwrite 2016-10-06 14:06:21 +01:00
James Carter
770ca0d0e5 Merge branch 'fix-test-names' into 'master'
Fixed up internal test names (copy/pasta?)

The test names output by `make check` now reflect reality.

See merge request !16
2016-10-06 14:04:54 +01:00
Patrick J Cherry
6505588f25 Fixed check_readwrite test to pass correct handle to fd_write_reply
The (char*) cast to resp->received.handle.b was causing a segfault
2016-10-06 14:01:47 +01:00
Patrick J Cherry
957707bcfc Fixed up internal test names (copy/pasta?)
The test names output by `make check` now reflect reality.
2016-10-06 13:44:20 +01:00
James Carter
3f01b77221 Merge branch 'update-manpages-again' into 'master'
Updated manpages, replaces a2x with txt2man

This simplifies the build-deps for Debian packages a little, and brings
the docs up to date.

See merge request !15
2016-10-06 13:43:53 +01:00
Patrick J Cherry
0dbea7f8fe Removed extra tabs
Oops
2016-10-06 13:11:07 +01:00
Patrick J Cherry
091aacd16d Updated manpages, replaces a2x with txt2man
This simplifies the build-deps for Debian packages a little, and brings
the docs up to date.
2016-10-06 12:55:05 +01:00
Patrick J Cherry
04b6637451 Merge branch 'failed-tests-cause-error' into 'master'
failures in make check now result in an error



See merge request !13
2016-10-05 17:20:43 +01:00
James F. Carter
7d2eda6cea failures in make check now result in an error 2016-10-05 16:28:27 +01:00
Patrick J Cherry
7e152ca4f2 Merge branch '24-tests-in-gitlab' into 'master'
Resolve "tests should be run in gitlab-ci"

Closes #24

See merge request !12
2016-10-05 15:08:22 +01:00
James F. Carter
fe0125efbc Merge branch 'master' into 24-tests-in-gitlab 2016-10-05 14:27:56 +01:00
James Carter
ebaaa6d671 Merge branch '25-retire-rake' into 'master'
Moved tasks from Rake to Make

The rake file was obsolete, apart from one invocation of ruby in a shell!

Closes #25

See merge request !11
2016-10-05 14:26:06 +01:00
James F. Carter
8cc8588744 Merge branch '24-tests-in-gitlab' of gitlab.bytemark.co.uk:open-source/flexnbd-c into 24-tests-in-gitlab 2016-10-05 13:06:36 +01:00
James F. Carter
5da77ea39a remove unnecessary step in gitlab-ci 2016-10-05 12:52:32 +01:00
James F. Carter
a744965c67 add missing deps on server object files when building check binaries 2016-10-05 12:51:58 +01:00
Patrick J Cherry
d07659f694 Merge branch '24-tests-in-gitlab' of gitlab.bytemark.co.uk:open-source/flexnbd-c into 25-retire-rake 2016-10-05 12:49:53 +01:00
Patrick J Cherry
30562ed900 Added dpkg-dev to requirements
Allows dpkg-architecture to run.
2016-10-05 12:49:25 +01:00
Patrick J Cherry
93c0fa2e92 Merged in gitlab-ci.yml and fixed to use Make
The CI should now use Make instead of Rake
2016-10-05 12:47:24 +01:00
Patrick J Cherry
8dc491fb89 Merge branch '24-tests-in-gitlab' of gitlab.bytemark.co.uk:open-source/flexnbd-c into 25-retire-rake 2016-10-05 12:46:47 +01:00
Patrick J Cherry
ea7cd64fc2 Moved tasks from Rake to Make
The rake file was obsolete, apart from one invocation of ruby in a
shell!
2016-10-05 12:36:06 +01:00
James F. Carter
35d3340708 avoid need for slow-to-install asciidoc in gitlab-ci 2016-10-05 12:10:21 +01:00
James F. Carter
d47a44a204 install asciidoc in gitlab-ci 2016-10-05 12:07:24 +01:00
James F. Carter
d6968d8242 explicitly compile before running tests in gitlab-ci 2016-10-05 12:06:11 +01:00
James F. Carter
bf85e329a0 clean build environment before running tests in gitlab-ci 2016-10-05 12:03:23 +01:00
James F. Carter
edcaef532c revert gitlab-ci to ruby2.1 2016-10-05 11:58:50 +01:00
James F. Carter
cb920e4e9d Merge branch 'master' into 24-tests-in-gitlab 2016-10-05 11:58:21 +01:00
Patrick J Cherry
91d85633b6 Merge branch 'force-encoding-for-ruby21' into 'master'
force binary encoding in a ruby2.1-compatible way



See merge request !10
2016-10-05 11:57:14 +01:00
James Carter
7c516b85a6 Merge branch 'makefile-fixes' into 'master'
Makefile fixes



See merge request !2
2016-10-05 11:55:54 +01:00
James F. Carter
679fa6dbf8 force binary encoding in a ruby2.1-compatible way 2016-10-05 11:54:09 +01:00
James F. Carter
50708326ec try ruby2.3 in gitlab-ci 2016-10-05 11:42:32 +01:00
James F. Carter
d907025d71 try a newer version of ruby in gitlab-ci 2016-10-05 11:39:56 +01:00
James F. Carter
e4d398a078 install net-tools in gitlab-ci 2016-10-05 11:29:42 +01:00
James F. Carter
8de0780125 install libev-dev in gitlab-ci 2016-10-05 11:26:46 +01:00
James F. Carter
0fd16822ea run tests in gitlab-ci 2016-10-05 11:12:39 +01:00
Patrick J Cherry
1e3c61b541 Merge branch '23-fix-unit-tests' into 'master'
update tests to reflect changes in handle storage

Closes #23

See merge request !9
2016-10-05 11:08:21 +01:00
James F. Carter
a09e14b2d4 whitespace fix 2016-10-05 11:06:39 +01:00
James F. Carter
a6710b6c32 update tests to reflect changes in handle storage 2016-10-05 10:57:52 +01:00
Patrick J Cherry
ed3995303f Reinstate doc to all 2016-10-05 10:41:23 +01:00
James Carter
f5de8fb12b Merge branch '20-fix-encoding-failures' into 'master'
Use a BINARY encoded string when doing read/write comparisons.

This is a bit of a cheat really, but `#read` returns an ASCII encoded
string, where as our ruby generates UTF-8 encoded strings, causing
assertion failures.

Closes #20

See merge request !8
2016-10-05 10:32:05 +01:00
James F. Carter
99a5f79a52 fixed typo 2016-10-05 10:30:44 +01:00
Patrick J Cherry
356e1fd6a1 Use a BINARY encoded string when doing read/write comparisons.
This is a bit of a cheat really, but `#read` returns an ASCII encoded
string, where as our ruby generates UTF-8 encoded strings, causing
assertion failures.

Fixes #20
2016-10-05 10:01:15 +01:00
James Carter
67dcea207d Merge branch '19-fix-double-definition-warnings' into 'master'
Fixes "double-definition of constants" warning

Looks like `#constants.include?` doesn't work as well as `#const_defined?`.

Closes #19

See merge request !6
2016-10-05 10:00:50 +01:00
Patrick J Cherry
d3762162db Fixes "double-definition of constants" warning
Looks like `#constants.include?` doesn't work as well as
`#const_defined?`.
2016-10-05 09:29:07 +01:00
Patrick J Cherry
3571d3f82e Added net-tools to the build-deps for testing
Fixes #21
2016-10-05 09:27:10 +01:00
Patrick J Cherry
4cd7e764bb Updated changelog 2016-10-04 21:22:07 +01:00
Patrick J Cherry
4f535fbb02 Merge branch 'master' of gitlab.bytemark.co.uk:open-source/flexnbd-c into debian 2016-10-04 21:14:26 +01:00
James Carter
218c55fb63 Merge branch 'simplify-nbd-handles-part-deux' into 'master'
Simplified NBD handle comparisons

8 bytes, therefore a uing64_t to compare to, no need for memcmp()

Signed-off-by: Michel Pollet <buserror@gmail.com>

See merge request !5
2016-10-04 15:49:07 +01:00
Michel Pollet
956a602475 Simplified NBD handle comparisons
8 bytes, therefore a uing64_t to compare to, no need for memcmp()

Signed-off-by: Michel Pollet <buserror@gmail.com>
2016-10-04 15:41:48 +01:00
James Carter
26a0a82f9d Merge branch '12-fix-bind' into 'master'
Attempt at fixing bind() bug

This will prevent the bind() wrapper to loop forever in some cases. I
could nor reproduce the issue, but this removes the only infinite loop I
could find.

Closes #12

See merge request !3
2016-10-04 15:41:37 +01:00
Michel Pollet
76e0476113 Attempt at fixing bind() bug
This will prevent the bind() wrapper to loop forever in some cases. I
could nor reproduc the issue, but this removes the only infinite loop I
could find.

Signed-off-by: Michel Pollet <buserror@gmail.com>
2016-10-04 15:36:46 +01:00
Michel Pollet
d9651a038c Makefile: don't include *.d's before 'all'
Include any .d file from the build directory, and do that after all the
other targets

Signed-off-by: Michel Pollet <buserror@gmail.com>
2016-10-04 15:32:56 +01:00
Michel Pollet
fcd3d33498 Simplified Makefile
gcc and clang can generate dep files as well as compiling in a single
pass, no need for two.

Signed-off-by: Michel Pollet <buserror@gmail.com>
2016-10-04 15:32:49 +01:00
James Carter
e3360a3a1b Merge branch 'cherry-pick-41f25408' into 'master'
Close socket fix, might relate to migration crashing

This was listed as a bug, and was immediatelly picked the static
analyzer anyway, this is very likely the cause for the
migration-cancel-crash bug.

closes #10 and possibly closes #11

See merge request !1
2016-09-14 11:29:12 +01:00
Michel Pollet
1fefe1a669 Close socket fix, might relate to migration crashing
This was listed as a bug, and was immediatelly picked the static
analyzer anyway, this is very likely the cause for the
migration-cancel-crash bug.

Signed-off-by: Michel Pollet <buserror@gmail.com>
2016-09-14 10:45:49 +01:00
Patrick J Cherry
4ed8d49b2c Updated rules to skip ruby tests, and just use the normal make check 2016-08-31 10:06:07 +01:00
Patrick J Cherry
3af0e84f5f Updated Debian packaging to be in a separate branch.
This should allow us to use git-buildpackage to build our packages.
2016-08-30 21:57:00 +01:00
Patrick J Cherry
ba14943b60 Removed old changelog.template 2016-08-30 21:49:54 +01:00
Patrick J Cherry
4a709e73f8 Moved .hgignore to .gitignore 2016-08-30 21:47:25 +01:00
Patrick J Cherry
91a8946ddc Removed debian directory 2016-08-30 21:46:59 +01:00
nick
20f99b4554 flexnbd: We only require 1/8th of the memory we allocate for bitsets (bits vs. bytes confusion) 2015-05-13 09:25:09 +01:00
nick
c363991cfd Makefile: Add -lm to LLDFLAGS 2015-04-01 12:39:07 +01:00
Alex Young
c41eeff2fc Moved the server-specific files into src/server 2014-03-11 11:05:43 +00:00
Alex Young
5960e4d10b Remove the proxy's dependency on flexnbd.h 2014-03-11 10:37:00 +00:00
Alex Young
f0911b5c6c Tighten up some variable scopes. 2014-03-11 10:24:29 +00:00
Alex Young
b063f41ba8 Avoid a potential null pointer dereference 2014-03-11 09:57:19 +00:00
Alex Young
28c7e43e45 Fix a harmless buffer overflow 2014-03-11 09:49:25 +00:00
Alex Young
9326b6b882 Merge 2014-02-27 16:18:17 +00:00
Alex Young
f93476ebd3 Replace off64_t with uint64_t where it makes sense to do so.
It looks like off64_t was propagated through the code from the return
type of lseek64(), which isn't appropriate in many of the places we're
using it.
2014-02-27 16:04:25 +00:00
Alex Young
666b60ae1c Allow subset reads in prefetch_contains and prefetch_offset 2014-02-27 14:54:18 +00:00
nick
f48bf2b296 Automated merge with ssh://dev/flexnbd-c 2014-02-27 14:33:01 +00:00
nick
705164ae3b Cork/uncork in mirror - socket_connect already sets nodelay 2014-02-27 14:32:54 +00:00
nick
dbe7053bf3 Avoid some false positives 2014-02-27 14:32:26 +00:00
Alex Young
fa8023cf69 Proxy prefetch cache becomes a command-line argument. 2014-02-27 14:21:36 +00:00
nick
aba802d415 bitset: Allocate the right amount of memory
We were calculating the wrong number of words per byte in the first
place, and then passing the number of *words* to malloc, which expects
the number of *bytes*.

Fix both errors
2014-02-27 12:57:09 +00:00
Alex Young
d146102c2c Cherry-pick extra toolchain Makefile options 2014-02-26 15:56:41 +00:00
Alex Young
5551373073 Merge 2014-02-26 15:37:44 +00:00
Alex Young
77f333423b Apply Michel's tidy-ups 2014-02-26 15:19:03 +00:00
Alex Young
ffa45879d7 Pull back the changelog generation to the simplest thing that can possibly work 2014-02-25 17:24:25 +00:00
Alex Young
2fa1ce8e6b Tweak changelog generation not to skip commits since last tag 2014-02-25 16:35:51 +00:00
nick
6f540ce238 proxy: Turn on TCP_CORK
Now that we're using NODELAY, we should definitely use cork around
writes to the upstream server. This prevents each partial write()
from being its own packet, which would be terrible if it actually
happened with any regularity (we'd mostly see it when the kernel
is stressed, and write() is progressing a few bytes at a time as
a result)
2014-02-25 16:00:48 +00:00
nick
f9a3447bc9 proxy: Turn on TCP_NODELAY for the proxy->upstream leg
Nagle doesn't actually affect us too badly here, as we don't write
the header and then the data in two separate calls under normal
circumstances, which is the pathological case, but we should have
NODELAY on, regardless
2014-02-25 15:59:05 +00:00
nick
7806ec11ee client: cork/uncork around NBD_REQUEST_READ responses
We don't cork/uncork around NBD_REQUEST_WRITE responses because
they're only 16 bytes, and we're using blocking writes.
2014-02-25 15:45:41 +00:00
nick
1817c13acb sockutil: Add a tcp_cork helper 2014-02-25 15:44:46 +00:00
nick
97c8d7a358 Remove a compile-time optional selection of O_DIRECT (was never used)
The mmap() manpage tells us to avoid using O_DIRECT with mmap() - so
do so.
2014-02-24 13:47:29 +00:00
Alex Young
8cf92af900 Call srand() to make sure request handles are properly randomised 2014-02-24 12:20:50 +00:00
Alex Young
5185be39c9 Merge 2014-02-24 11:25:46 +00:00
Alex Young
374b4c616e Remove unreachable code to make -Wunreachable-code on clang useful. 2014-02-24 11:23:09 +00:00
Alex Young
50ec8fb7cc Depend on either libev4 or libev3, whichever is available 2014-02-24 11:22:26 +00:00
Alex Young
5fc9ad6fd8 Add some build-depends which make doc needs 2014-02-21 21:40:55 +00:00
Alex Young
85c463c4bd Add asciidoc as a Build-Depends 2014-02-21 20:46:44 +00:00
Alex Young
278a3151a8 Update Rakefile to generate debian/changelog.
`rake changelog` and a commit should be run after each `hg tag`.
2014-02-21 19:58:02 +00:00
Alex Young
0ea66b1e04 Added tag 0.1.1 for changeset 303f6859295d 2014-02-21 19:54:25 +00:00
Alex Young
83e3d65be9 Update the Makefile to work with dpkg-buildpackage 2014-02-21 19:39:27 +00:00
Alex Young
4f31bd9340 Switch from a rake-based build to a make-based build.
This commit beefs up the Makefile to do the build, instead of the
Rakefile.

It also removes from the Rakefile the dependency on rake_utils, which
should mean it's ok to build in a schroot.

The files are reorganised to make the Makefile rules more tractable,
although the reorganisation reveals a problem with our current code
organisation.

The problem is that the proxy-specific code transitively depends on the
server code via flexnbd.h, which has a circular dependency on the server
and client structs. This should be broken in a future commit by
separating the flexnbd struct into a shared config struct and
server-specific parts, so that the server code can be moved into
src/server to more accurately show the functional dependencies.
2014-02-21 19:10:55 +00:00
nick
0baf93fd7b proxy: Fix a read corruption issue caused by us failing to reset needles on timeout 2014-02-11 20:43:44 +00:00
nick
175f19b3e7 client: Add a cork TODO pair 2014-02-11 15:22:54 +00:00
nick
8d56316548 client: Start checking for exceptions on the client socket 2014-02-11 14:32:12 +00:00
nick
27f2cc7083 Some debug and whitespace tweaks 2014-02-11 14:31:58 +00:00
nick
8084a41ad2 flexnbd client: Catch a few cases where the killswitch wasn't disarmed 2014-01-28 11:45:27 +00:00
nick
5ca5858929 Increase a timeout on a test to handle slow unlink calls on other filesystems 2014-01-22 12:21:49 +00:00
nick
afcc07a181 Fix stop signal logic broken by the killswitch 2014-01-22 12:16:09 +00:00
nick
dcead04cf6 Fix up the check_util test once more 2014-01-22 12:10:34 +00:00
nick
4f7f5f1745 Fix a few dangling bits in client.h 2014-01-22 12:01:42 +00:00
nick
976e9ba07f Automated merge with ssh://dev.bytemark.co.uk//repos/flexnbd-c 2014-01-22 11:49:26 +00:00
nick
91d9531a60 flexnbd serve: Make the killswitch per-client-thread
This is a bit tricky, but calling shutdown() on a socket in a signal
handler is safe, and (at least in linux) appears to cause any read()
or write() calls blocked on that socket to return, even with SA_RESTART.

I'm not confident enough about the rest of flexnbd's syscall error
handling to turn SA_RESTART off for this signal...
2014-01-22 11:49:21 +00:00
nick
905d66af77 Rework a test 2014-01-22 11:45:35 +00:00
nick
eee7c9644c Another fedora build fix 2014-01-22 11:42:00 +00:00
nick
ce5c51cdcf Fix a test case 2014-01-22 11:40:19 +00:00
nick
c6c53c63ba Fix compilation on fedora 2014-01-22 10:39:29 +00:00
Tristan Heaven
20bd58749e Fix help_text errors for break and status modes 2013-11-07 16:45:04 +00:00
nick
866bf835e6 tests: Fix an uninitialized memory access 2013-10-30 22:46:49 +00:00
nick
53cbe14556 mirror: lengthen the request timeout to 60 seconds
This is complicated slightly by a need to keep the tests fast, so
we introduce an environment variable that can override the constant
2013-10-30 22:45:12 +00:00
nick
cd3281f62d acl: Make some compilers happy 2013-10-30 22:44:15 +00:00
nick
1e5457fed0 mirror: Couple of tiny cleanups 2013-10-30 22:04:41 +00:00
nick
0753369b77 mirror: Turn off the 'begin' timer before continuing 2013-10-30 20:25:50 +00:00
nick
9d9ae40953 Increase loglevel of some allocation map messages 2013-10-30 16:40:32 +00:00
nick
65d4f581b9 mirror: Clean up bps calculation slightly 2013-10-24 15:11:55 +01:00
nick
77c71ccf09 mirror: Ensure the bitset is actually disabled on mirror error 2013-10-23 16:18:00 +01:00
nick
97a923afdf mirror: Don't start migrating until the allocation map is built
There is a fun race that can happen if we begin migrating while the
allocation map is still building. We call bitset_enable_stream()
when the migration begins, which causes the builder to start putting
events into the stream. This is bad all by itself, as it slows the
migration down for no reason, but the stream is a limited-size queue
and there are situations (migration fails and is restarted) where we
can end up with the queue full and nobody able to empty it, freezing
the whole thing.
2013-10-23 15:58:47 +01:00
nick
335261869d mirror: Don't count bytes transferred for the purposes of keeping the stream empty as part of our bwlimit
This prevents a fairly nasty situation occurring where the rate of change on the disc is high enough that
just servicing it generates enough traffic to keep us over the bwlimit threshold indefinitely. That would
cause us to sleep during the only windows we'd ordinarily have to advance the offset.
2013-10-23 15:26:28 +01:00
nick
8cf9cae8c0 mirror: Don't sleep if our stream is filling up 2013-10-23 14:38:27 +01:00
nick
6986c70888 bitset: Swap pthread_cond_broadcast for pthread_cond_signal
Normally we'll only have one thread waiting anyway, but there's no
point activating a race here in the cases where we have > 1 waiting,
so signal is what we want.
2013-09-24 15:28:58 +01:00
nick
4b9ded0e1d bitset: More-efficient implementation of bitset_stream_queued_bytes
Rather than iterating the entire queue every time this function is
called, we instead take a small hit on enqueue and dequeue to keep
a running byte total keyed by event type that we can return.
2013-09-24 15:27:17 +01:00
nick
b177faacd6 mirror: Reduce the mirror convergence window to 5 seonds, from 60
Also remove some obsolete constants
2013-09-24 14:42:21 +01:00
nick
96e60a4a29 Added tag 0.1.0 for changeset acad9e9df53c 2013-09-24 12:27:29 +01:00
nick
d87af93cec tests: Add a migration test with many clients connecting in two waves 2013-09-24 10:11:40 +01:00
nick
bc50532321 Fix a current compiler warning 2013-09-23 17:15:56 +01:00
nick
22f92c5df0 Fix a potential compiler warning on 32-bit 2013-09-23 17:15:47 +01:00
nick
78fc65c515 bitset: Rename bitset_stream_on/off as bitset_enable/disable_stream 2013-09-23 17:10:14 +01:00
nick
5c1b119f83 serve: Fix calulation of server_mirror_bytes_remaining
Previously, we didn't count the number of bytes represented by events
in the stream; we just counted each pending event as one byte. Whoops.
2013-09-23 17:09:55 +01:00
nick
f4793c7059 bitset: Rename bitset_mapping to bitset 2013-09-23 16:58:40 +01:00
nick
0f0697a0aa serve: Remove an unused (and incorrect, in any case) function 2013-09-23 16:47:32 +01:00
nick
e98c2f2f05 serve: Fix the sense of allow/forbid_new_clients
We need a migration test where more clients connect after the gong
2013-09-23 16:46:43 +01:00
nick
ebe6c4a8ab mirror: Remove dead code. We still rely on all_dirty in one place. 2013-09-23 14:20:05 +01:00
nick
847b2ec9ad status: Remove useless stats 2013-09-23 14:19:49 +01:00
nick
ca9aea0d13 status: Expose migration_seconds_left 2013-09-23 14:09:25 +01:00
nick
0ae249009c serve/mirror: Move some code tracking migration speed into serve
The rationale is that status will need this information
2013-09-23 13:49:01 +01:00
nick
0f2225becf status: Display number of currently connected clients, and whether new clients are allowed
These will be useful for migration status monitoring - replaces "is pass == 7?"
2013-09-23 13:38:19 +01:00
nick
a6c175ed1d serve: Allow number of clients currently being used to be counted 2013-09-23 13:37:13 +01:00
nick
94654419c5 serve: Add a comment clarifying that a behaviour is safe 2013-09-23 10:53:55 +01:00
nick
e161121c7a flexnbd: Remove unused ".INCOMPLETE" file code
The original idea was that we'd create a .incomplete file at the destination
for mirroring, but that code was removed some time ago. This is all dead, now
2013-09-23 10:38:18 +01:00
nick
150e506780 flexnbd: Remove the server I/O lock as it no longer has any consumers 2013-09-23 10:29:06 +01:00
nick
9a3106f946 flexnbd: Remove the server I/O lock from around NBD requests
NBD doesn't actually guarantee what happens if you have two
concurrent writes to overlapping areas of the disc, and this
mutex was causing us a near-deadlock when the TCP connection
died uncleanly, partway through a request. So now we don't
bother. This actually removes the last user of the server I/O
mutex, so we can remove it completely from the codebase in a
future commit.
2013-09-23 10:22:48 +01:00
nick
71036730c4 Fix a warning in a test 2013-09-23 10:17:50 +01:00
nick
6553907972 mirror: Fix mirroring, break status
This removes the concept of 'passes' completely from mirror.c,
although it leaves the relevant bits in mirror.h to keep status from
failing - although its current code is now Wrong. FIXME.

We also now get the previous test passing, meaning mirroring works
again.
2013-09-20 17:08:14 +01:00
nick
9770bbe42b tests: Fix for the previous commit 2013-09-20 16:53:30 +01:00
nick
6ffa10bf89 flexnbd: Make a test a bit stricter 2013-09-20 16:00:56 +01:00
nick
eb80c0d235 mirror: Remove server I/O lock and dirty map
Given our bitset_stream events, we no longer need to worry about
keeping track of the dirty map. This also lets us rip out the
server I/O lock from mirroring.

It's possible that we can remove the lock from client.c as well at
this point, but I need to have a bit more of a think about possible
races
2013-09-19 15:18:30 +01:00
nick
a5c296f948 mirror: Fix a comment 2013-09-18 16:28:05 +01:00
nick
77a66c85a0 serve: Move bitset freeing to after closing the mirror and clients 2013-09-17 17:30:33 +01:00
nick
0172eb1cba flexnbd: Some comments and a minor fix in client.c to do with the event stream 2013-09-13 15:17:15 +01:00
nick
c3a5eb0600 bitset: add bitset_stream_size and bitset_stream_queued_bytes 2013-09-12 16:54:42 +01:00
nick
0a029fbbf5 bitset: Add an event stream implementation
Nothing is using it yet
2013-09-12 12:30:50 +01:00
nick
83426e1c01 tests: Update check_bitset to use new bitset_free() function 2013-09-11 16:09:27 +01:00
nick
86a000c717 bitset: Some whitespace changes 2013-09-11 15:48:19 +01:00
nick
54a41aacdf bitset: Add a bitset_free() function 2013-09-11 14:41:59 +01:00
nick
487bef1f40 flexnbd: Disconnect clients at the start of a mirror last pass
Currently, we prevent clients from processing requests by taking
the server I/O lock. This leads to requests hanging for a long
time before being terminated when the migration completes, which
is not ideal. With this change, at the start of the final pass,
existing clients are closed and any new connections will be closed
immediately (so no NBD server handshake will be seen).

This is part of the work required to remove remove the server I/O
lock completely.
2013-09-10 16:03:26 +01:00
nick
0494295705 mirror: Ensure the mirror client socket is closed after a fail, and before a retry 2013-08-27 15:54:59 +01:00
nick
14fde0f2a1 mirror: Remove overly-verbose log line 2013-08-21 14:41:19 +01:00
nick
e13d1d8fb4 mirror: honour max_bytes_per_second - naive scheme
If we're above max_bytes_per_second once we've finished a transfer
(8MB chunks, worst-case) then we delay the next transfer until
all_dirty_bytes / duration < max_bytes_per_second - checking once
per second.

If this isn't good enough, we can improve it - leaky bucket is one
option. To begin with, though, we'll mostly be using this to set
max_bps to either 0 or 100MB/sec or so. So it should be fine.
2013-08-14 16:24:50 +01:00
nick
efdd613968 listen: Turn off CLIENT_MAX_WAIT_SECS
The idea behind this feature was to avoid the client thread in a listen
server getting stuck forever if the mirroring thread in the source died.
However, it breaks any sane implementation of max_Bps in that thread,
and there are lingering concerns over how it might operate under normal
conditions anyway.

Specifically, if iterating over the bitmap takes a long time, or even just
reading the requisite 8MB from the disc in order to send it, then the
5-second timeout could be hit, causing mirroring to fail unnecessarily.
2013-08-14 16:09:55 +01:00
nick
d0022402ae mirror: Start our timeout watcher from the first, not second, transfer 2013-08-14 15:29:24 +01:00
nick
28fff91af1 flexnbd: add a mirror-speed command to change mirror->max_bytes_per_second
It's not actually honoured yet, and ideally, you'd also be able to set it as
part of the initial setup: "flexnbd mirror ... -m 4G". remote_argv for the
mirror case would need to become x=y z=w format first, though.
2013-08-14 13:33:02 +01:00
nick
385c9027db flexnbd status: display mirror->max_bytes_per_second as mirror_speed_limit 2013-08-14 13:30:25 +01:00
nick
b73081e417 One more fix 2013-08-13 16:22:44 +01:00
nick
cc468b0b17 control/mirror: Use uint64_t and strtoull to get max_Bps into the mirror 2013-08-13 12:30:18 +01:00
nick
7128fcc901 control: Output abandoned mirror state 2013-08-13 12:29:53 +01:00
nick
45355666f7 mirror: And another abandon fix 2013-08-12 16:14:53 +01:00
nick
8a294e5ee0 mirror: fix abandon 2013-08-12 15:54:49 +01:00
nick
c6764b0de1 mirror: abandon signals are now honoured outside of the remote end being readable / writable 2013-08-12 15:30:21 +01:00
nick
41facd2ccf Branch merge 2013-08-09 17:07:06 +01:00
nick
f6456349f7 Backed out changeset e58ff57b5e2d
Slows tests down
2013-08-09 17:06:56 +01:00
nick
9f4fbe782c Branch merge 2013-08-09 17:03:25 +01:00
nick
8c750a5e9d listen: Allow longer gaps between transfers 2013-08-09 17:02:58 +01:00
nick
64702d992d Minor fixes here and there 2013-08-09 17:02:33 +01:00
nick
c2df38c9d3 mirror: Use libev to provide an event loop inside the mirror thread
We're doing this so we can implement bandwidth controls sanely.
2013-08-09 17:02:10 +01:00
nick
754949d43f bitset: Add a bitset_run_count_ex that lets you learn the value of the bits in the run 2013-08-09 16:49:38 +01:00
lupine
1a966ca0be bitset: Prove that bitset operations with len=0 don't underflow 2013-07-26 17:09:21 +01:00
nick
f590f8ed3c status: Add migration_speed ( bytes per second ) and migration_duration( seconds ) to the migration output 2013-07-26 11:50:01 +01:00
nick
bc9ce93648 bitset: squash one more bug 2013-07-25 10:58:50 +01:00
nick
a5870b8e9b Remove a stray debugging statement 2013-07-25 10:14:14 +01:00
nick
bed8959d47 bitset: Fix large runs 2013-07-24 17:42:08 +01:00
nick
5c59a412af flexnbd-proxy: ensure upstream cooldown is applied when read init from upstream fails 2013-07-24 16:01:38 +01:00
nick
253cee5a10 flexnbd: Acknowledge new return type of bitset_run_count 2013-07-24 15:08:29 +01:00
nick
7de22a385e flexnbd: clients should be MADV_RANDOM, rather than MADV_SEQUENTIAL 2013-07-24 14:18:23 +01:00
nick
14db3315ca non-debug builds get -O2 for impressive bitset speedups 2013-07-24 12:34:36 +01:00
nick
efe9eaef7c bitset: A more-efficient bit(set)_run_count 2013-07-24 12:03:24 +01:00
nick
f8fd4e0437 bitset: Actually enable an optimization in bit_set/clear_range
Previously, we were setting bits up to the first byte boundary,
memset()ing to the last byte boundary, then ignoring the memset()
and resetting every single bit up to the last one individually,
from where the first for-loop left off.

This should be *at least* nine times faster.
2013-07-24 11:19:52 +01:00
nick
9a37951aaa bitset: Use uint64_t everywhere to avoid possible integer overflows
Hasn't been a problem in practice, mind.
2013-07-24 10:34:22 +01:00
nick
d18423c153 tests: Fix a couple of compile warnings 2013-07-23 17:22:23 +01:00
nick
1b0fe24529 test: Add some tests for bitset_run_count 2013-07-23 17:13:40 +01:00
nick
5c5636b053 flexnbd mirror: If the final run would be longer than the file size, truncate to file size
This fixes migrations of images that are not exactly divisible by 4096
2013-07-23 11:00:51 +01:00
nick
afe76debf7 flexnbd status: Actually output pass statistics 2013-07-08 14:27:04 +01:00
nick
f4bfc70a4b flexnbd status: Add current pass clean/dirty byte statistics 2013-07-08 13:51:15 +01:00
nick
b29ef6d4de flexnbd status: Avoid a possible NULL dereference reading migration status
While the mirror mutex is taken, the mirroring can be abandoned and serve->mirror
set to NULL, so we need to lock around reading information from serve->mirror
2013-07-08 13:32:14 +01:00
nick
dee0bb27d6 flexnbd status: Add the size of the backing file, in bytes
This will be handy information if you're querying flexnbd for migration
stats, particularly.
2013-07-08 10:11:18 +01:00
nick
f556f298b1 flexnbd status: Add current migration pass to the status output if we're migrating 2013-07-08 09:58:31 +01:00
nick
55b452ebef Fix tests for new killswitch argument 2013-07-03 10:04:08 +01:00
nick
9f34752842 flexnbd: Make the killswitch runtime-selectable
We're not actually using it in production right now because it doesn't
shut its sockets down cleanly enough. This is a better option than
reverting the functionality or keeping production downgraded until
we sort out a handler that cleanly closes the sockets.
2013-07-03 09:56:35 +01:00
nick
81d41f567d proxy: Reduce the reconnect cooldown from 15 seconds to 3.
Exponential backoff would be better, but that's OK
2013-06-20 10:26:34 +01:00
nick
89fd18f6f0 proxy: Add a 30-second timeout for requests in-flight to upstream
It's a little more complicated than that, actually. For the various
states that involve reading from, or writing to, the upstream fd,
if the amount of time spent in that state is > 30 seconds, we reconnect
to the server and resend the request.

we also introduce a 15-second reconnect dampener to keep us from stressing
things unduly. This may need to be decreased, or turned into an exponential
backoff, at some point.
2013-06-19 16:36:19 +01:00
nick
3c56ba0af6 proxy: Fix a comment 2013-06-19 11:27:09 +01:00
nick
2a9884e9e9 proxy: Fix the prefetch code 2013-06-19 11:18:52 +01:00
nick
1afea5c73d proxy: Respect the REQUEST_MASK 2013-06-19 11:18:22 +01:00
nick
62bdad2a6e ioutil: Add a bit more debug output to iobuf_read/write 2013-06-19 11:17:46 +01:00
nick
cd0a1f905f proxy: The minor optimisation bugs if needle is not advanced on iobuf_read() 2013-06-19 11:16:35 +01:00
nick
2156d06368 proxy: DRY up some code 2013-06-18 16:58:39 +01:00
nick
b14bba36ec proxy: Set proxy->upstream_fd before calling proxy_finish_connect_to_upstream
The only thing this affects is a log message
2013-06-18 15:58:38 +01:00
nick
f5c434f21c proxy: Initial move to event-loop proxy model.
Building with -DPREFETCH is currently broken, I'm sure, but otherwise
this version seems to be feature-complete compared to the previous one,
albeit wordier. Upcoming: cleanups
2013-06-18 15:37:39 +01:00
nick
662b9c2d07 readwrite: Expose a couple of points of functionality 2013-06-18 15:36:15 +01:00
nick
197c1131bf tests: Tell us which offset fails 2013-06-18 15:35:24 +01:00
nick
cecf2ebc77 proxy: log details of a request that fails upstream at the warn level 2013-06-07 12:12:12 +01:00
nick
f7e5353355 serve: Add a killswitch that causes the server to uncleanly exit on hang
We define a hang as 120 seconds for now; that should be OK (famous last words).
When I say unclean, I mean it; the control socket is left hanging around too.

This is a workaround for the fact that the client can hang the whole server by
sending a write request header specifying > 0 bytes, then uncleanly going away.
On the server side, we acquire the IO mutex, and then try to read > 0 bytes from
the socket; the data never arrives, and when the client reconnects, its requests
never get a response (since we're waiting on that mutex). Getting rid of that
mutex (which isn't actually needed, except for migration) would be better.
2013-06-06 14:16:20 +01:00
nick
f9fe421472 proxy: Some logging cleanups
New scheme:

Individual requests and extra information about stuff are debug
Lifecycle events are now info.
Problems doing anything are warn.
2013-06-06 12:24:28 +01:00
nick
1b6c10926f docs: Fix the documentation for the loglevel timestamps
We're actually using the system monotonic clock.
2013-06-06 12:23:14 +01:00
nick
24858fcde5 logging: Add a timestamp to the log messages we emit 2013-06-06 11:57:05 +01:00
nick
26c7f1b1c4 mirror: munmap() our range on cleanup 2013-05-30 11:09:24 +01:00
nick
055836c8cb mirror: Don't undo the MADV_SEQUENTIAL hinting over the course of a migration 2013-05-30 11:06:15 +01:00
nick
76cf2dc7b9 mirror: Only say we're unlinking the file if we actually are 2013-05-30 11:05:26 +01:00
nick
a5a7d45355 flexnbd: Add more madvise() hints, both for mirroring out and normal operation.
This is hopefully going to reduce flexnbd rss
2013-05-28 14:16:49 +01:00
Alex Young
e548cc53c8 Formatting fixup 2013-05-01 11:02:46 +01:00
nick
151b739e8d Automated merge with ssh://dev/flexnbd-c 2013-04-30 15:50:09 +01:00
nick
d9b3aab972 flexnbd: Pass MS_INVALIDATE to our msync calls
It's not necessary on Linux, but may be needed elsewhere
2013-04-30 11:04:17 +01:00
Alex Young
574d44f17f Add a trivial read buffer to flexnbd-proxy.
Since the vast majority (something like 94% on boot) are sequential small
reads, and since network latency is a major factor in determining how fast the
exposed device appears to the client, it makes sense for us to try to minimise
the number of network requests where we safely can.

This patch implements the simplest possible read cache in flexnbd-proxy.  When
it receives a read request, if it's a small request then flexnbd-proxy will
double the length of data requested.  On receiving the data from the upstream
server, flexnbd-proxy will return the first half to the downstream as normal,
and stash the second half in a buffer.  If the very next request is a read, and
the offset and length match those of what we have stored, that second request
will be satisfied from the buffer without going out over the network.

The cache is invalidated by any non-read request, or by a disconnection.
2013-04-29 14:50:42 +01:00
nick
33ee19dc5a flexnbd-proxy: Add UNIX socket support for the listen address 2013-04-15 16:52:54 +01:00
nick
4e70db8d7f readwrite.c: Set TCP_NODELAY on our NBD client sockets 2013-04-15 15:13:44 +01:00
nick
6984d3709e flexnbd: Don't bind() unless a bind address is specified 2013-04-09 11:47:32 +01:00
nick
2bb8434128 sockutil: Make sockaddr_address_string conform to its comment 2013-03-19 14:47:50 +00:00
nick
e994b80756 proxy: Switch to blocking I/O with signal handlers to exit.
It's safe to terminate the proxy at any point in its lifecycle, so
there's no point using signalfd() (and the associated select() +
non-blocking I/O gubbins) in it. We might want to use non-blocking
I/O in the future for other reasons, of course, at which point it
might become sensible to use signalfd() again. For now, this makes
us reliably responsive to TERM,INT and QUIT in a way that we weren't
previously.
2013-03-19 14:39:04 +00:00
nick
5257e93cb7 flexnbd: Split the proxy mode out into its own binary.
"flexnbd-proxy ..." should be identical in operation to "flexnbd proxy ..."
2013-03-19 13:13:37 +00:00
nick
21ac3cd0ed proxy: Deal with close() failures (and EINTR errnos) comprehensively 2013-03-15 12:07:16 +00:00
nick
f89352aa28 Add an explanatory comment in sock_try_connect() 2013-02-28 12:14:07 +00:00
nick
1d9f055dc7 Turn a couple of FIXME fatals in readwrite.c into warnings 2013-02-28 12:07:21 +00:00
nick
e659a78855 proxy: Fix the return value of a function to match the comment 2013-02-25 15:53:19 +00:00
nick
78299de299 Dummy commit to get past a merge commit 2013-02-21 13:57:33 +00:00
nick
6842864e74 Automated merge with file:///home/lupine/Development/bigv-repos/flexnbd-c-sockutil 2013-02-15 16:53:18 +00:00
nick
98d8fbeaf0 flexnbd: Add a proxy mode
This lets us proxy connections between NBD clients and servers, resiliently.
2013-02-15 16:52:16 +00:00
nick
9b67d30608 serve: Make some error conditions non-fatal, test them.
We don't want flexnbd serve to fall over and die if the client sends an invalid request.
2013-02-15 16:51:28 +00:00
nick
63f7e3e8d4 Fix some sockutil tests 2013-02-15 16:48:23 +00:00
nick
9826dc6c65 Automated merge with ssh://dev/flexnbd-c 2013-02-15 13:36:15 +00:00
nick
0324d3000d branch merge 2013-02-15 13:35:42 +00:00
nick
91085b87fc flexnbd: Add valgrind suppressions for a bug in glibc-2.11 2013-02-15 13:35:21 +00:00
nick
dfa7e1a21b serve: Don't die horribly in the event of EINTR being returned by select() 2013-02-14 16:38:45 +00:00
nick
8281809f42 flexnbd: Fix sock_try_bind so we don't retry on EADDRINUSE 2013-02-14 16:37:14 +00:00
nick
03bc12dd57 flexnbd read/write: Switch to a non-blocking connect() to allow us to time these out 2013-02-14 16:24:10 +00:00
nick
58c4a9530b Make acceptance tests verbose by default 2013-02-14 11:17:44 +00:00
nick
cb7eed28e7 sockutil: Add some tests for sockaddr_address_string 2013-02-13 15:07:30 +00:00
nick
ac560bd907 serve: Refactor some socket utility code into its own module.
We'll be using this in proxy mode later
2013-02-13 13:43:52 +00:00
nick
0fcbe04f80 flexnbd: Remove some obsolete 'rebind' options
They steal short options that I want for other things
2013-02-13 13:11:20 +00:00
nick
f63be84d80 flexnbd: Add some more information to nbdtypes.h 2013-02-08 17:05:22 +00:00
nick
8c04564645 flexnbd: Avoid a SIGSEGV when the allocation map fails to build.
In the event of a fiemap ioctl failing (when the file is on a tmpfs,
for instance), we would free() serve->allocation_map, but it would
remain not NULL, leading to segfaults in client.c when responding to
write requests.

Keeping the free() behaviour is more hassle than it's worth, as there
are synchronization problems with setting serve->allocation_map to
NULL, so we just omit the free() instead to avoid the segfault. This
is safe because we never consult the map until allocation_map_built is
set to true, and we never do that when the builder thread fails.
2013-02-08 16:17:16 +00:00
nick
ecfd108a53 Introduce socket_nbd_write_hello() and a macro to display errno results nicely 2013-02-08 15:53:27 +00:00
nick
56ce7d35c2 Add a debug message for cases where sendfile() fails 2013-02-06 14:41:49 +00:00
nick
2dd3db95bc Automated merge with file:///home/zander/00-projects/17-bigv/new-trial/src/incoming/flexnbd-c 2013-02-06 12:17:40 +00:00
nick
184a13bc9f Add an all-debug task to the makefile 2013-02-05 13:46:55 +00:00
nick
0b3a71bb03 flexnbd: Allocate the right amount of memory for a struct client 2013-02-05 13:27:48 +00:00
nick
719bd30071 Add a minimal Makefile that lets 'make' and 'make clean' do the Right Thing 2013-02-05 09:44:59 +00:00
nick
1afba29b63 flexnbd: Normalise some variable declarations 2013-02-01 15:20:43 +00:00
nick
7583ffbc4d flexnbd: constantize the quiet log level 2013-02-01 15:06:47 +00:00
Alex Young
f002b8ca1f madvise after mirroring to control the RSS 2012-12-28 11:38:54 +00:00
Alex Young
00d7237f66 Remove an errant debug output from test_happy_path.rb 2012-11-21 09:26:12 +00:00
Alex Young
ed70dacf2f Don't skip parts of a file when calling fiemap
A mis-incremented offset in the fiemap-processing code meant that
non-sparse portions of files were missed.
2012-11-20 17:24:19 +00:00
Alex Young
4f650d85c2 Fix the error message for flexnbd write --help 2012-11-20 15:09:48 +00:00
Alex Young
dcef6d29e5 Allocate the bitset in the foreground thread.
This prevents the possibility of a race in dereferencing it in the
client threads.
2012-10-09 17:54:00 +01:00
Alex Young
22bea81445 Don't open the control socket until after the server socket is bound
This makes it easier for the tests (and supervisor) to guarantee to be
able to connect to the server socket.

Also this patch moves freeing the mirror supervisor into the server
thread.
2012-10-09 17:35:20 +01:00
Alex Young
83eb31aba4 Merge 2012-10-09 17:28:41 +01:00
Alex Young
161d2fccf1 Rename serve->has_control to serve->success.
This makes the use of this variable to signal an unexpected SIGTERM
while migrating less confusing.
2012-10-09 17:20:39 +01:00
mbloch
029ebb5ef4 Fixed build_allocation_map in ioutil.c to correctly traverse fiemaps where
there are more than 1000 extents in a 100MB file chunk.
2012-10-08 18:11:21 +01:00
Alex Young
a039ceffcb Merge 2012-10-08 16:02:37 +01:00
Alex Young
062ecca1fd Backed out changeset c25e7d82e56e
This causes test failures under valgrind, and we don't need the
reordering with a background allocation map builder.
2012-10-08 16:01:25 +01:00
Alex Young
cf62b10adf Nullcheck *before* dereferencing.
Also bracketing, replacing a lost comment, and some variable naming.
2012-10-08 14:54:10 +01:00
Matthew Bloch
a49cf14927 Block allocation map is now built in a separate thread, and does not delay
server startup (sparse write avoidance doesn't happen until it is finished).
Added mutex to bitset functions, which were already being called from
multiple threads.  Rewrote allocation map builder to request file
information in multiple chunks, to avoid uninterruptible wait and dynamic
memory allocation.
2012-10-07 21:55:01 +01:00
Matthew Bloch
7b13964c39 Update Rakefile to support locally-installed libcheck, removed efence, pushed
-l arguments to end of link command line.
2012-10-07 02:09:34 +01:00
Alex Young
1fa8ba82a5 Merge 2012-10-04 14:51:54 +01:00
Alex Young
f3e0d61323 Quit with an error status on SIGTERM during migration
This prevents the supervisor from thinking that the migration completed
successfully.

In order to do this, I've introduced a new lock around the start (and
finish) of the migration so that we avoid a race between the signal
handler in the server_accept loop and the control thread mirror startup.
Without that, we'd risk successfully starting a migration after the
SIGTERM handler fired, which would be Bad.
2012-10-04 14:41:55 +01:00
nick
32cae67a75 flexnbd: Move building the allocation map to before server socket bind()
Building the allocation map takes time, which scales with the size of the disc
being presented. By building that map in the space between bind() and accept(),
we leave the process in a useless state after the only good signal we have for
"we are ready" and the state where it is actually ready. This was breaking
migrations of large files.
2012-09-25 11:47:44 +01:00
nick
ccbfce1075 Whitespace 2012-09-20 13:37:48 +01:00
Alex Young
ddc57e76d1 Remove an unneeded sanity check from the tests 2012-09-13 15:13:20 +01:00
Alex Young
1d9c88d4ca Add the write-during-migration test to the acceptance test run 2012-09-13 14:41:50 +01:00
Alex Young
8b43321ef2 Fix for deadlocks when writing while migrating 2012-09-13 12:21:43 +01:00
nick
13328910c8 Add a test case that tickles a deadlock bug when migrating active source discs 2012-09-12 17:13:33 +01:00
Alex Young
50001cd6e7 Merge 2012-09-12 15:43:15 +01:00
Alex Young
ccf5baa956 Add a -dbg package to the debian build 2012-09-12 15:42:58 +01:00
nick
ee652a2965 Fix some races in the acceptance tests 2012-09-11 16:21:35 +01:00
nick
e724d83bec Ensure fiemap ioctl calls are synchronous. 2012-09-11 15:37:13 +01:00
Alex Young
239136064a Add default empty LDFLAGS 2012-08-24 09:32:33 +01:00
Alex Young
c3c621f750 Don't free a client which hasn't finished yet. 2012-08-23 17:51:19 +01:00
Alex Young
c5dfe16f35 Don't close the same file descriptor more than once. 2012-08-23 16:01:37 +01:00
Alex Young
b1a4db2727 Further merge fail fix
The reversal of the control protocol lines for the mirror command wasn't
complete.
2012-07-24 14:19:53 +01:00
nick
2c0f86c018 Fix a merge fail 2012-07-24 09:21:40 +01:00
Alex Young
53eca40fad Fix tests broken by entrust removal
Missed check_readwrite and check_flexnbd
2012-07-23 15:45:39 +01:00
Alex Young
33f95e1986 Add the --unlink option to mirror
This deletes the local file before tearing down the mirror connection,
allowing us to avoid an ambiguous recovery situation.
2012-07-23 13:39:27 +01:00
Alex Young
fd935ce4c9 Simplify the migration handover protocol
The three-way hand-off has a problem: there's no way to arrange for the
state of the migration to be unambiguous in case of failure.  If the
final "disconnect" message is lost (as in, the destination never
receives it whether it is sent by the sender or not), the destination
has no option but to quit with an error status and let a human sort it
out.  However, at that point we can either arrange to have a .INCOMPLETE
file still on disc or not - and it doesn't matter which we choose, we
can still end up with dataloss by picking a specific calamity to have
befallen the sender.

Given this, it makes sense to fall back to a simpler protocol: just send
all the data, then send a "disconnect" message.  This has the same
downside that we need a human to sort out specific failure cases, but
combined with --unlink before sending "disconnect" (see next patch) it
will always be possible for a human to disambiguate, whether the
destination quit with an error status or not.
2012-07-23 10:22:25 +01:00
Alex Young
f6f4266fd6 Update the README for new listen behaviour
Get rid of references to rebind addresses and update the usage examples.
2012-07-23 10:10:47 +01:00
Alex Young
4790912750 Remove listen mode
Changing behaviour so that instead of rebinding after a successful
migration and continuing as an ordinary server, we simply quit with a
0 exit code and let our caller restart us as a server if they want to.
This means that everything in listen.c, listen.h, and anything making
reference to a rebind address is unneeded.
2012-07-23 09:48:50 +01:00
Alex Young
77f4ac29c6 Include strerror(errno) in stat debug output 2012-07-20 09:51:53 +01:00
Alex Young
b0f1a027c6 Add .INCOMPLETE file marker to flexnbd listen
We drop a marker onto the filesystem to say when we know the image we're
serving is not yet ready.
2012-07-19 17:34:20 +01:00
Alex Young
76bbdb4889 Force gzipping the man page 2012-07-19 17:22:25 +01:00
Alex Young
314c0c2a2a Added the flexnbd break command to stop mirroring 2012-07-17 16:30:49 +01:00
Alex Young
1caa3d4e27 Make an EADDRINUSE on server bind fatal.
This is important because if we try to rebind after a migration and
someone else is in the way, any clients trying to reconnect to us will
instead be connecting to the squatter.
2012-07-16 12:34:39 +01:00
Alex Young
2e20e7197a Add the pid to the status output
This will be needed if we daemonise flexnbd.
2012-07-16 11:50:59 +01:00
Alex Young
8814894874 Test setting an ACL 2012-07-16 11:38:01 +01:00
Alex Young
66ff06fe0e Block a second mirror attempt
If a second mirror command is run while the first is still going,
flexnbd needs to prevent the second because we only have one dirty map.
Also, the shutdown becomes Complicated if we allow more than one mirror
at a time.
2012-07-16 11:21:56 +01:00
Alex Young
db30ea0c48 Better error handling for remotes 2012-07-16 11:04:45 +01:00
Alex Young
9a81af5f8f Added tag 0.0.2 for changeset 99b403167181 2012-07-16 10:49:03 +01:00
Alex Young
484a29b3f6 Add README.txt to the deb task code files 2012-07-16 10:29:06 +01:00
Alex Young
d0b39cce08 Flush bad write data from the client socket.
If the client makes a write that's out of range, by the time we get to
validate the message at the server end the client has already stuffed
the socket with data we can't use, so we have to flush it.

This patch also fixes a potential problem in the acceptance tests where
the error field was being returned as an array rather than a value.
2012-07-15 23:19:12 +01:00
Alex Young
f5850e5aaf Switch from expecting a reconnection to *not* doing do
If we're aborting mirror operations early, a couple of specs need to
change sense.
2012-07-15 22:07:00 +01:00
Alex Young
10625e402b Move the mirror commit state mbox to struct control
The mirror_super signals the commit state to the control thread via an
mbox, and this mbox is moved to control.  It was owned by mirror_super,
but the problem with that is that mirror_super can free the mbox before
the control client has been scheduled to receive the message.  If it's
owned by the control object, that can't happen.
2012-07-15 21:57:36 +01:00
Alex Young
b20fbc6a66 Don't retry a mirror which failed on the first attempt
If the mirror attempt failed and we were able to report an error to the
user, it makes no sense to attempt a retry.  We don't have a way to
abort a mirror attempt yet, so if the user got a setting wrong and it's
failing for that reason, the only recourse they'd have would be to
restart the server.
2012-07-15 20:07:17 +01:00
Alex Young
a10adf007c Switch the mirror commit_signal to an mbox
At the moment, a first-pass failed migration will retry. This is wrong,
it should abort.  However, to make that happen the mirror supervisor
needs to know the commit state of the mirror thread.  With a self_pipe
mirror commit signal that information wasn't there.
2012-07-15 19:46:35 +01:00
Alex Young
5794913fdf Delete the MS_FINALISE mirror state
It's not being used for anything.
2012-07-15 18:40:50 +01:00
Alex Young
e77234c6b1 Close the mirror client socket on rejection
If the mirror attempt connects ok, but is rejected (say, for reporting
the wrong size), the client socket needs to be closed.  The destination
end can't close its socket and accept another connection attempt unless
it does.
2012-07-15 18:30:20 +01:00
Alex Young
e0a61e91e6 Simplify acceptance test launching
Get rid of checking for --verbose, since it's always there now
2012-07-15 17:14:22 +01:00
Alex Young
f7379e3278 Tweak help output for the --bind option
Each option's parameter should be unique - they're instances, not
classes
2012-07-14 21:43:27 +01:00
Alex Young
a1ea2ba4c5 Add a rake task to build the man page
Also tweak the debian .install to put it in the right place.
2012-07-14 18:47:25 +01:00
Alex Young
54a1409dce Added a README.txt and a man page
Spoiler: they're the same thing. Added a `rake man` task to build the
man page.  Depends on asciidoc.
2012-07-14 18:36:02 +01:00
Alex Young
f9baa95b0f Raise the log level of a write-request-out-of-range
Without this, the error you get is a "Bad magic", when the next read
loop tries to read write data as a request.  This should be flushed from
the socket (although *when* is an open question), but upping the log
level at least gives us a more informative output.
2012-07-14 17:27:13 +01:00
Alex Young
69ad6d6b7a Only copy constants from C to Ruby once
This avoids unnecessary duplicate constant warnings for C constants that
are defined in two legs of an #ifdef.
2012-07-14 17:25:26 +01:00
Alex Young
b734a468c1 Make the --verbose flag universal
Previously, the --verbose flag was only present in debug builds. Now
it's present whether you define DEBUG or not.  What changes is the
amount of information printed to stderr: DEBUG sets the --verbose log
level to 0 (debug), while DEBUG unset sets it to 1 (info).  This makes
driving the binary slightly simpler as you don't have to detect whether
it's a debug build by scanning for "--verbose" in the help output.
2012-07-14 12:27:16 +01:00
Alex Young
768b30c4eb Clobber a dangling fprintf 2012-07-14 12:11:25 +01:00
Alex Young
1ce1003d3d Error when reading sent data fails
If the client cuts off part-way through the write, it should cause an
error, not a fatal.  Previously this happened if the open file had a
fiemap, but not if there was no allocation map.  This patch fixes that,
along with an associated valgrind error.
2012-07-14 12:10:12 +01:00
Alex Young
c6e6952def Open files with O_DIRECT dependent on a compile-time DIRECT_IO #define.
O_DIRECT causes problems on (at least) a wheezy VM, and there are mixed
reports about its performance impact.  This patch makes it a
compile-time choice which should remain until it's been benchmarked.
2012-07-14 10:07:58 +01:00
Alex Young
03c06a689d Append the CFLAGS environment variable to the build flags.
This is going to be used for the DIRECT_IO flag.
2012-07-14 10:05:35 +01:00
Alex Young
e4d2b9a667 Make test sockets less dependent on enviroment
It seems that ruby in a default wheezy VM can't handle a source address
of nil.
2012-07-14 10:04:55 +01:00
Alex Young
2ea5a2e38a Unlink the control socket on clean shutdown
Previously, the behaviour was to unlink any control socket sat where we
wanted to open ours.  This would make us lose control of running servers
if we happened to collide accidentally.  With this patch, the new
process will abort() if there is a control socket squatting on the
path we want, and unlink it when it closes.

This means that an unclean shutdown will leave a dangling, unattached
control socket which will block a restart, but that's a better option
than intentionally cutting off running servers.
2012-07-13 14:09:52 +01:00
Alex Young
a838714571 Tweak the fuzz script to work with the new test layout 2012-07-13 13:13:04 +01:00
Alex Young
fd8ee5b8c3 Tweak the parse_acl declaration
Array lengths don't make sense in function declarations.
2012-07-13 12:37:21 +01:00
Alex Young
15109c72d1 Add a newline to log messages at macro expansion
This simplifies building the log output because it means we don't have
to malloc a buffer to append a newline, and we keep the atomic write
property we're after.  It also takes advantage of the C constant string
concatenation which we already require to work to prepend the thread and
pid data.
2012-07-13 12:18:19 +01:00
Alex Young
9f4da5def0 Switch to use nbd_r2h_reply in read_reply()
Use a wrapper function to simplify the reply field reading.
2012-07-13 12:13:55 +01:00
Alex Young
40101e49f3 Silence a vfprintf valgrind error
Turns out that %lld causes valgrind to find an uninitialised variable
problem inside vfprintf.  Avoid it here by s/%lld/%d/.
2012-07-13 11:57:46 +01:00
Alex Young
2a50b64a43 Free the flexnbd switch mutex 2012-07-13 11:31:22 +01:00
Alex Young
00e912d0a6 Add a 'just in case' error case to acl checking 2012-07-13 10:16:44 +01:00
Alex Young
2f24d02a8f Remove unused variables
use_connect_from in control_mirror() and success in mode_serve() are no
longer used.
2012-07-13 09:34:18 +01:00
Alex Young
2e4e592c08 Enable writing after the 2G boundary
This patch fixes a bug in readwrite.c which truncated the 'from' field
in nbd requests.  It was casting them down from an off64_t to an int.
2012-07-12 18:01:10 +01:00
Alex Young
cef2dcaad2 Rename struct mirror_status to struct mirror 2012-07-12 14:54:48 +01:00
Alex Young
c6a084ce82 Add a --quiet command-line option
--quiet will suppress all log lines except FATAL.  Conceptually it's
exclusive with --verbose, but this isn't checked - last one wins.
2012-07-12 14:45:55 +01:00
Alex Young
10b46beeea Retry failed rebind attempts
When we receive a migration, if rebinding to the new listen address and
port fails for a reason which might be fixable, rather than killing the
server we retry once a second.  Also in this patch: non-overlapping log
messages and a fix for the client going away halfway through a sendfile
loop.
2012-07-12 14:14:46 +01:00
Alex Young
9002341e77 Fix the broken --rebind-port command-line option. 2012-07-12 10:45:19 +01:00
Alex Young
71b7708964 Minor tidy 2012-07-12 10:22:31 +01:00
Alex Young
eb90308b6e Handle a failed disconnect correctly
If the sender disconnects its socket before sending the disconnect
message, the destination should restart the migration process.  This
patch makes sure that happens.
2012-07-12 09:39:39 +01:00
Alex Young
f3cebcdcd5 Test a source crashing after an entrust.
This adds a test for destination behaviour, in that if a source crashes
after sending an entrust message but before the destination can reply,
the destination must allow the source to reconnect and retry the mirror.
2012-07-11 15:19:50 +01:00
Alex Young
84dd052465 Fix a test broken by stdout/stderr reshuffle 2012-07-11 10:12:10 +01:00
Alex Young
f3f017a87d Free all possibly held mutexes in error handlers
Now that we have 3 mutexes lying around, it's important that we check
and free these if necessary if error() is called in any thread that can
hold them.  To do this, we now have flexthread.c, which defines a
flexthread_mutex struct.  This is a wrapper around a pthread_mutex_t and
a pthread_t.  The idea is that in the error handler, the thread can
check whether it holds the mutex and can free it if and only if it does.
This is important because pthread fast mutexes can be freed by *any*
thread, not just the thread which holds them.

Note: it is only ever safe for a thread to check if it holds the mutex
itself.  It is *never* safe to check if another thread holds a mutex
without first locking that mutex, which makes the whole operation rather
pointless.
2012-07-11 09:43:16 +01:00
Alex Young
17fe6d3023 Test that a blocked entrust causes a retry 2012-07-03 18:00:31 +01:00
Alex Young
061512f3dc Test that a write reply with the wrong magic will force a retry 2012-07-03 17:01:39 +01:00
Alex Young
5c66d35677 Test that closing the socket immediately after sending write data causes an error 2012-07-03 15:33:00 +01:00
Alex Young
d16aebf36e Test that a disconnect after the write request but before the data is an error 2012-07-03 15:25:39 +01:00
Alex Young
a767d4bc8c Test the source handles a dest crash after write correctly 2012-07-03 14:52:27 +01:00
Alex Young
64ebbe7688 Refactor FakeSource from a module to a class 2012-07-03 14:39:05 +01:00
Alex Young
ded4914c84 Simplified FlexNBD::FakeDest 2012-07-03 14:23:20 +01:00
Alex Young
9e67f228f0 Rename a test class 2012-07-03 13:35:47 +01:00
Alex Young
2283b99834 Split acceptance tests into separate files 2012-07-03 13:33:52 +01:00
Alex Young
988b2ec014 Moved acceptance tests into tests/acceptance 2012-07-03 10:59:31 +01:00
Alex Young
c0c9c6f076 Moved unit tests into tests/unit 2012-07-03 10:53:08 +01:00
Alex Young
e817129c47 Changes to error severity in readwrite.c made a test fail, this patch fixes it 2012-07-02 18:10:02 +01:00
Alex Young
cc2e67d4bb Test that an invalid write gets an error response 2012-07-02 15:37:52 +01:00
Alex Young
ea4642a878 Check that a mirror write returning an error will cause a reconnect and retry 2012-07-02 15:04:45 +01:00
Alex Young
99f8c24b01 Tweak a timeout to prevent an intermittent test failure 2012-07-02 13:00:30 +01:00
Alex Young
9850f5d0a4 Test that timing out a write causes a disconnect and a reconnect 2012-06-28 14:45:53 +01:00
Alex Young
4de4cee3d0 Test for acl rejection 2012-06-28 13:29:22 +01:00
Alex Young
c9fdd5a60e Handle ECONNRESET during a read request 2012-06-28 11:46:02 +01:00
Alex Young
9b717d6391 Factor common code out of fake destinations 2012-06-28 11:34:36 +01:00
Alex Young
192471ee82 Factor common code out of the test fake sources
* * *
More fake source refacoring
2012-06-27 17:28:24 +01:00
Alex Young
137a764cc7 Add a test for a second client connecting during a mirror 2012-06-27 16:32:01 +01:00
Alex Young
cea9d97086 Missing file 2012-06-27 16:19:13 +01:00
Alex Young
04a10179a0 check_acl correctly sets log_level 2012-06-27 16:18:38 +01:00
Alex Young
ac3e6692a8 make sure that an invalid flexnbd signal fd can't break the serve accept loop 2012-06-27 16:17:51 +01:00
Alex Young
94b4fa887c Add mboxes 2012-06-27 15:45:33 +01:00
Alex Young
2078d17053 connect failure scenarios 2012-06-22 10:05:41 +01:00
Alex Young
80f298f6cd Make non-fatal errors return properly 2012-06-21 18:01:56 +01:00
Alex Young
f37a217cb9 Add listen mode 2012-06-21 18:01:50 +01:00
Alex Young
79ba1cf728 Make max_nbd_clients configurable per struct server 2012-06-21 17:22:34 +01:00
Alex Young
e21beb1866 Add the REQUEST_ENTRUST nbd request type 2012-06-21 17:12:06 +01:00
Alex Young
a3dc670939 Squash valgrind errors by making sure client threads get joined on termination 2012-06-21 17:11:12 +01:00
Alex Young
bafc3d3687 Make sure filename_incomplete gets freed 2012-06-21 15:58:32 +01:00
Alex Young
322eae137b Add a missed free() 2012-06-21 15:55:48 +01:00
Alex Young
43e95dc4db Make sure all the lines we read get freed (including the trailing blank) 2012-06-21 15:31:28 +01:00
Alex Young
cc22f50fe6 Avoid a use-after-free in serve.c 2012-06-21 14:15:58 +01:00
Alex Young
c054403208 Trim the length bitset_run_count looks at not to exceed the bits array 2012-06-21 12:05:01 +01:00
Alex Young
80fff4e0e6 Squash a valgrind error caused by debug output 2012-06-21 11:55:21 +01:00
Alex Young
4e8a9670e5 Merge 2012-06-21 11:37:18 +01:00
Alex Young
e3c04ade29 Added early-exit on any valgrind error 2012-06-21 11:37:00 +01:00
Alex Young
ed3090d6d5 Tweak struct initialisation to squash a valgrind error 2012-06-21 10:29:06 +01:00
Alex Young
50b0db7bf6 Reject mirroring if the remote size doesn't match the local size 2012-06-13 15:51:37 +01:00
Alex Young
c9ece5a63f Tidy mirror_runner somewhat 2012-06-13 15:45:59 +01:00
Alex Young
c2b6fac92d Fix an argv array reference (root cause of a bug from the last commit) 2012-06-13 13:52:15 +01:00
Alex Young
7d1c15b07a Fix two bugs in mirroring.
First, Leaving off the source address caused a segfault in the
command-sending process because there was no NULL check on the ARGV
entry.

Second, while the migration thread sent a signal to the server to close
on successful completion, it didn't wait until the close actually
happened before releasing the IO lock.  This meant that any client
thread waiting on that IO lock could have a read or a write queued up
which could succeed despite the server shutdown.  This would have meant
dataloss as the guest would see a successful write to the wrong instance
of the file.  This patch adds a noddy serve_wait_for_close() function
which the mirror_runner calls to ensure that any clients will reject
operations they're waiting to complete.

This patch also adds a simple scenario test for migration, and fixes
TempFileWriter#read_original.
2012-06-13 13:44:21 +01:00
Alex Young
b986f6b63e Take _GNU_SOURCE out of source and put it in CFLAGS 2012-06-13 09:59:08 +01:00
Alex Young
c7525f87dc Removed proxying completely and fixed the pthread_join bug revealed in the process 2012-06-12 15:08:07 +01:00
Alex Young
2a71b4e7a4 Fix broken error checking around pthread functions 2012-06-11 16:08:19 +01:00
Alex Young
5996c8f7ba Simplify a FATAL_IF_NEGATIVE 2012-06-11 15:31:59 +01:00
Alex Young
4c52bcd870 Make the error and fatal functions swallow semicolons properly 2012-06-11 15:26:42 +01:00
Alex Young
13a6a403a4 Make the error and fatal macros swallow semicolons properly 2012-06-11 15:23:06 +01:00
Alex Young
83b8b9eaac Add general-purpose ERROR/FATAL_IF and ERROR/FATAL_UNLESS macros 2012-06-11 15:20:05 +01:00
Alex Young
c6182b9edf Merge 2012-06-11 14:59:52 +01:00
Alex Young
e2d3161a4a Set default log level to warn to shut the tests up 2012-06-11 14:59:26 +01:00
nick
8513144354 Automated merge with ssh://dev/flexnbd-c 2012-06-11 14:40:53 +01:00
nick
5ab9e10019 test: make check_serve bind() its outgoing socket to a known IP for these tests 2012-06-11 14:40:41 +01:00
Alex Young
710d8254d4 Make sure all ifs are braced 2012-06-11 14:34:17 +01:00
Alex Young
25fc0969cf Make the compiler stricter and tidy up code to make the subsequent errors and warnings go away 2012-06-11 13:57:03 +01:00
Alex Young
8825f86726 Merge 2012-06-11 13:49:56 +01:00
Alex Young
b5427d13db Explicitly check for which fd is acceptable in server_accept 2012-06-11 13:49:35 +01:00
nick
893db71d7c Whitespace 2012-06-11 13:05:22 +01:00
nick
224bdcbf87 Fix handling ACLs where > 1 entry exists 2012-06-11 12:56:45 +01:00
nick
0b90517035 tests: Get rid of a warning 2012-06-11 10:08:24 +01:00
nick
0441ef9d74 tests: Get check_serve working after the merge of doom 2012-06-11 10:04:31 +01:00
Matthew Bloch
e8b5fae7ab Merge, just renaming old error macros. 2012-06-09 02:37:23 +01:00
Matthew Bloch
b546539ab8 Rewrote error & log functions to be more general, use longjmp to get out of
trouble and into predictable cleanup functions (one for each of serve,
client & control contexts).  We use 'fatal' to mean 'kill the thread' and
'error' to mean 'don't kill the thread', assuming some recovery action,
except I don't use error anywhere yet.
2012-06-09 02:25:12 +01:00
Matthew Bloch
8691533d88 Added hopeful default path to find rake_utils, turned undefined function
warnings into errors, and added expensive header scanning to .c->.o rule to
ensure changes to .h files cause recompiles as you'd expect.
2012-06-09 02:17:34 +01:00
Alex Young
b7096ef908 Audit client connections on acl update 2012-06-08 18:03:41 +01:00
Alex Young
35ca93b42c Lock around acl updates 2012-06-08 11:02:40 +01:00
Alex Young
f7e1a098b1 Move updating the acl object into serve.c
* * *
Replacing the server acl sends an acl_updated signal
2012-06-08 10:32:33 +01:00
Alex Young
5fb0cd4cca Fix O_NONBLOCK setting on self_pipes 2012-06-08 10:11:06 +01:00
Alex Young
2d9d00b636 Pull ACLs into their own struct 2012-06-07 17:47:43 +01:00
Alex Young
601e5b475a Tidy the NULLCHECK macro to swallow semicolons properly 2012-06-07 16:00:38 +01:00
Alex Young
c628435f77 Fix an invalid define symbol 2012-06-07 15:59:13 +01:00
Alex Young
1cd8f4660f Merge of doom 2012-06-07 14:40:55 +01:00
Alex Young
5930f25034 Use client stop signals for thread stopping 2012-06-07 14:25:30 +01:00
Matthew Bloch
40f0f9fab6 Big bit of debug output in write_not_zeroes (disabled). 2012-06-07 12:28:21 +01:00
Matthew Bloch
d763ab4e74 Fixed bug in bitset_run_count which was causing data corruptionn writing
around sparse boundaries.
2012-06-07 12:27:46 +01:00
Matthew Bloch
3810a8210f Added some record-keeping / printing to fuzzer to assist with backtracking. 2012-06-07 12:25:56 +01:00
Alex Young
a90f84972b Add stop signals to client threads 2012-06-07 11:44:19 +01:00
Matthew Bloch
5710431780 Refactored write_not_zeroes to use struct bitset_mapping instead of
repeating all that code (has not fixed earlier bug yet, but lots of
repetition cut).
2012-06-07 11:17:02 +01:00
Matthew Bloch
08f3d42b34 Improved fuzz test to find an actual code bug (previous bug was in the test
<g>).
2012-06-07 02:06:08 +01:00
Matthew Bloch
9fc3c061f8 Fixed arguments to debug function. 2012-06-07 01:15:29 +01:00
Matthew Bloch
8cf1a515dd Missing break; in switch statement (verbose was setting default deny!) 2012-06-07 00:01:11 +01:00
Alex Young
cfa9f9c71f Fix the sense of client_serve_request 2012-06-06 14:25:35 +01:00
Alex Young
e8b47d5855 Remove the accept lock as being unneeded 2012-06-06 14:07:55 +01:00
Alex Young
1fc76ad77f Merge 2012-06-06 13:44:49 +01:00
Alex Young
16001eb9eb Move checking for a closed client out of server_lock_io and into client_serve_request 2012-06-06 13:44:38 +01:00
nick
648f768ff6 tests: fix the Ruby flexnbd wrapper for mirror 2012-06-06 13:33:24 +01:00
Alex Young
1b289a0e87 Change io lock and unlock to server error on failure 2012-06-06 13:29:13 +01:00
Alex Young
9dbc0a31a8 Better error message 2012-06-06 13:19:24 +01:00
Alex Young
339e766339 Use self_pipe for close_signal 2012-06-06 12:41:03 +01:00
nick
14c9468b68 Automated merge with ssh://dev/flexnbd-c 2012-06-06 12:35:18 +01:00
nick
7544a59da1 mirror: Add --bind to our mirror mode.
Mirroring doesn't actually work yet, of course.
2012-06-06 12:35:01 +01:00
nick
f4a403842d flexnbd: Fix specifying -d as --default-deny on the command line 2012-06-06 12:07:40 +01:00
Alex Young
457987664a Renamed struct client_params to struct client 2012-06-06 11:33:17 +01:00
Alex Young
40279bc9ca Split client-specific code into client.{c,h} 2012-06-06 11:27:52 +01:00
Alex Young
d22471d195 Fix a \#define symbol 2012-06-06 10:55:50 +01:00
Alex Young
a80c5ce6b5 Moved sockaddr_address_data to serve.c and renamed params.h to serve.h 2012-06-06 10:45:07 +01:00
Alex Young
cc97dd4842 Rename control to control_fd and struct mode_serve_params to struct server 2012-06-06 10:35:50 +01:00
Alex Young
a0990b824c Merge 2012-06-06 10:24:33 +01:00
Alex Young
d7fa05d42c Backed out changeset 0cbb8e9cf515 because it breaks deb packaging. 2012-06-06 10:24:04 +01:00
Alex Young
78b1879cab Merge 2012-06-06 10:19:59 +01:00
Alex Young
059be22c27 Rename int server to int server_fd in mode_serve_params 2012-06-06 10:19:45 +01:00
nick
15513c03df Remove a duplicated line due to the last merge 2012-06-06 10:05:12 +01:00
nick
682f3c70ef Automated merge with ssh://dev/flexnbd-c 2012-06-06 10:03:46 +01:00
nick
3e0628e2fc flexnbd: Re-add --sock to flexnbd mirror 2012-06-06 09:55:47 +01:00
nick
8a2fd06c31 flexnbd: Add --bind to flexnbd read and flexnbd write 2012-06-06 09:55:08 +01:00
Matthew Bloch
60cb089e45 Added fuzzer which currently exposes ugly bug with unaligned writes. 2012-06-06 01:28:54 +01:00
Matthew Bloch
d981dde8d1 Fixed FlexNBD#serve parameters, added detection of non-starting server. 2012-06-06 01:28:30 +01:00
Matthew Bloch
2245385117 Added msync() call after every write - not sure whether it's necessary yet. 2012-06-06 01:27:37 +01:00
Matthew Bloch
29151b8a78 Isolated missing library code to pkg:deb task - couldn't locate library code
(must be available from Debian, or bundled).
2012-06-05 23:46:28 +01:00
Alex Young
d87d7a826f Rename the 'debug' cli option 'verbose' and switch default-deny from 'D' to 'd' 2012-06-01 16:58:32 +01:00
Alex Young
8511cacb03 Make sure the -d short option is honoured 2012-06-01 16:47:34 +01:00
Alex Young
29937cdcf9 Merge 2012-06-01 16:25:41 +01:00
Alex Young
1ddb3bb609 Add a self_pipe set of convenience functions 2012-06-01 16:25:27 +01:00
Alex Young
91ab715659 Indentation fix 2012-06-01 16:24:50 +01:00
nick
b985e97098 Automated merge with ssh://dev/flexnbd-c 2012-06-01 14:51:43 +01:00
nick
04d67b3bab acls: Add a default-deny option, which allows you to specify what an empty ACL means.
When this option is specified, an empty ACL means "reject all clients". Without it,
an empty ACL means "accept all clients"
2012-06-01 14:48:34 +01:00
Alex Young
9dbb107bf8 Use nbdtypes to write the nbd hello message 2012-05-31 20:33:42 +01:00
Alex Young
17ed766c74 Null-terminated strings strike again 2012-05-31 18:04:57 +01:00
Alex Young
185a840e03 Factor out the bulk of client_serve_request, and add convenience converters in src/nbdtypes.c 2012-05-31 17:44:11 +01:00
Alex Young
949d7d6a72 Don't check for the INCOMPLETE file on read 2012-05-31 14:11:57 +01:00
Alex Young
1aec12613c Ditch a couple of unneeded variables to silence gcc warnings 2012-05-31 14:09:35 +01:00
Alex Young
b90b73fba6 build and default rake tasks, because I keep trying to type them 2012-05-31 14:01:49 +01:00
Alex Young
49c4ef7c56 Add .orig merge files to .hgignore 2012-05-31 13:55:35 +01:00
Alex Young
81fe41f016 Merge 2012-05-31 13:53:21 +01:00
Alex Young
074efd9fa4 Add a no-op debug() define for non-debug builds and make valgrind optional in nbd_scenarios 2012-05-31 13:53:04 +01:00
Alex Young
c2d1414bff Merge 2012-05-31 13:32:56 +01:00
Alex Young
623a398767 Add a --debug flag for DEBUG builds
If you compile with:

  DEBUG=true rake build

then all the commands get a --debug flag as an option which will make
the server dump crazy amounts of data to stderr.
2012-05-31 13:31:22 +01:00
Alex Young
268bebd408 Run the nbd_scenario tests under valgrind 2012-05-31 13:23:12 +01:00
nick
71e755906b Make the Rakefile take note of DEBUG= 2012-05-31 12:12:32 +01:00
nick
e863bffe3d Set TCP_NODELAY on our socket. This decreases average NBD read request RTT from 0.3ms to 0.001ms 2012-05-31 11:33:31 +01:00
mbloch
c6dd4fbd89 Merge 2012-05-30 20:14:14 +01:00
mbloch
cd976d1c2c Fixed short copies of struct sockaddr (it's shorter than sockaddr_in6!)
which was giving duff results when comparing IPv6 ACL entries.
2012-05-30 20:13:56 +01:00
Alex Young
42599fe01e Make sure we build arch-specific packages 2012-05-30 18:11:32 +01:00
Alex Young
f21dd9e888 Basic debian packaging
Add a build dependency on rake_utils, but we get simple debian packages
out of it.
2012-05-30 17:35:07 +01:00
Alex Young
15c3133458 Simplify option definition with som handy macros 2012-05-30 17:33:38 +01:00
Alex Young
fe08084144 Added tag 0.0.1 for changeset 27409c2c1313 2012-05-30 17:11:10 +01:00
Alex Young
0102217019 Merge 2012-05-30 15:39:55 +01:00
Alex Young
0c62e66a70 Added getopt_long command-line handling.
All parameters now have switches.  The one gotcha is the parameter which
was overloaded - s_length_or_filename to params_readwrite - is only
pretending to be a length at the moment. If you pass a filename it'll
still work, but the help messages don't mention that.  I'll split the
parameter into two in a later commit.
2012-05-30 15:19:40 +01:00
Alex Young
a01621dc1e Added .h files to the Rakefile 2012-05-30 15:06:06 +01:00
mbloch
6d8afd1035 Fixed bug where ACL was accidentally deleted when being set from control
socket.
2012-05-30 13:03:02 +01:00
nick
46ceb85aec Fix the usage message 2012-05-30 11:28:32 +01:00
Alex Young
7832958522 Rearranged the project to have src/ and build/ directories
This simplifies keeping everything clean.
2012-05-30 09:51:20 +01:00
mbloch
cf2400fedd Fixed race in tests. 2012-05-29 17:01:54 +01:00
Matthew Bloch
21ccd17ea5 Added .INCOMPLETE hack to aid with marking finished transfers. 2012-05-29 11:24:24 +01:00
Matthew Bloch
ab0dfb5eca Added mirror write barrier / final pass stuff & clean exit afterwards.
Plenty of code documentation.
2012-05-29 04:03:28 +01:00
mbloch
dcb1633b8b Lots of errors spotted by Alex fixed, added mutexes to accept & I/O, added
"remote" commands to set ACL, start mirror etc.
2012-05-29 00:59:12 +01:00
Matthew Bloch
c54d4a68ba Added another write/read test, fixed bugs in splice() usage and IPv6
socket handling.
2012-05-27 14:40:16 +01:00
Matthew Bloch
5a5041a751 First few external tests with test/unit, some minor tidying of internal data
structures.
2012-05-24 01:39:35 +01:00
mbloch
d5d6e0f55d Pulled some duplicated code out of control.c into
read_lines_until_blankline.
2012-05-23 14:03:30 +01:00
Matthew Bloch
9c26f7f36f Split control-socket functions into separate file. 2012-05-23 00:42:14 +01:00
Matthew Bloch
811e4ab2cd Fixed mirroring to work (error reporting suspect though). 2012-05-22 00:22:06 +01:00
Matthew Bloch
7eaf5c3fd3 Initial, untested mirror implementation and resolved some type confusion
around struct ip_and_mask pointers (no idea how it worked before).  Added a
header for readwrite.h used in mirror implementation.
2012-05-21 04:03:17 +01:00
Matthew Bloch
cd6e878673 More valgrind-found bugs, extracted open_and_mmap from main code. 2012-05-21 04:00:45 +01:00
Matthew Bloch
43239feb38 Fixed some uninitialised variables courtesy of valgrind. 2012-05-21 03:59:43 +01:00
Matthew Bloch
f7ce2c0ea5 Mostly finished bitset tests, fixed test build to include utilities, remove
efence as valgrind far preferable.
2012-05-21 03:17:32 +01:00
Matthew Bloch
c94b6f365c Tweaks to bitset.h, established a C test framework. 2012-05-20 14:38:46 +01:00
Matthew Bloch
8a38cf48eb Fixed segfaulting access control, allowed change to acl via control socket. 2012-05-19 12:48:03 +01:00
Matthew Bloch
580b821f61 Added dummy control socket answering / changed serve_accept_loop to use
select() to avoid a separate listening thread.
2012-05-18 23:39:16 +01:00
mbloch
b533e4e31c Added control socket, doesn't do anything yet. 2012-05-18 18:44:34 +01:00
Matthew Bloch
f5d8e740f8 Added .hgignore file 2012-05-18 13:25:54 +01:00
Matthew Bloch
ca53d6f270 Stopped NBD writes from committing all-zero blocks to disc (tentative, needs
further testing).
2012-05-18 13:24:35 +01:00
Matthew Bloch
0432fef8f5 Split code out into separate compilation units (first pass, anyway). 2012-05-17 20:14:22 +01:00
Matthew Bloch
aec90e5244 Non-functioning commit, half-way through adding sparse bitmap feature. 2012-05-17 11:54:25 +01:00
Matthew Bloch
f688d416a5 Added write mode. 2012-05-16 11:58:41 +01:00
Matthew Bloch
b1aa942b3d Added working read via splice syscall. 2012-05-16 03:20:09 +01:00
mbloch
c796a526d0 Added Rakefile 2012-05-16 01:27:14 +01:00
mbloch
c6099f78ea Silly bug fixes, added ACL support, added parser for read/write requests. 2012-05-15 18:40:58 +01:00
Matthew Bloch
94c2d44d7d Some debugging, got it to serve. 2012-05-15 03:16:19 +01:00
120 changed files with 19094 additions and 423 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
**/*.o
**/*~
flexnbd
build/
pkg/
**/*.orig
**/.*.swp
cscope.out
valgrind.out

27
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,27 @@
stages:
- package
- publish
package:jessie: &package
stage: package
image: $CI_REGISTRY/docker-images/layers:$DISTRO-deb
variables:
DISTRO: jessie
script:
- package
artifacts:
paths:
- pkg/
package:stretch:
<<: *package
variables:
DISTRO: stretch
publish:
stage: publish
tags:
- shell
script:
- publish

24
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,24 @@
# Contribution guide
The code is formatted using the K&R style of "indent".
```
indent -kr <files go here>
```
The C unit tests have also been indented in the same way, but manually adjsted
such that the functions follow the normal libcheck layout.
```c
START_TEST( ... ) {
}
END TEST
```
Indent tends to mangle the `END_TEST` macro, so that will need adjusting if
`indent` is run over the test files again.

115
Makefile Normal file
View File

@@ -0,0 +1,115 @@
#!/usr/bin/make -f
VPATH=src:tests/unit
DESTDIR?=/
PREFIX?=/usr/local/bin
INSTALLDIR=$(DESTDIR)/$(PREFIX)
ifdef DEBUG
CFLAGS_EXTRA=-g -DDEBUG
LDFLAGS_EXTRA=-g
else
CFLAGS_EXTRA=-O2
endif
CFLAGS_EXTRA += -fPIC --std=gnu99
LDFLAGS_EXTRA += -Wl,--relax,--gc-sections -L$(LIB) -Wl,-rpath-link,$(LIB)
# The -Wunreachable-code warning is only implemented in clang, but it
# doesn't break anything for gcc to see it.
WARNINGS=-Wall \
-Wextra \
-Werror-implicit-function-declaration \
-Wstrict-prototypes \
-Wno-missing-field-initializers \
-Wunreachable-code
CCFLAGS=-D_GNU_SOURCE=1 $(WARNINGS) $(CFLAGS_EXTRA) $(CFLAGS)
LLDFLAGS=-lm -lrt -lev $(LDFLAGS_EXTRA) $(LDFLAGS)
CC?=gcc
LIBS=-lpthread
INC=-I/usr/include/libev -Isrc/common -Isrc/server -Isrc/proxy
COMPILE=$(CC) -MMD $(INC) -c $(CCFLAGS)
LINK=$(CC) $(LLDFLAGS) -Isrc $(LIBS)
LIB=build/
COMMON_SRC := $(wildcard src/common/*.c)
SERVER_SRC := $(wildcard src/server/*.c)
PROXY_SRC := $(wildcard src/proxy/*.c)
COMMON_OBJ := $(COMMON_SRC:src/%.c=build/%.o)
SERVER_OBJ := $(SERVER_SRC:src/%.c=build/%.o)
PROXY_OBJ := $(PROXY_SRC:src/%.c=build/%.o)
SRCS := $(COMMON_SRC) $(SERVER_SRC) $(PROXY_SRC)
OBJS := $(COMMON_OBJ) $(SERVER_OBJ) $(PROXY_OBJ)
all: build doc
build: server proxy
build/%.o: %.c
mkdir -p $(dir $@)
$(COMPILE) $< -o $@
objs: $(OBJS)
build/flexnbd: $(COMMON_OBJ) $(SERVER_OBJ) build/main.o
$(LINK) $^ -o $@
build/flexnbd-proxy: $(COMMON_OBJ) $(PROXY_OBJ) build/proxy-main.o
$(LINK) $^ -o $@
server: build/flexnbd
proxy: build/flexnbd-proxy
CHECK_SRC := $(wildcard tests/unit/*.c)
CHECK_OBJ := $(CHECK_SRC:tests/unit/%.c=build/%.o)
# Why can't we reuse the build/%.o rule above? Not sure.
CHECK_BINS := $(CHECK_SRC:tests/unit/%.c=build/%)
build/check_%: build/check_%.o
$(LINK) $^ -o $@ $(COMMON_OBJ) $(SERVER_OBJ) -lcheck -lsubunit
check_objs: $(CHECK_OBJ)
check_bins: $(CHECK_BINS)
check: $(OBJS) $(CHECK_BINS)
r=true ; for bin in $(CHECK_BINS); do $$bin || r=false; done ; $$r
acceptance: build
cd tests/acceptance && RUBYOPT='-I.' ruby nbd_scenarios -v
test: check acceptance
build/flexnbd.1: README.txt
txt2man -t flexnbd -s 1 $< > $@
build/flexnbd-proxy.1: README.proxy.txt
txt2man -t flexnbd-proxy -s 1 $< > $@
# If we don't pipe to file, gzip clobbers the original, causing make
# to rebuild each time
%.1.gz: %.1
gzip -c -f $< > $@
doc: build/flexnbd.1.gz build/flexnbd-proxy.1.gz
install:
mkdir -p $(INSTALLDIR)
cp build/flexnbd build/flexnbd-proxy $(INSTALLDIR)
clean:
rm -rf build/*
.PHONY: clean objs check_objs all server proxy check_bins check doc build test acceptance
# Include extra dependencies at the end, NOT before 'all'
-include $(wildcard build/*.d)

206
README.proxy.txt Normal file
View File

@@ -0,0 +1,206 @@
NAME
flexnbd-proxy - A simple NBD proxy
SYNOPSIS
flexnbd-proxy --addr ADDR [--port PORT] --conn-addr ADDR
--conn-port PORT [--bind ADDR] [--cache[=CACHE_BYTES]]
[--help] [--verbose] [--quiet]
DESCRIPTION
flexnbd-proxy is a simple NBD proxy server that implements resilient
connection logic for the client. It connects to an upstream NBD server
and allows a single client to connect to it. All server properties are
proxied to the client, and the client connection is kept alive across
reconnections to the upstream server. If the upstream goes away while
an NBD request is in-flight then the proxy (silently, from the point
of view of the client) reconnects and retransmits the request, before
returning the response to the client.
USAGE
Proxy requests from an NBD client to an NBD server, resiliently. Only one
client can be connected at a time, and ACLs cannot be applied to the client, as they
can be to clients connecting directly to a flexnbd in serve mode.
On starting up, the proxy will attempt to connect to the server specified by
--conn-addr and --conn-port (from the address specified by --bind, if given). If
it fails, then the process will die with an error exit status.
Assuming a successful connection to the `upstream` server is made, the proxy
will then start listening on the address specified by --addr and --port, waiting
for `downstream` to connect to it (this will be your NBD client). The client
will be given the same hello message as the proxy was given by the server.
When connected, any request the client makes will be read by the proxy and sent
to the server. If the server goes away for any reason, the proxy will remember
the request and regularly (~ every 5 seconds) try to reconnect to the server.
Upon reconnection, the request is sent and a reply is waited for. When a reply
is received, it is sent back to the client.
When the client disconnects, cleanly or otherwise, the proxy goes back to
waiting for a new client to connect. The connection to the server is maintained
at that point, in case it is needed again.
Only one request may be in-flight at a time under the current architecture; that
doesn't seem to slow things down much relative to alternative options, but may
be changed in the future if it becomes an issue.
OPTIONS
--addr, -l ADDR
The address to listen on. If this begins with a '/', it is assumed to be
a UNIX domain socket to create. Otherwise, it should be an IPv4 or IPv6
address.
--port, -p PORT
The port to listen on, if --addr is not a UNIX socket.
--conn-addr, -C ADDR
The address of the NBD server to connect to. Required.
--conn-port, -P PORT
The port of the NBD server to connect to. Required.
--cache, -c=CACHE_BYTES
If given, the size in bytes of read cache to use. CACHE_BYTES
defaults to 4096.
--help, -h
Show command or global help.
--verbose, -v
Output all available log information to STDERR.
--quiet, -q
Output as little log information as possible to STDERR.
LOGGING
Log output is sent to STDERR. If --quiet is set, no output will be
seen unless the program termintes abnormally. If neither --quiet nor
--verbose are set, no output will be seen unless something goes wrong
with a specific request. If --verbose is given, every available log
message will be seen (which, for a debug build, is many). It is not an
error to set both --verbose and --quiet. The last one wins.
The log line format is:
<TIMESTAMP>:<LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
<TIMESTAMP>
Time the log entry was made. This is expressed in terms of monotonic ms
<LEVEL>
This will be one of 'D', 'I', 'W', 'E', 'F' in increasing order of
severity. If flexnbd is started with the --quiet flag, only 'F' will
be seen. If it is started with the --verbose flag, any from 'I'
upwards will be seen. Only if you have a debug build and start it
with --verbose will you see 'D' entries.
<PID>
This is the process ID.
<THREAD>
flexnbd-proxy is currently single-threaded, so this should be the
same for all lines. That may not be the case in the future.
<SOURCEFILE:SOURCELINE>
Identifies where in the source code this log line can be found.
<MSG>
A short message describing what's happening, how it's being done, or
if you're very lucky why it's going on.
EXAMPLES
The main point of the proxy mode is to allow clients that would otherwise break
when the NBD server goes away (during a migration, for instance) to see a
persistent TCP connection throughout the process, instead of needing its own
reconnection logic.
For maximum reliability, the proxy process would be run on the same machine as
the actual NBD client; an example might look like:
nbd-server-1$ flexnbd serve -l 10.0.0.1 -p 4777 myfile [...]
nbd-client-1$ flexnbd-proxy -l 127.0.0.1 -p 4777 -C 10.0.0.1 -P 4777
nbd-client-1$ nbd-client -c 127.0.0.1 4777 /dev/nbd0
nbd-server-2$ flexnbd listen -l 10.0.0.2 -p 4777 -f myfile [...]
nbd-server-1$ flexnbd mirror --addr 10.0.0.2 -p 4777 [...]
Upon completing the migration, the mirroring and listening flexnbd servers will
both exit. With the proxy mediating requests, this does not break the TCP
connection that nbd-client is holding open. If no requests are in-flight, it
will not notice anything at all; if requests are in-flight, then the reply may
take longer than usual to be returned.
When flexnbd is restarted in serve mode on the second server:
nbd-server-2$ flexnbd serve -l 10.0.0.1 -p 4777 -f myfile [...]
The proxy notices and reconnects, fulfiling any request it has in its buffer.
The data in myfile has been moved between physical servers without the nbd
client process having to be disturbed at all.
READ CACHE
If the --cache option is given at the command line, either without an
argument or with an argument greater than 0, flexnbd-proxy will use a
read-ahead cache. The cache as currently implemented doubles each read
request size, up to a maximum of 2xCACHE_BYTES, and retains the latter
half in a buffer. If the next read request from the client exactly
matches the region held in the buffer, flexnbd-proxy responds from the
cache without making a request to the server.
This pattern is designed to match sequential reads, such as those
performed by a booting virtual machine.
Note: If specifying a cache size, you must use this form:
nbd-client$ flexnbd-proxy --cache=XXXX
That is, the '=' is required. This is a limitation of getopt-long.
If no cache size is given, a size of 4096 bytes is assumed. Caching can
be explicitly disabled by setting a size of 0.
BUGS
Should be reported via GitHub.
* https://github.com/BytemarkHosting/flexnbd-c/issues
Current issues include:
* only old-style NBD negotiation is supported;
* only one request may be in-flight at a time;
* all I/O is blocking, and signals terminate the process immediately;
* UNIX socket support is limited to the listen address;
* FLUSH and TRIM commands, and the FUA flag, are not supported;
* DISCONNECT requests do not get passed through to the NBD server;
* no active timeout-retry of requests - we trust the kernel's idea of
failure.
AUTHOR
Originally written by Alex Young <alex@blackkettle.org>.
Original concept and core code by Matthew Bloch <matthew@bytemark.co.uk>.
Proxy mode written by Nick Thomas <me@ur.gs>.
The full commit history is available on GitHub.
SEE ALSO
flexnbd(1), nbd-client(8), xnbd-server(8), xnbd-client(8)
COPYRIGHT
Copyright (c) 2012-2016 Bytemark Hosting Ltd. Free use of this
software is granted under the terms of the GNU General Public License
version 3 or later.

403
README.txt Normal file
View File

@@ -0,0 +1,403 @@
NAME
flexnbd - A fast NBD server
SYNOPSIS
flexnbd MODE [ ARGS ]
flexnbd serve --addr ADDR --port PORT --file FILE [--sock SOCK]
[--default-deny] [--killswitch] [global_option]* [acl_entry]*
flexnbd listen --addr ADDR --port PORT --file FILE [--sock SOCK]
[--default-deny] [global_option]* [acl_entry]*
flexnbd mirror --addr ADDR --port PORT --sock SOCK [--unlink]
[--bind BIND_ADDR] [global_option]*
flexnbd acl --sock SOCK [acl_entry]+ [global_option]*
flexnbd break --sock SOCK [global_option]*
flexnbd status --sock SOCK [global_option]*
flexnbd read --addr ADDR --port PORT --from OFFSET --size SIZE
[--bind BIND_ADDR] [global_option]*
flexnbd write --addr ADDR --port PORT --from OFFSET --size SIZE
[--bind BIND_ADDR] [global_option]*
flexnbd help [mode] [global_option]*
DESCRIPTION
Flexnbd is a fast NBD server which supports live migration. Live
migration is performed by writing the data to a new server. A failed
migration will be invisible to any connected clients.
Flexnbd tries quite hard to preserve sparsity of files it is serving,
even across migrations.
SERVE MODE
Serve a file.
$ flexnbd serve --addr <ADDR> --port <PORT> --file <FILE>
[--sock <SOCK>] [--default-deny] [-k] [global_option]*
[acl_entry]*
If any ACL entries are given (which should be IP
addresses), only those clients listed will be permitted to connect.
flexnbd will continue to serve until a SIGINT, SIGQUIT, or a successful
migration.
OPTIONS
--addr, -l ADDR
The address to listen on. Required.
--port, -p PORT
The port to listen on. Required.
--file, -f FILE
The file to serve. Must already exist. Required.
--sock, -s SOCK
Path to a control socket to open. You will need this if you want to
migrate, get the current status, or manipulate the access control
list.
--default-deny, -d
How to interpret an empty ACL. If --default-deny is given, an
empty ACL will let no clients connect. If it is not given, an
empty ACL will let any client connect.
--killswitch, -k
If set, we implement a 2-minute timeout on NBD requests and
responses. If a request takes longer than that to complete,
the client is disconnected. This is useful to keep broken
clients from breaking migrations, among other things.
LISTEN MODE
Listen for an inbound migration, and quit with a status of 0 on
completion.
$ flexnbd listen --addr ADDR --port PORT --file FILE
[--sock SOCK] [--default-deny] [global_option]*
[acl_entry]*
flexnbd will wait for a successful migration, and then quit. The file
to write the inbound migration data to must already exist before you
run 'flexnbd listen'.
Only one sender may connect to send data, and if the sender
disconnects part-way through the migration, the destination will
expect it to reconnect and retry the whole migration. It isn't safe
to assume that a partial migration can be resumed because the
destination has no knowledge of whether a client has made a write to
the source in the interim.
If the migration fails for a reason which the 'flexnbd listen' process
can't fix (say, a failed local write), it will exit with an error
status. In this case, the sender will continually retry the migration
until it succeeds, and you will need to restart the 'flexnbd listen'
process to allow that to happen.
OPTIONS
As for serve.
MIRROR MODE
Start a migration from the server with control socket SOCK to the server
listening at ADDR:PORT.
$ flexnbd mirror --addr ADDR --port PORT --sock SOCK [--unlink]
[--bind BIND_ADDR] [global_option]*
Migration can be a slow process. Rather than block the 'flexnbd mirror'
process until it completes, it will exit with a message of "Migration
started" once it has confirmation that the local server was able to
connect to ADDR:PORT and got an NBD header back. To check on the
progress of a running migration, use 'flexnbd status'.
If the destination unexpectedly disconnects part-way through the
migration, the source will attempt to reconnect and start the migration
again. It is not safe to resume the migration from where it left off
because the source can't see that the backing store behind the
destination is intact, or even on the same machine.
If the --unlink option is given, the local file will be deleted
immediately before the mirror connection is terminated. This allows
an otherwise-ambiguous situation to be resolved: if you don't unlink
the file and the flexnbd process at either end is terminated, it's not
possible to tell which copy of the data is canonical. Since the
unlink happens as soon as the sender knows that it has transmitted all
the data, there can be no ambiguity.
Note: files smaller than 4096 bytes cannot be mirrored.
OPTIONS
--addr, -l ADDR
The address of the remote server to migrate to. Required.
--port, -p PORT
The port of the remote server to migrate to. Required.
--sock, -s SOCK
The control socket of the local server to migrate from. Required.
--unlink, -u
Unlink the served file from the local filesystem after
successfully mirroring.
--bind, -b BIND_ADDR
The local address to bind to. You may need this if the remote
server is using an access control list.
BREAK MODE
Stop a running migration.
$ flexnbd break --sock SOCK [global_option]*
OPTIONS
--sock, -s SOCK
The control socket of the local server whose migration to stop.
Required.
ACL MODE
Set the access control list of the server with the control socket SOCK
to the given access control list entries.
$ flexnbd acl --sock SOCK [acl_entry]+ [global_option]*
ACL entries are given as IP addresses.
OPTIONS
--sock, -s SOCK
The control socket of the server whose ACL to replace. Required
STATUS MODE
Get the current status of the server with control socket SOCK.
$ flexnbd status --sock SOCK [global_option]*
The status will be printed to STDOUT. It is a space-separated list of
key=value pairs. The space character will never appear in a key or
value. Currently reported values are:
pid
The process id of the server listening on SOCK.
is_mirroring
'true' if this server is sending migration data, 'false' otherwise.
has_control
'false' if this server was started in 'listen' mode. 'true' otherwise.
OPTIONS
--sock, -s SOCK
The control socket of the server of interest. Required.
READ MODE
Connect to the server at ADDR:PORT, and read SIZE bytes starting at
OFFSET in a single NBD query.
$ flexnbd read --addr ADDR --port PORT --from OFFSET --size SIZE
[--bind BIND_ADDR] [global_option]*
The returned data will be echoed to STDOUT. In case of a remote ACL,
set the local source address to BIND_ADDR.
OPTIONS
--addr, -l ADDR
The address of the remote server. Required.
--port, -p PORT
The port of the remote server. Required.
--from, -F OFFSET
The byte offset to start reading from. Required. Maximum 2^62.
--size, -S SIZE
The number of bytes to read. Required. Maximum 2^30.
--bind, -b BIND_ADDR
The local address to bind to. You may need this if the remote
server is using an access control list.
WRITE MODE
Connect to the server at ADDR:PORT, and write SIZE bytes from STDIN
starting at OFFSET in a single NBD query.
$ cat ... | flexnbd write --addr ADDR --port PORT --from OFFSET
--size SIZE [--bind BIND_ADDR] [global_option]*
In case of a remote ACL, set the local source address to BIND_ADDR.
OPTIONS
--addr, -l ADDR
The address of the remote server. Required.
--port, -p PORT
The port of the remote server. Required.
--from, -F OFFSET
The byte offset to start writing from. Required. Maximum 2^62.
--size, -S SIZE
The number of bytes to write. Required. Maximum 2^30.
--bind, -b BIND_ADDR
The local address to bind to. You may need this if the remote
server is using an access control list.
HELP MODE
$ flexnbd help [mode] [global_option]*
Without mode, show the list of available modes. With mode, show help for that mode.
GLOBAL OPTIONS
--help, -h Show mode or global help.
--verbose, -v Output all available log information to STDERR.
--quiet, -q Output as little log information as possible to STDERR.
LOGGING
Log output is sent to STDERR. If --quiet is set, no output will be
seen unless the program termintes abnormally. If neither --quiet nor
--verbose are set, no output will be seen unless something goes wrong
with a specific request. If --verbose is given, every available log
message will be seen (which, for a debug build, is many). It is not an
error to set both --verbose and --quiet. The last one wins.
The log line format is:
<TIMESTAMP>:<LEVEL>:<PID> <THREAD> <SOURCEFILE:SOURCELINE>: <MSG>
<TIMESTAMP>
Time the log entry was made. This is expressed in terms of monotonic
ms.
<LEVEL>
This will be one of 'D', 'I', 'W', 'E', 'F' in increasing order of
severity. If flexnbd is started with the --quiet flag, only 'F'
will be seen. If it is started with the --verbose flag, any from 'I'
upwards will be seen. Only if you have a debug build and start it
with --verbose will you see 'D' entries.
<PID>
This is the process ID.
<THREAD>
There are several pthreads per flexnbd process: a main thread, a
serve thread, a thread per client, and possibly a pair of mirror
threads and a control thread. This field identifies which thread was
responsible for the log line.
<SOURCEFILE:SOURCELINE>
Identifies where in the source code this log line can be found.
<MSG>
A short message describing what's happening, how it's being done, or
if you're very lucky why it's going on.
EXAMPLES
SERVING A FILE
The simplest case is serving a file on the default nbd port:
$ cp /etc/passwd /tmp
$ flexnbd serve --file /tmp/passwd --addr 0.0.0.0 --port 4777 &
$ flexnbd read --addr 127.0.0.1 --port 4777 --from 0 --size 7
root:x:
$
READING SERVER STATUS
In order to read a server's status, we need it to open a control socket.
$ flexnbd serve --file /tmp/passwd --addr 0.0.0.0 --port 4777 \
--sock /tmp/flexnbd.sock
$ flexnbd status --sock /tmp/flexnbd.sock
pid=9635 is_mirroring=false has_control=true
$
Note that the status output is newline-terminated.
MIGRATING
To migrate, we need to provide a destination file of the right size.
$ dd if=/dev/urandom of=/tmp/data bs=1024 count=1K
$ truncate -s 1M /tmp/data.copy
$ flexnbd serve --file /tmp/data --addr 0.0.0.0 --port 4778 \
--sock /tmp/flex-source.sock &
$ flexnbd listen --file /tmp/data.copy --addr 0.0.0.0 --port 4779 \
--sock /tmp/flex-dest.sock &
$
Now we check the status of each server, to check that they are both in
the right state:
$ flexnbd status --sock /tmp/flex-source.sock
pid=9648 is_mirroring=false has_control=true
$ flexnbd status --sock /tmp/flex-dest.sock
pid=9651 is_mirroring=false has_control=false
$
With this knowledge in hand, we can start the migration:
$ flexnbd mirror --addr 127.0.0.1 --port 4779 \
--sock /tmp/flex-source.sock
Migration started
[1] + 9648 done flexnbd serve --addr 0.0.0.0 --port 4778
[2] + 9651 done flexnbd listen --addr 0.0.0.0 --port 4779
$
Note that because the file is so small in this case, we see the source
server quit soon after we start the migration, and the destination
exited at roughly the same time.
BUGS
Should be reported on GitHub at
* https://github.com/BytemarkHosting/flexnbd-c/issues
AUTHOR
Originally written by Alex Young <alex@blackkettle.org>.
Original concept and core code by Matthew Bloch <matthew@bytemark.co.uk>.
Proxy mode written by Nick Thomas <me@ur.gs>.
The full commit history is available on GitHub.
SEE ALSO
flexnbd-proxy(1), nbd-client(8), xnbd-server(8), xnbd-client(8)
COPYRIGHT
Copyright (c) 2012-2016 Bytemark Hosting Ltd. Free use of this
software is granted under the terms of the GNU General Public License
version 3 or later.

2844
debian/changelog vendored Normal file

File diff suppressed because it is too large Load Diff

1
debian/compat vendored Normal file
View File

@@ -0,0 +1 @@
7

25
debian/control vendored Normal file
View File

@@ -0,0 +1,25 @@
Source: flexnbd
Section: web
Priority: extra
Maintainer: Patrick J Cherry <patrick@bytemark.co.uk>
Build-Depends: debhelper (>= 7.0.50), ruby, gcc, libev-dev, txt2man, check, net-tools, libsubunit-dev, ruby-test-unit
Standards-Version: 3.8.1
Homepage: https://github.com/BytemarkHosting/flexnbd-c
Package: flexnbd
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, libev4 | libev3
Description: FlexNBD server
An NBD server offering push-mirroring and intelligent sparse file handling
Package: flexnbd-dbg
Architecture: any
Section: debug
Priority: extra
Depends:
flexnbd (= ${binary:Version}),
${misc:Depends}
Description: debugging symbols for flexnbd
An NBD server offering push-mirroring and intelligent sparse file handling
.
This package contains the debugging symbols for flexnbd.

53
debian/copyright vendored Normal file
View File

@@ -0,0 +1,53 @@
This work was packaged for Debian by:
Alex Young <alex@bytemark.co.uk> on Wed, 30 May 2012 16:46:58 +0100
It was downloaded from:
<url://example.com>
Upstream Author(s):
<put author's name and email here>
<likewise for another author>
Copyright:
<Copyright (C) YYYY Firstname Lastname>
<likewise for another author>
License:
### SELECT: ###
This package is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
### OR ###
This package is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
##########
This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
On Debian systems, the complete text of the GNU General
Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".
The Debian packaging is:
Copyright (C) 2012 Alex Young <alex@bytemark.co.uk>
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
# Please also look if there are files or directories which have a
# different copyright/license attached and list them here.

3
debian/flexnbd.install vendored Normal file
View File

@@ -0,0 +1,3 @@
build/flexnbd usr/bin
build/flexnbd-proxy usr/bin

2
debian/flexnbd.manpages vendored Normal file
View File

@@ -0,0 +1,2 @@
build/flexnbd.1.gz
build/flexnbd-proxy.1.gz

19
debian/rules vendored Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
%:
dh $@
override_dh_strip:
dh_strip --dbg-package=flexnbd-dbg
#
# TODO: The ruby test suites don't work during buiding in a chroot, so leave
# them out for now.
#
#override_dh_auto_test:
# rake test:run

1
debian/source/format vendored Normal file
View File

@@ -0,0 +1 @@
3.0 (native)

423
flexnbd.c
View File

@@ -1,423 +0,0 @@
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <malloc.h>
#include <errno.h>
#include <endian.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
/* http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-09/2332.html */
#define INIT_PASSWD "NBDMAGIC"
#define INIT_MAGIC 0x0000420281861253
#define REQUEST_MAGIC 0x25609513
#define REPLY_MAGIC 0x67446698
#define REQUEST_READ 0
#define REQUEST_WRITE 1
#define REQUEST_DISCONNECT 2
#include <linux/types.h>
struct nbd_init {
char passwd[8];
__be64 magic;
__be64 size;
char reserved[128];
};
struct nbd_request {
__be32 magic;
__be32 type; /* == READ || == WRITE */
char handle[8];
__be64 from;
__be32 len;
} __attribute__((packed));
struct nbd_reply {
__be32 magic;
__be32 error; /* 0 = ok, else error */
char handle[8]; /* handle you got from request */
};
void syntax()
{
fprintf(stderr,
"Syntax: flexnbd serve <IP address> <port> <file> [ip addresses ...]\n"
" flexnbd read <IP address> <port> <offset> <length> > data\n"
" flexnbd write <IP address> <port> <offset> [length] < data\n"
" flexnbd mirror <IP address> <port> <target IP> <target port>\n"
);
exit(1);
}
static pthread_t server_thread_id;
void error(int consult_errno, int close_socket, const char* format, ...)
{
va_list argptr;
fprintf(stderr, "*** ");
va_start(argptr, format);
vfprintf(stderr, format, argptr);
va_end(argptr);
if (consult_errno) {
fprintf(stderr, " (errno=%d, %s)", errno, strerror(errno));
}
if (close_socket)
close(close_socket);
fprintf(stderr, "\n");
if (pthread_equal(pthread_self(), server_thread_id))
pthread_exit((void*) 1);
else
exit(1);
}
#define CLIENT_ERROR(msg, ...) \
error(0, client->socket, msg, ##__VA_ARGS__)
#define CLIENT_ERROR_ON_FAILURE(test, msg, ...) \
if (test < 0) { error(1, client->socket, msg, ##__VA_ARGS__); }
#define SERVER_ERROR(msg, ...) \
error(0, 0, msg, ##__VA_ARGS__)
#define SERVER_ERROR_ON_FAILURE(test, msg, ...) \
if (test < 0) { error(1, 0, msg, ##__VA_ARGS__); }
void* xmalloc(size_t size)
{
void* p = malloc(size);
if (p == NULL)
SERVER_ERROR("couldn't malloc %d bytes", size);
return p;
}
struct ip_and_mask {
/* FIXME */
};
struct mode_serve_params {
union { struct sockaddr generic;
struct sockaddr_in v4;
struct sockaddr_in6 v6; } bind_to;
struct ip_and_mask** acl;
char* filename;
int tcp_backlog;
int server;
int threads;
};
struct client_params {
int socket;
char* filename;
int fileno;
off64_t size;
char* mapped;
};
union mode_params {
struct mode_serve_params serve;
};
int writeloop(int filedes, const void *buffer, size_t size)
{
size_t written=0;
while (written < size) {
size_t result = write(filedes, buffer+written, size-written);
if (result == -1)
return -1;
written += result;
}
return 0;
}
int readloop(int filedes, void *buffer, size_t size)
{
size_t readden=0;
while (readden < size) {
size_t result = read(filedes, buffer+readden, size-readden);
printf("read size=%d readden=%d result=%d\n", size, readden, result);
if (result == -1)
return -1;
readden += result;
}
return 0;
}
int sendfileloop(int out_fd, int in_fd, off64_t *offset, size_t count)
{
size_t sent=0;
while (sent < count) {
size_t result = sendfile64(out_fd, in_fd, offset+sent, count-sent);
if (result == -1)
return -1;
sent += result;
}
return 0;
}
int client_serve_request(struct client_params* client)
{
off64_t offset;
struct nbd_request request;
struct nbd_reply reply;
CLIENT_ERROR_ON_FAILURE(
readloop(client->socket, &request, sizeof(request)),
"Failed to read request"
);
reply.magic = htobe32(REPLY_MAGIC);
reply.error = htobe32(0);
memcpy(reply.handle, request.handle, 8);
if (be32toh(request.magic) != REQUEST_MAGIC)
CLIENT_ERROR("Bad magic %08x", be32toh(request.magic));
switch (be32toh(request.type))
{
case REQUEST_READ:
case REQUEST_WRITE:
/* check it's not out of range */
if (be64toh(request.from) < 0 ||
be64toh(request.from)+be64toh(request.len) > client->size) {
reply.error = htobe32(1);
write(client->socket, &reply, sizeof(reply));
return 0;
}
case REQUEST_DISCONNECT:
return 1;
default:
CLIENT_ERROR("Unknown request %08x", be32toh(request.type));
}
switch (be32toh(request.type))
{
case REQUEST_READ:
write(client->socket, &reply, sizeof(reply));
offset = be64toh(request.from);
CLIENT_ERROR_ON_FAILURE(
sendfileloop(
client->socket,
client->fileno,
&offset,
be64toh(request.len)
),
"sendfile failed from=%ld, len=%ld",
offset,
be64toh(request.len)
);
break;
case REQUEST_WRITE:
CLIENT_ERROR_ON_FAILURE(
readloop(
client->socket,
client->mapped + be64toh(request.from),
be64toh(request.len)
),
"read failed from=%ld, len=%d",
be64toh(request.from),
be64toh(request.len)
);
write(client->socket, &reply, sizeof(reply));
break;
}
return 0;
}
void client_open_file(struct client_params* client)
{
client->fileno = open(client->filename, O_RDWR|O_DIRECT|O_SYNC);
CLIENT_ERROR_ON_FAILURE(client->fileno, "Couldn't open %s",
client->filename);
client->size = lseek64(client->fileno, 0, SEEK_END);
CLIENT_ERROR_ON_FAILURE(client->fileno, "Couldn't seek to end of %s",
client->filename);
client->mapped = mmap64(NULL, client->size, PROT_READ|PROT_WRITE,
MAP_SHARED, client->fileno, 0);
CLIENT_ERROR_ON_FAILURE((long) client->mapped, "Couldn't map file %s",
client->filename);
}
void client_send_hello(struct client_params* client)
{
struct nbd_init init;
memcpy(init.passwd, INIT_PASSWD, sizeof(INIT_PASSWD));
init.magic = htobe64(INIT_MAGIC);
init.size = htobe64(client->size);
memset(init.reserved, 0, 128);
CLIENT_ERROR_ON_FAILURE(
writeloop(client->socket, &init, sizeof(init)),
"Couldn't send hello"
);
}
void* client_serve(void* client_uncast)
{
struct client_params* client = (struct client_params*) client_uncast;
client_open_file(client);
client_send_hello(client);
while (client_serve_request(client) == 0)
CLIENT_ERROR_ON_FAILURE(
close(client->socket),
"Couldn't close socket %d",
client->socket
);
free(client);
return NULL;
}
/* FIXME */
int is_included_in_acl(struct ip_and_mask** list, struct sockaddr* test)
{
return 1;
}
void serve_open_socket(struct mode_serve_params* params)
{
params->server = socket(PF_INET, SOCK_STREAM, 0);
SERVER_ERROR_ON_FAILURE(params->server,
"Couldn't create server socket");
SERVER_ERROR_ON_FAILURE(
bind(params->server, &params->bind_to.generic,
sizeof(params->bind_to.generic)),
"Couldn't bind server to IP address"
);
SERVER_ERROR_ON_FAILURE(
listen(params->server, params->tcp_backlog),
"Couldn't listen on server socket"
);
}
void serve_accept_loop(struct mode_serve_params* params)
{
while (1) {
pthread_t client_thread;
struct sockaddr client_address;
struct client_params* client_params;
socklen_t socket_length;
int client_socket = accept(params->server, &client_address,
&socket_length);
SERVER_ERROR_ON_FAILURE(client_socket, "accept() failed");
if (params->acl &&
!is_included_in_acl(params->acl, &client_address)) {
write(client_socket, "Access control error", 20);
close(client_socket);
continue;
}
client_params = xmalloc(sizeof(struct client_params));
client_params->socket = client_socket;
client_params->filename = params->filename;
client_thread = pthread_create(&client_thread, NULL,
client_serve, client_params);
SERVER_ERROR_ON_FAILURE(client_thread,
"Failed to create client thread");
/* FIXME: keep track of them? */
/* FIXME: maybe shouldn't be fatal? */
}
}
void serve(struct mode_serve_params* params)
{
serve_open_socket(params);
serve_accept_loop(params);
}
void params_serve(
struct mode_serve_params* out,
char* s_ip_address,
char* s_port,
char* s_file
)
{
out->tcp_backlog = 10; /* does this need to be settable? */
out->acl = NULL; /* ignore for now */
if (s_ip_address == NULL)
SERVER_ERROR("No IP address supplied");
if (s_port == NULL)
SERVER_ERROR("No port number supplied");
if (s_file == NULL)
SERVER_ERROR("No filename supplied");
if (s_ip_address[0] == '0' && s_ip_address[1] == '\0') {
out->bind_to.v4.sin_family = AF_INET;
out->bind_to.v4.sin_addr.s_addr = INADDR_ANY;
}
else if (inet_pton(AF_INET, s_ip_address, &out->bind_to.v4) == 0) {
}
else if (inet_pton(AF_INET6, s_ip_address, &out->bind_to.v6) == 0) {
}
else {
SERVER_ERROR("Couldn't understand address '%%' "
"(use 0 if you don't care)", s_ip_address);
}
out->bind_to.v4.sin_port = atoi(s_port);
if (out->bind_to.v4.sin_port < 0 || out->bind_to.v4.sin_port > 65535)
SERVER_ERROR("Port number must be >= 0 and <= 65535");
out->bind_to.v4.sin_port = htobe16(out->bind_to.v4.sin_port);
out->filename = s_file;
}
void mode(char* mode, int argc, char **argv)
{
union mode_params params;
if (strcmp(mode, "serve") == 0) {
if (argc >= 3) {
params_serve(&params.serve, argv[0], argv[1], argv[2]);
serve(&params.serve);
}
else {
syntax();
}
}
else {
syntax();
}
exit(0);
}
int main(int argc, char** argv)
{
server_thread_id = pthread_self();
if (argc < 2)
syntax();
mode(argv[1], argc-2, argv+2);
return 0;
}

371
src/common/ioutil.c Normal file
View File

@@ -0,0 +1,371 @@
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/fs.h>
#include <linux/fiemap.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "util.h"
#include "bitset.h"
#include "ioutil.h"
int build_allocation_map(struct bitset *allocation_map, int fd)
{
/* break blocking ioctls down */
const unsigned long max_length = 100 * 1024 * 1024;
const unsigned int max_extents = 1000;
unsigned long offset = 0;
struct {
struct fiemap fiemap;
struct fiemap_extent extents[max_extents];
} fiemap_static;
struct fiemap *fiemap = (struct fiemap *) &fiemap_static;
memset(&fiemap_static, 0, sizeof(fiemap_static));
for (offset = 0; offset < allocation_map->size;) {
fiemap->fm_start = offset;
fiemap->fm_length = max_length;
if (offset + max_length > allocation_map->size) {
fiemap->fm_length = allocation_map->size - offset;
}
fiemap->fm_flags = FIEMAP_FLAG_SYNC;
fiemap->fm_extent_count = max_extents;
fiemap->fm_mapped_extents = 0;
if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
debug("Couldn't get fiemap, returning no allocation_map");
return 0; /* it's up to the caller to free the map */
} else {
for (unsigned int i = 0; i < fiemap->fm_mapped_extents; i++) {
bitset_set_range(allocation_map,
fiemap->fm_extents[i].fe_logical,
fiemap->fm_extents[i].fe_length);
}
/* must move the offset on, but careful not to jump max_length
* if we've actually hit max_offsets.
*/
if (fiemap->fm_mapped_extents > 0) {
struct fiemap_extent *last =
&fiemap->fm_extents[fiemap->fm_mapped_extents - 1];
offset = last->fe_logical + last->fe_length;
} else {
offset += fiemap->fm_length;
}
}
}
info("Successfully built allocation map");
return 1;
}
int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
void **out_map)
{
/*
* size and out_size are intentionally of different types.
* lseek64() uses off64_t to signal errors in the sign bit.
* Since we check for these errors before trying to assign to
* *out_size, we know *out_size can never go negative.
*/
off64_t size;
/* O_DIRECT should not be used with mmap() */
*out_fd = open(filename, O_RDWR | O_NOATIME);
if (*out_fd < 1) {
warn("open(%s) failed: does it exist?", filename);
return *out_fd;
}
size = lseek64(*out_fd, 0, SEEK_END);
if (size < 0) {
warn("lseek64() failed");
return size;
}
/* If discs are not in multiples of 512, then odd things happen,
* resulting in reads/writes past the ends of files.
*/
if (size != (size & (~0x1ff))) {
warn("file does not fit into 512-byte sectors; the end of the file will be ignored.");
size &= ~0x1ff;
}
if (out_size) {
*out_size = size;
}
if (out_map) {
*out_map = mmap64(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
*out_fd, 0);
if (((long) *out_map) == -1) {
warn("mmap64() failed");
return -1;
}
debug("opened %s size %ld on fd %d @ %p", filename, size, *out_fd,
*out_map);
} else {
debug("opened %s size %ld on fd %d", filename, size, *out_fd);
}
return 0;
}
int writeloop(int filedes, const void *buffer, size_t size)
{
size_t written = 0;
while (written < size) {
ssize_t result = write(filedes, buffer + written, size - written);
if (result == -1) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
continue; // busy-wait
}
return -1; // failure
}
written += result;
}
return 0;
}
int readloop(int filedes, void *buffer, size_t size)
{
size_t readden = 0;
while (readden < size) {
ssize_t result = read(filedes, buffer + readden, size - readden);
if (result == 0 /* EOF */ ) {
return -1;
}
if (result == -1) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
continue; // busy-wait
}
return -1; // failure
}
readden += result;
}
return 0;
}
int sendfileloop(int out_fd, int in_fd, off64_t * offset, size_t count)
{
size_t sent = 0;
while (sent < count) {
ssize_t result = sendfile64(out_fd, in_fd, offset, count - sent);
debug
("sendfile64(out_fd=%d, in_fd=%d, offset=%p, count-sent=%ld) = %ld",
out_fd, in_fd, offset, count - sent, result);
if (result == -1) {
debug("%s (%i) calling sendfile64()", strerror(errno), errno);
return -1;
}
sent += result;
debug("sent=%ld, count=%ld", sent, count);
}
debug("exiting sendfileloop");
return 0;
}
#include <errno.h>
ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
loff_t * off_out, size_t len, unsigned int flags2)
{
const unsigned int flags = SPLICE_F_MORE | SPLICE_F_MOVE | flags2;
size_t spliced = 0;
//debug("spliceloop(%d, %ld, %d, %ld, %ld)", fd_in, off_in ? *off_in : 0, fd_out, off_out ? *off_out : 0, len);
while (spliced < len) {
ssize_t result =
splice(fd_in, off_in, fd_out, off_out, len, flags);
if (result < 0) {
//debug("result=%ld (%s), spliced=%ld, len=%ld", result, strerror(errno), spliced, len);
if (errno == EAGAIN && (flags & SPLICE_F_NONBLOCK)) {
return spliced;
} else {
return -1;
}
} else {
spliced += result;
//debug("result=%ld (%s), spliced=%ld, len=%ld", result, strerror(errno), spliced, len);
}
}
return spliced;
}
int splice_via_pipe_loop(int fd_in, int fd_out, size_t len)
{
int pipefd[2]; /* read end, write end */
size_t spliced = 0;
if (pipe(pipefd) == -1) {
return -1;
}
while (spliced < len) {
ssize_t run = len - spliced;
ssize_t s2, s1 = spliceloop(fd_in, NULL, pipefd[1], NULL, run,
SPLICE_F_NONBLOCK);
/*if (run > 65535)
run = 65535; */
if (s1 < 0) {
break;
}
s2 = spliceloop(pipefd[0], NULL, fd_out, NULL, s1, 0);
if (s2 < 0) {
break;
}
spliced += s2;
}
close(pipefd[0]);
close(pipefd[1]);
return spliced < len ? -1 : 0;
}
/* Reads single bytes from fd until either an EOF or a newline appears.
* If an EOF occurs before a newline, returns -1. The line is lost.
* Inserts the read bytes (without the newline) into buf, followed by a
* trailing NULL.
* Returns the number of read bytes: the length of the line without the
* newline, plus the trailing null.
*/
int read_until_newline(int fd, char *buf, int bufsize)
{
int cur;
for (cur = 0; cur < bufsize; cur++) {
int result = read(fd, buf + cur, 1);
if (result <= 0) {
return -1;
}
if (buf[cur] == 10) {
buf[cur] = '\0';
break;
}
}
return cur + 1;
}
int read_lines_until_blankline(int fd, int max_line_length, char ***lines)
{
int lines_count = 0;
char line[max_line_length + 1];
*lines = NULL;
memset(line, 0, max_line_length + 1);
while (1) {
int readden = read_until_newline(fd, line, max_line_length);
/* readden will be:
* 1 for an empty line
* -1 for an eof
* -1 for a read error
*/
if (readden <= 1) {
return lines_count;
}
*lines = xrealloc(*lines, (lines_count + 1) * sizeof(char *));
(*lines)[lines_count] = strdup(line);
if ((*lines)[lines_count][0] == 0) {
return lines_count;
}
lines_count++;
}
}
int fd_is_closed(int fd_in)
{
int errno_old = errno;
int result = fcntl(fd_in, F_GETFL) < 0;
errno = errno_old;
return result;
}
static inline int io_errno_permanent(void)
{
return (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR);
}
/* Returns -1 if the operation failed, or the number of bytes read if all is
* well. Note that 0 bytes may be returned. Unlike read(), this is not an EOF! */
ssize_t iobuf_read(int fd, struct iobuf * iobuf, size_t default_size)
{
size_t left;
ssize_t count;
if (iobuf->needle == 0) {
iobuf->size = default_size;
}
left = iobuf->size - iobuf->needle;
debug("Reading %" PRIu32 " of %" PRIu32 " bytes from fd %i", left,
iobuf->size, fd);
count = read(fd, iobuf->buf + iobuf->needle, left);
if (count > 0) {
iobuf->needle += count;
debug("read() returned %" PRIu32 " bytes", count);
} else if (count == 0) {
warn("read() returned EOF on fd %i", fd);
errno = 0;
return -1;
} else if (count == -1) {
if (io_errno_permanent()) {
warn(SHOW_ERRNO("read() failed on fd %i", fd));
} else {
debug(SHOW_ERRNO("read() returned 0 bytes"));
count = 0;
}
}
return count;
}
ssize_t iobuf_write(int fd, struct iobuf * iobuf)
{
size_t left = iobuf->size - iobuf->needle;
ssize_t count;
debug("Writing %" PRIu32 " of %" PRIu32 " bytes to fd %i", left,
iobuf->size, fd);
count = write(fd, iobuf->buf + iobuf->needle, left);
if (count >= 0) {
iobuf->needle += count;
debug("write() returned %" PRIu32 " bytes", count);
} else {
if (io_errno_permanent()) {
warn(SHOW_ERRNO("write() failed on fd %i", fd));
} else {
debug(SHOW_ERRNO("write() returned 0 bytes"));
count = 0;
}
}
return count;
}

77
src/common/ioutil.h Normal file
View File

@@ -0,0 +1,77 @@
#ifndef __IOUTIL_H
#define __IOUTIL_H
#include <sys/types.h>
struct iobuf {
unsigned char *buf;
size_t size;
size_t needle;
};
ssize_t iobuf_read(int fd, struct iobuf *iobuf, size_t default_size);
ssize_t iobuf_write(int fd, struct iobuf *iobuf);
#include "serve.h"
struct bitset; /* don't need whole of bitset.h here */
/** Scan the file opened in ''fd'', set bits in ''allocation_map'' that
* correspond to which blocks are physically allocated on disc (or part-
* allocated). If the OS represents allocated blocks at a finer resolution
* than you've asked for, any block or part block will count as "allocated"
* with the corresponding bit set. Returns 1 if successful, 0 otherwise.
*/
int build_allocation_map(struct bitset *allocation_map, int fd);
/** Repeat a write() operation that succeeds partially until ''size'' bytes
* are written, or an error is returned, when it returns -1 as usual.
*/
int writeloop(int filedes, const void *buffer, size_t size);
/** Repeat a read() operation that succeeds partially until ''size'' bytes
* are written, or an error is returned, when it returns -1 as usual.
*/
int readloop(int filedes, void *buffer, size_t size);
/** Repeat a sendfile() operation that succeeds partially until ''size'' bytes
* are written, or an error is returned, when it returns -1 as usual.
*/
int sendfileloop(int out_fd, int in_fd, off64_t * offset, size_t count);
/** Repeat a splice() operation until we have 'len' bytes. */
ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
loff_t * off_out, size_t len, unsigned int flags2);
/** Copy ''len'' bytes from ''fd_in'' to ''fd_out'' by creating a temporary
* pipe and using the Linux splice call repeatedly until it has transferred
* all the data. Returns -1 on error.
*/
int splice_via_pipe_loop(int fd_in, int fd_out, size_t len);
/** Fill up to ''bufsize'' characters starting at ''buf'' with data from ''fd''
* until an LF character is received, which is written to the buffer at a zero
* byte. Returns -1 on error, or the number of bytes written to the buffer.
*/
int read_until_newline(int fd, char *buf, int bufsize);
/** Read a number of lines using read_until_newline, until an empty line is
* received (i.e. the sequence LF LF). The data is read from ''fd'' and
* lines must be a maximum of ''max_line_length''. The set of lines is
* returned as an array of zero-terminated strings; you must pass an address
* ''lines'' in which you want the address of this array returned.
*/
int read_lines_until_blankline(int fd, int max_line_length, char ***lines);
/** Open the given ''filename'', determine its size, and mmap it in its
* entirety. The file descriptor is stored in ''out_fd'', the size in
* ''out_size'' and the address of the mmap in ''out_map''. If anything goes
* wrong, returns -1 setting errno, otherwise 0.
*/
int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
void **out_map);
/** Check to see whether the given file descriptor is closed.
*/
int fd_is_closed(int fd_in);
#endif

96
src/common/mode.h Normal file
View File

@@ -0,0 +1,96 @@
#ifndef MODE_H
#define MODE_H
void mode(char *mode, int argc, char **argv);
#include <getopt.h>
#define GETOPT_ARG(x,s) {(x), required_argument, 0, (s)}
#define GETOPT_FLAG(x,v) {(x), no_argument, 0, (v)}
#define GETOPT_OPTARG(x,s) {(x), optional_argument, 0, (s)}
#define OPT_HELP "help"
#define OPT_ADDR "addr"
#define OPT_BIND "bind"
#define OPT_PORT "port"
#define OPT_FILE "file"
#define OPT_SOCK "sock"
#define OPT_FROM "from"
#define OPT_SIZE "size"
#define OPT_DENY "default-deny"
#define OPT_CACHE "cache"
#define OPT_UNLINK "unlink"
#define OPT_CONNECT_ADDR "conn-addr"
#define OPT_CONNECT_PORT "conn-port"
#define OPT_KILLSWITCH "killswitch"
#define OPT_MAX_SPEED "max-speed"
#define CMD_SERVE "serve"
#define CMD_LISTEN "listen"
#define CMD_READ "read"
#define CMD_WRITE "write"
#define CMD_ACL "acl"
#define CMD_MIRROR "mirror"
#define CMD_MIRROR_SPEED "mirror-speed"
#define CMD_BREAK "break"
#define CMD_STATUS "status"
#define CMD_HELP "help"
#define LEN_CMD_MAX 13
#define PATH_LEN_MAX 1024
#define ADDR_LEN_MAX 64
#define IS_CMD(x,c) (strncmp((x),(c),(LEN_CMD_MAX)) == 0)
#define GETOPT_HELP GETOPT_FLAG( OPT_HELP, 'h' )
#define GETOPT_DENY GETOPT_FLAG( OPT_DENY, 'd' )
#define GETOPT_ADDR GETOPT_ARG( OPT_ADDR, 'l' )
#define GETOPT_PORT GETOPT_ARG( OPT_PORT, 'p' )
#define GETOPT_FILE GETOPT_ARG( OPT_FILE, 'f' )
#define GETOPT_SOCK GETOPT_ARG( OPT_SOCK, 's' )
#define GETOPT_FROM GETOPT_ARG( OPT_FROM, 'F' )
#define GETOPT_SIZE GETOPT_ARG( OPT_SIZE, 'S' )
#define GETOPT_BIND GETOPT_ARG( OPT_BIND, 'b' )
#define GETOPT_CACHE GETOPT_OPTARG( OPT_CACHE, 'c' )
#define GETOPT_UNLINK GETOPT_ARG( OPT_UNLINK, 'u' )
#define GETOPT_CONNECT_ADDR GETOPT_ARG( OPT_CONNECT_ADDR, 'C' )
#define GETOPT_CONNECT_PORT GETOPT_ARG( OPT_CONNECT_PORT, 'P' )
#define GETOPT_KILLSWITCH GETOPT_ARG( OPT_KILLSWITCH, 'k' )
#define GETOPT_MAX_SPEED GETOPT_ARG( OPT_MAX_SPEED, 'm' )
#define OPT_VERBOSE "verbose"
#define SOPT_VERBOSE "v"
#define GETOPT_VERBOSE GETOPT_FLAG( OPT_VERBOSE, 'v' )
#define VERBOSE_LINE \
"\t--" OPT_VERBOSE ",-" SOPT_VERBOSE "\t\tOutput debug information.\n"
#ifdef DEBUG
#define VERBOSE_LOG_LEVEL 0
#else
#define VERBOSE_LOG_LEVEL 1
#endif
#define QUIET_LOG_LEVEL 4
#define OPT_QUIET "quiet"
#define SOPT_QUIET "q"
#define GETOPT_QUIET GETOPT_FLAG( OPT_QUIET, 'q' )
#define QUIET_LINE \
"\t--" OPT_QUIET ",-" SOPT_QUIET "\t\tOutput only fatal information.\n"
#define HELP_LINE \
"\t--" OPT_HELP ",-h \tThis text.\n"
#define SOCK_LINE \
"\t--" OPT_SOCK ",-s <SOCK>\tPath to the control socket.\n"
#define BIND_LINE \
"\t--" OPT_BIND ",-b <BIND-ADDR>\tBind the local socket to a particular IP address.\n"
#define MAX_SPEED_LINE \
"\t--" OPT_MAX_SPEED ",-m <bps>\tMaximum speed of the migration, in bytes/sec.\n"
char *help_help_text;
#endif

61
src/common/nbdtypes.c Normal file
View File

@@ -0,0 +1,61 @@
#include "nbdtypes.h"
#include <string.h>
#include <endian.h>
/**
* We intentionally ignore the reserved 128 bytes at the end of the
* request, since there's nothing we can do with them.
*/
void nbd_r2h_init(struct nbd_init_raw *from, struct nbd_init *to)
{
memcpy(to->passwd, from->passwd, 8);
to->magic = be64toh(from->magic);
to->size = be64toh(from->size);
to->flags = be32toh(from->flags);
}
void nbd_h2r_init(struct nbd_init *from, struct nbd_init_raw *to)
{
memcpy(to->passwd, from->passwd, 8);
to->magic = htobe64(from->magic);
to->size = htobe64(from->size);
to->flags = htobe32(from->flags);
}
void nbd_r2h_request(struct nbd_request_raw *from, struct nbd_request *to)
{
to->magic = be32toh(from->magic);
to->flags = be16toh(from->flags);
to->type = be16toh(from->type);
to->handle.w = from->handle.w;
to->from = be64toh(from->from);
to->len = be32toh(from->len);
}
void nbd_h2r_request(struct nbd_request *from, struct nbd_request_raw *to)
{
to->magic = htobe32(from->magic);
to->flags = htobe16(from->flags);
to->type = htobe16(from->type);
to->handle.w = from->handle.w;
to->from = htobe64(from->from);
to->len = htobe32(from->len);
}
void nbd_r2h_reply(struct nbd_reply_raw *from, struct nbd_reply *to)
{
to->magic = be32toh(from->magic);
to->error = be32toh(from->error);
to->handle.w = from->handle.w;
}
void nbd_h2r_reply(struct nbd_reply *from, struct nbd_reply_raw *to)
{
to->magic = htobe32(from->magic);
to->error = htobe32(from->error);
to->handle.w = from->handle.w;
}

114
src/common/nbdtypes.h Normal file
View File

@@ -0,0 +1,114 @@
#ifndef __NBDTYPES_H
#define __NBDTYPES_H
/* http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-09/2332.html */
#define INIT_PASSWD "NBDMAGIC"
#define INIT_MAGIC 0x0000420281861253
#define REQUEST_MAGIC 0x25609513
#define REPLY_MAGIC 0x67446698
#define REQUEST_READ 0
#define REQUEST_WRITE 1
#define REQUEST_DISCONNECT 2
#define REQUEST_FLUSH 3
/* values for transmission flag field */
#define FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
#define FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
#define FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */
/* values for command flag field */
#define CMD_FLAG_FUA (1 << 0)
#if 0
/* Not yet implemented by flexnbd */
#define REQUEST_TRIM 4
#define REQUEST_WRITE_ZEROES 6
#define FLAG_READ_ONLY (1 << 1) /* Device is read-only */
#define FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */
#define FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */
#define FLAG_SEND_WRITE_ZEROES (1 << 6) /* Send NBD_CMD_WRITE_ZEROES */
#define FLAG_CAN_MULTI_CONN (1 << 8) /* multiple connections are okay */
#define CMD_FLAG_NO_HOLE (1 << 1)
#endif
/* 32 MiB is the maximum qemu will send you:
* https://github.com/qemu/qemu/blob/v2.11.0/include/block/nbd.h#L183
*/
#define NBD_MAX_SIZE ( 32 * 1024 * 1024 )
#define NBD_REQUEST_SIZE ( sizeof( struct nbd_request_raw ) )
#define NBD_REPLY_SIZE ( sizeof( struct nbd_reply_raw ) )
#include <linux/types.h>
#include <inttypes.h>
typedef union nbd_handle_t {
uint8_t b[8];
uint64_t w;
} nbd_handle_t;
/* The _raw types are the types as they appear on the wire. Non-_raw
* types are in host-format.
* Conversion functions are _r2h_ for converting raw to host, and _h2r_
* for converting host to raw.
*/
struct nbd_init_raw {
char passwd[8];
__be64 magic;
__be64 size;
__be32 flags;
char reserved[124];
};
struct nbd_request_raw {
__be32 magic;
__be16 flags;
__be16 type; /* == READ || == WRITE || == FLUSH */
nbd_handle_t handle;
__be64 from;
__be32 len;
} __attribute__ ((packed));
struct nbd_reply_raw {
__be32 magic;
__be32 error; /* 0 = ok, else error */
nbd_handle_t handle; /* handle you got from request */
};
struct nbd_init {
char passwd[8];
uint64_t magic;
uint64_t size;
uint32_t flags;
char reserved[124];
};
struct nbd_request {
uint32_t magic;
uint16_t flags;
uint16_t type; /* == READ || == WRITE || == DISCONNECT || == FLUSH */
nbd_handle_t handle;
uint64_t from;
uint32_t len;
} __attribute__ ((packed));
struct nbd_reply {
uint32_t magic;
uint32_t error; /* 0 = ok, else error */
nbd_handle_t handle; /* handle you got from request */
};
void nbd_r2h_init(struct nbd_init_raw *from, struct nbd_init *to);
void nbd_r2h_request(struct nbd_request_raw *from, struct nbd_request *to);
void nbd_r2h_reply(struct nbd_reply_raw *from, struct nbd_reply *to);
void nbd_h2r_init(struct nbd_init *from, struct nbd_init_raw *to);
void nbd_h2r_request(struct nbd_request *from, struct nbd_request_raw *to);
void nbd_h2r_reply(struct nbd_reply *from, struct nbd_reply_raw *to);
#endif

125
src/common/parse.c Normal file
View File

@@ -0,0 +1,125 @@
#include "parse.h"
#include "util.h"
int atoi(const char *nptr);
#define IS_IP_VALID_CHAR(x) ( ((x) >= '0' && (x) <= '9' ) || \
((x) >= 'a' && (x) <= 'f') || \
((x) >= 'A' && (x) <= 'F' ) || \
(x) == ':' || (x) == '.' \
)
/* FIXME: should change this to return negative on error like everything else */
int parse_ip_to_sockaddr(struct sockaddr *out, char *src)
{
NULLCHECK(out);
NULLCHECK(src);
char temp[64];
struct sockaddr_in *v4 = (struct sockaddr_in *) out;
struct sockaddr_in6 *v6 = (struct sockaddr_in6 *) out;
/* allow user to start with [ and end with any other invalid char */
{
int i = 0, j = 0;
if (src[i] == '[') {
i++;
}
for (; i < 64 && IS_IP_VALID_CHAR(src[i]); i++) {
temp[j++] = src[i];
}
temp[j] = 0;
}
if (temp[0] == '0' && temp[1] == '\0') {
v4->sin_family = AF_INET;
v4->sin_addr.s_addr = INADDR_ANY;
return 1;
}
if (inet_pton(AF_INET, temp, &v4->sin_addr) == 1) {
out->sa_family = AF_INET;
return 1;
}
if (inet_pton(AF_INET6, temp, &v6->sin6_addr) == 1) {
out->sa_family = AF_INET6;
return 1;
}
return 0;
}
int parse_to_sockaddr(struct sockaddr *out, char *address)
{
struct sockaddr_un *un = (struct sockaddr_un *) out;
NULLCHECK(address);
if (address[0] == '/') {
un->sun_family = AF_UNIX;
strncpy(un->sun_path, address, 108); /* FIXME: linux only */
return 1;
}
return parse_ip_to_sockaddr(out, address);
}
int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
{
struct ip_and_mask *list;
int i;
if (max == 0) {
*out = NULL;
return 0;
} else {
list = xmalloc(max * sizeof(struct ip_and_mask));
*out = (struct ip_and_mask(*)[]) list;
debug("acl alloc: %p", *out);
}
for (i = 0; i < max; i++) {
int j;
struct ip_and_mask *outentry = &list[i];
# define MAX_MASK_BITS (outentry->ip.family == AF_INET ? 32 : 128)
if (parse_ip_to_sockaddr(&outentry->ip.generic, entries[i]) == 0) {
return i;
}
for (j = 0; entries[i][j] && entries[i][j] != '/'; j++); // increment j!
if (entries[i][j] == '/') {
outentry->mask = atoi(entries[i] + j + 1);
if (outentry->mask < 1 || outentry->mask > MAX_MASK_BITS) {
return i;
}
} else {
outentry->mask = MAX_MASK_BITS;
}
# undef MAX_MASK_BITS
debug("acl ptr[%d]: %p %d", i, outentry, outentry->mask);
}
for (i = 0; i < max; i++) {
debug("acl entry %d @ %p has mask %d", i, list[i], list[i].mask);
}
return max;
}
void parse_port(char *s_port, struct sockaddr_in *out)
{
NULLCHECK(s_port);
int raw_port;
raw_port = atoi(s_port);
if (raw_port < 0 || raw_port > 65535) {
fatal("Port number must be >= 0 and <= 65535");
}
out->sin_port = htobe16(raw_port);
}

28
src/common/parse.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef PARSE_H
#define PARSE_H
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
union mysockaddr {
unsigned short family;
struct sockaddr generic;
struct sockaddr_in v4;
struct sockaddr_in6 v6;
struct sockaddr_un un;
};
struct ip_and_mask {
union mysockaddr ip;
int mask;
};
int parse_ip_to_sockaddr(struct sockaddr *out, char *src);
int parse_to_sockaddr(struct sockaddr *out, char *src);
int parse_acl(struct ip_and_mask (**out)[], int max, char **entries);
void parse_port(char *s_port, struct sockaddr_in *out);
#endif

268
src/common/readwrite.c Normal file
View File

@@ -0,0 +1,268 @@
#include "nbdtypes.h"
#include "ioutil.h"
#include "sockutil.h"
#include "util.h"
#include "serve.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
int socket_connect(struct sockaddr *to, struct sockaddr *from)
{
int fd =
socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM,
0);
if (fd < 0) {
warn("Couldn't create client socket");
return -1;
}
if (NULL != from) {
if (0 > bind(fd, from, sizeof(struct sockaddr_in6))) {
warn(SHOW_ERRNO("bind() to source address failed"));
if (0 > close(fd)) { /* Non-fatal leak */
warn(SHOW_ERRNO("Failed to close fd %i", fd));
}
return -1;
}
}
if (0 > sock_try_connect(fd, to, sizeof(struct sockaddr_in6), 15)) {
warn(SHOW_ERRNO("connect failed"));
if (0 > close(fd)) { /* Non-fatal leak */
warn(SHOW_ERRNO("Failed to close fd %i", fd));
}
return -1;
}
if (sock_set_tcp_nodelay(fd, 1) == -1) {
warn(SHOW_ERRNO("Failed to set TCP_NODELAY"));
}
return fd;
}
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
uint32_t * out_flags)
{
if (strncmp(init_raw->passwd, INIT_PASSWD, 8) != 0) {
warn("wrong passwd");
goto fail;
}
if (be64toh(init_raw->magic) != INIT_MAGIC) {
warn("wrong magic (%x)", be64toh(init_raw->magic));
goto fail;
}
if (NULL != out_size) {
*out_size = be64toh(init_raw->size);
}
if (NULL != out_flags) {
*out_flags = be32toh(init_raw->flags);
}
return 1;
fail:
return 0;
}
int socket_nbd_read_hello(int fd, uint64_t * out_size,
uint32_t * out_flags)
{
struct nbd_init_raw init_raw;
if (0 > readloop(fd, &init_raw, sizeof(init_raw))) {
warn(SHOW_ERRNO("Couldn't read init"));
return 0;
}
return nbd_check_hello(&init_raw, out_size, out_flags);
}
void nbd_hello_to_buf(struct nbd_init_raw *buf, off64_t out_size,
uint32_t out_flags)
{
struct nbd_init init;
memcpy(&init.passwd, INIT_PASSWD, 8);
init.magic = INIT_MAGIC;
init.size = out_size;
init.flags = out_flags;
memset(buf, 0, sizeof(struct nbd_init_raw)); // ensure reserved is 0s
nbd_h2r_init(&init, buf);
return;
}
int socket_nbd_write_hello(int fd, off64_t out_size, uint32_t out_flags)
{
struct nbd_init_raw init_raw;
nbd_hello_to_buf(&init_raw, out_size, out_flags);
if (0 > writeloop(fd, &init_raw, sizeof(init_raw))) {
warn(SHOW_ERRNO("failed to write hello to socket"));
return 0;
}
return 1;
}
void fill_request(struct nbd_request_raw *request_raw, uint16_t type,
uint16_t flags, uint64_t from, uint32_t len)
{
request_raw->magic = htobe32(REQUEST_MAGIC);
request_raw->type = htobe16(type);
request_raw->flags = htobe16(flags);
request_raw->handle.w =
(((uint64_t) rand()) << 32) | ((uint64_t) rand());
request_raw->from = htobe64(from);
request_raw->len = htobe32(len);
}
void read_reply(int fd, uint64_t request_raw_handle,
struct nbd_reply *reply)
{
struct nbd_reply_raw reply_raw;
ERROR_IF_NEGATIVE(readloop
(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
SHOW_ERRNO("Couldn't read reply"));
nbd_r2h_reply(&reply_raw, reply);
if (reply->magic != REPLY_MAGIC) {
error("Reply magic incorrect (%x)", reply->magic);
}
if (reply->error != 0) {
error("Server replied with error %d", reply->error);
}
if (request_raw_handle != reply_raw.handle.w) {
error("Did not reply with correct handle");
}
}
void wait_for_data(int fd, int timeout_secs)
{
fd_set fds;
struct timeval tv = { timeout_secs, 0 };
int selected;
FD_ZERO(&fds);
FD_SET(fd, &fds);
selected =
sock_try_select(FD_SETSIZE, &fds, NULL, NULL,
timeout_secs >= 0 ? &tv : NULL);
FATAL_IF(-1 == selected, "Select failed");
ERROR_IF(0 == selected, "Timed out waiting for reply");
}
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs)
{
struct nbd_request_raw request_raw;
struct nbd_reply reply;
fill_request(&request_raw, REQUEST_READ, 0, from, len);
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
SHOW_ERRNO("Couldn't write request"));
wait_for_data(fd, timeout_secs);
read_reply(fd, request_raw.handle.w, &reply);
if (out_buf) {
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len),
SHOW_ERRNO("Read failed"));
} else {
FATAL_IF_NEGATIVE(splice_via_pipe_loop(fd, out_fd, len),
"Splice failed");
}
}
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd,
void *in_buf, int timeout_secs)
{
struct nbd_request_raw request_raw;
struct nbd_reply reply;
fill_request(&request_raw, REQUEST_WRITE, 0, from, len);
ERROR_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
SHOW_ERRNO("Couldn't write request"));
if (in_buf) {
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len),
SHOW_ERRNO("Write failed"));
} else {
ERROR_IF_NEGATIVE(splice_via_pipe_loop(in_fd, fd, len),
"Splice failed");
}
wait_for_data(fd, timeout_secs);
read_reply(fd, request_raw.handle.w, &reply);
}
int socket_nbd_disconnect(int fd)
{
int success = 1;
struct nbd_request_raw request_raw;
fill_request(&request_raw, REQUEST_DISCONNECT, 0, 0, 0);
/* FIXME: This shouldn't be a FATAL error. We should just drop
* the mirror without affecting the main server.
*/
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
SHOW_ERRNO
("Failed to write the disconnect request."));
return success;
}
#define CHECK_RANGE(error_type) { \
uint64_t size;\
uint32_t flags;\
int success = socket_nbd_read_hello(params->client, &size, &flags); \
if ( success ) {\
uint64_t endpoint = params->from + params->len; \
if (endpoint > size || \
endpoint < params->from ) { /* this happens on overflow */ \
fatal(error_type \
" request %d+%d is out of range given size %d", \
params->from, params->len, size\
);\
}\
}\
else {\
fatal( error_type " connection failed." );\
}\
}
void do_read(struct mode_readwrite_params *params)
{
params->client =
socket_connect(&params->connect_to.generic,
&params->connect_from.generic);
FATAL_IF_NEGATIVE(params->client, "Couldn't connect.");
CHECK_RANGE("read");
socket_nbd_read(params->client, params->from, params->len,
params->data_fd, NULL, 10);
close(params->client);
}
void do_write(struct mode_readwrite_params *params)
{
params->client =
socket_connect(&params->connect_to.generic,
&params->connect_from.generic);
FATAL_IF_NEGATIVE(params->client, "Couldn't connect.");
CHECK_RANGE("write");
socket_nbd_write(params->client, params->from, params->len,
params->data_fd, NULL, 10);
close(params->client);
}

26
src/common/readwrite.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef READWRITE_H
#define READWRITE_H
#include <sys/types.h>
#include <sys/socket.h>
#include "nbdtypes.h"
int socket_connect(struct sockaddr *to, struct sockaddr *from);
int socket_nbd_read_hello(int fd, uint64_t * size, uint32_t * flags);
int socket_nbd_write_hello(int fd, uint64_t size, uint32_t flags);
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs);
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs);
int socket_nbd_disconnect(int fd);
/* as you can see, we're slowly accumulating code that should really be in an
* NBD library */
void nbd_hello_to_buf(struct nbd_init_raw *buf, uint64_t out_size,
uint32_t out_flags);
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
uint32_t * out_flags);
#endif

65
src/common/remote.c Normal file
View File

@@ -0,0 +1,65 @@
#include "ioutil.h"
#include "util.h"
#include <stdlib.h>
#include <sys/un.h>
static const int max_response = 1024;
void print_response(const char *response)
{
char *response_text;
FILE *out;
int exit_status;
NULLCHECK(response);
exit_status = atoi(response);
response_text = strchr(response, ':');
FATAL_IF_NULL(response_text,
"Error parsing server response: '%s'", response);
out = exit_status > 0 ? stderr : stdout;
fprintf(out, "%s\n", response_text + 2);
}
void do_remote_command(char *command, char *socket_name, int argc,
char **argv)
{
char newline = 10;
int i;
debug("connecting to run remote command %s", command);
int remote = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un address;
char response[max_response];
memset(&address, 0, sizeof(address));
FATAL_IF_NEGATIVE(remote, "Couldn't create client socket");
address.sun_family = AF_UNIX;
strncpy(address.sun_path, socket_name, sizeof(address.sun_path));
FATAL_IF_NEGATIVE(connect
(remote, (struct sockaddr *) &address,
sizeof(address)), "Couldn't connect to %s",
socket_name);
write(remote, command, strlen(command));
write(remote, &newline, 1);
for (i = 0; i < argc; i++) {
if (NULL != argv[i]) {
write(remote, argv[i], strlen(argv[i]));
}
write(remote, &newline, 1);
}
write(remote, &newline, 1);
FATAL_IF_NEGATIVE(read_until_newline(remote, response, max_response),
"Couldn't read response from %s", socket_name);
print_response(response);
exit(atoi(response));
}

150
src/common/self_pipe.c Normal file
View File

@@ -0,0 +1,150 @@
/**
* self_pipe.c
*
* author: Alex Young <alex@bytemark.co.uk>
*
* Wrapper for the self-pipe trick for select()-based thread
* synchronisation. Get yourself a self_pipe with self_pipe_create(),
* select() on the read end of the pipe with the help of
* self_pipe_fd_set( sig, fds ) and self_pipe_fd_isset( sig, fds ).
* When you've received a signal, clear it with
* self_pipe_signal_clear(sig) so that the buffer doesn't get filled.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "util.h"
#include "self_pipe.h"
#define ERR_MSG_PIPE "Couldn't open a pipe for signaling."
#define ERR_MSG_FCNTL "Couldn't set a signalling pipe non-blocking."
#define ERR_MSG_WRITE "Couldn't write to a signaling pipe."
#define ERR_MSG_READ "Couldn't read from a signaling pipe."
void self_pipe_server_error(int err, char *msg)
{
char errbuf[1024] = { 0 };
strerror_r(err, errbuf, 1024);
fatal("%s\t%d (%s)", msg, err, errbuf);
}
/**
* Allocate a struct self_pipe, opening the pipe.
*
* Returns NULL if the pipe couldn't be opened or if we couldn't set it
* non-blocking.
*
* Remember to call self_pipe_destroy when you're done with the return
* value.
*/
struct self_pipe *self_pipe_create(void)
{
struct self_pipe *sig = xmalloc(sizeof(struct self_pipe));
int fds[2];
if (NULL == sig) {
return NULL;
}
if (pipe(fds)) {
free(sig);
self_pipe_server_error(errno, ERR_MSG_PIPE);
return NULL;
}
if (fcntl(fds[0], F_SETFL, O_NONBLOCK)
|| fcntl(fds[1], F_SETFL, O_NONBLOCK)) {
int fcntl_err = errno;
while (close(fds[0]) == -1 && errno == EINTR);
while (close(fds[1]) == -1 && errno == EINTR);
free(sig);
self_pipe_server_error(fcntl_err, ERR_MSG_FCNTL);
return NULL;
}
sig->read_fd = fds[0];
sig->write_fd = fds[1];
return sig;
}
/**
* Send a signal to anyone select()ing on this signal.
*
* Returns 1 on success. Can fail if weirdness happened to the write fd
* of the pipe in the self_pipe struct.
*/
int self_pipe_signal(struct self_pipe *sig)
{
NULLCHECK(sig);
FATAL_IF(1 == sig->write_fd, "Shouldn't be writing to stdout");
FATAL_IF(2 == sig->write_fd, "Shouldn't be writing to stderr");
int written = write(sig->write_fd, "X", 1);
if (written != 1) {
self_pipe_server_error(errno, ERR_MSG_WRITE);
return 0;
}
return 1;
}
/**
* Clear a received signal from the pipe. Every signal sent must be
* cleared by one (and only one) recipient when they return from select()
* if the signal is to be used more than once.
* Returns the number of bytes read, which will be 1 on success and 0 if
* there was no signal.
*/
int self_pipe_signal_clear(struct self_pipe *sig)
{
char buf[1];
return 1 == read(sig->read_fd, buf, 1);
}
/**
* Close the pipe and free the self_pipe. Do not try to use the
* self_pipe struct after calling this, the innards are mush.
*/
int self_pipe_destroy(struct self_pipe *sig)
{
NULLCHECK(sig);
while (close(sig->read_fd) == -1 && errno == EINTR);
while (close(sig->write_fd) == -1 && errno == EINTR);
/* Just in case anyone *does* try to use this after free,
* we should set the memory locations to an error value
*/
sig->read_fd = -1;
sig->write_fd = -1;
free(sig);
return 1;
}
int self_pipe_fd_set(struct self_pipe *sig, fd_set * fds)
{
FD_SET(sig->read_fd, fds);
return 1;
}
int self_pipe_fd_isset(struct self_pipe *sig, fd_set * fds)
{
return FD_ISSET(sig->read_fd, fds);
}

19
src/common/self_pipe.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef SELF_PIPE_H
#define SELF_PIPE_H
#include <sys/select.h>
struct self_pipe {
int read_fd;
int write_fd;
};
struct self_pipe *self_pipe_create(void);
int self_pipe_signal(struct self_pipe *sig);
int self_pipe_signal_clear(struct self_pipe *sig);
int self_pipe_destroy(struct self_pipe *sig);
int self_pipe_fd_set(struct self_pipe *sig, fd_set * fds);
int self_pipe_fd_isset(struct self_pipe *sig, fd_set * fds);
#endif

295
src/common/sockutil.c Normal file
View File

@@ -0,0 +1,295 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include "sockutil.h"
#include "util.h"
size_t sockaddr_size(const struct sockaddr * sa)
{
struct sockaddr_un *un = (struct sockaddr_un *) sa;
size_t ret = 0;
switch (sa->sa_family) {
case AF_INET:
ret = sizeof(struct sockaddr_in);
break;
case AF_INET6:
ret = sizeof(struct sockaddr_in6);
break;
case AF_UNIX:
ret = sizeof(un->sun_family) + SUN_LEN(un);
break;
}
return ret;
}
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
size_t len)
{
NULLCHECK(sa);
NULLCHECK(dest);
struct sockaddr_in *in = (struct sockaddr_in *) sa;
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) sa;
struct sockaddr_un *un = (struct sockaddr_un *) sa;
unsigned short real_port = ntohs(in->sin_port); // common to in and in6
const char *ret = NULL;
memset(dest, 0, len);
if (sa->sa_family == AF_INET) {
ret = inet_ntop(AF_INET, &in->sin_addr, dest, len);
} else if (sa->sa_family == AF_INET6) {
ret = inet_ntop(AF_INET6, &in6->sin6_addr, dest, len);
} else if (sa->sa_family == AF_UNIX) {
ret = strncpy(dest, un->sun_path, SUN_LEN(un));
}
if (ret == NULL) {
strncpy(dest, "???", len);
}
if (NULL != ret && real_port > 0 && sa->sa_family != AF_UNIX) {
size_t size = strlen(dest);
snprintf(dest + size, len - size, " port %d", real_port);
}
return ret;
}
int sock_set_reuseaddr(int fd, int optval)
{
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
}
int sock_set_keepalive_params(int fd, int time, int intvl, int probes)
{
if (sock_set_keepalive(fd, 1) ||
sock_set_tcp_keepidle(fd, time) ||
sock_set_tcp_keepintvl(fd, intvl) ||
sock_set_tcp_keepcnt(fd, probes)) {
return -1;
}
return 0;
}
int sock_set_keepalive(int fd, int optval)
{
return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval,
sizeof(optval));
}
int sock_set_tcp_keepidle(int fd, int optval)
{
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval,
sizeof(optval));
}
int sock_set_tcp_keepintvl(int fd, int optval)
{
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval,
sizeof(optval));
}
int sock_set_tcp_keepcnt(int fd, int optval)
{
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval,
sizeof(optval));
}
/* Set the tcp_nodelay option */
int sock_set_tcp_nodelay(int fd, int optval)
{
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval,
sizeof(optval));
}
int sock_set_tcp_cork(int fd, int optval)
{
return setsockopt(fd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval));
}
int sock_set_nonblock(int fd, int optval)
{
int flags = fcntl(fd, F_GETFL);
if (flags == -1) {
return -1;
}
if (optval) {
flags = flags | O_NONBLOCK;
} else {
flags = flags & (~O_NONBLOCK);
}
return fcntl(fd, F_SETFL, flags);
}
int sock_try_bind(int fd, const struct sockaddr *sa)
{
int bind_result;
char s_address[256];
int retry = 10;
sockaddr_address_string(sa, &s_address[0], 256);
do {
bind_result = bind(fd, sa, sockaddr_size(sa));
if (0 == bind_result) {
info("Bound to %s", s_address);
break;
} else {
warn(SHOW_ERRNO("Couldn't bind to %s", s_address));
switch (errno) {
/* bind() can give us EACCES, EADDRINUSE, EADDRNOTAVAIL, EBADF,
* EINVAL, ENOTSOCK, EFAULT, ELOOP, ENAMETOOLONG, ENOENT,
* ENOMEM, ENOTDIR, EROFS
*
* Any of these other than EADDRINUSE & EADDRNOTAVAIL signify
* that there's a logic error somewhere.
*
* EADDRINUSE is fatal: if there's something already where we
* want to be listening, we have no guarantees that any clients
* will cope with it.
*/
case EADDRNOTAVAIL:
retry--;
if (retry) {
debug("retrying");
sleep(1);
}
continue;
case EADDRINUSE:
warn("%s in use, giving up.", s_address);
retry = 0;
break;
default:
warn("giving up");
retry = 0;
}
}
} while (retry);
return bind_result;
}
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
fd_set * exceptfds, struct timeval *timeout)
{
int result;
do {
result = select(nfds, readfds, writefds, exceptfds, timeout);
if (errno != EINTR) {
break;
}
} while (result == -1);
return result;
}
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
int wait)
{
fd_set fds;
struct timeval tv = { wait, 0 };
int result = 0;
if (sock_set_nonblock(fd, 1) == -1) {
warn(SHOW_ERRNO
("Failed to set socket non-blocking for connect()"));
return connect(fd, to, addrlen);
}
FD_ZERO(&fds);
FD_SET(fd, &fds);
do {
result = connect(fd, to, addrlen);
if (result == -1) {
switch (errno) {
case EINPROGRESS:
result = 0;
break; /* success */
case EAGAIN:
case EINTR:
/* Try connect() again. This only breaks out of the switch,
* not the do...while loop. since result == -1, we go again.
*/
break;
default:
warn(SHOW_ERRNO("Failed to connect()"));
goto out;
}
}
} while (result == -1);
if (-1 == sock_try_select(FD_SETSIZE, NULL, &fds, NULL, &tv)) {
warn(SHOW_ERRNO("failed to select() on non-blocking connect"));
result = -1;
goto out;
}
if (!FD_ISSET(fd, &fds)) {
result = -1;
errno = ETIMEDOUT;
goto out;
}
int scratch;
socklen_t s_size = sizeof(scratch);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &scratch, &s_size) == -1) {
result = -1;
warn(SHOW_ERRNO("getsockopt() failed"));
goto out;
}
if (scratch == EINPROGRESS) {
scratch = ETIMEDOUT;
}
result = scratch ? -1 : 0;
errno = scratch;
out:
if (sock_set_nonblock(fd, 0) == -1) {
warn(SHOW_ERRNO("Failed to make socket blocking after connect()"));
return -1;
}
debug("sock_try_connect: %i", result);
return result;
}
int sock_try_close(int fd)
{
int result;
do {
result = close(fd);
if (result == -1) {
if (EINTR == errno) {
continue; /* retry EINTR */
} else {
warn(SHOW_ERRNO("Failed to close() fd %i", fd));
break; /* Other errors get reported */
}
}
} while (0);
return result;
}

58
src/common/sockutil.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef SOCKUTIL_H
#define SOCKUTIL_H
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
/* Returns the size of the sockaddr, or 0 on error */
size_t sockaddr_size(const struct sockaddr *sa);
/* Convert a sockaddr into an address. Like inet_ntop, it returns dest if
* successful, NULL otherwise. In the latter case, dest will contain "???"
*/
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
size_t len);
/* Configure TCP keepalive on a socket */
int sock_set_keepalive_params(int fd, int time, int intvl, int probes);
/* Set the SOL_KEEPALIVE otion */
int sock_set_keepalive(int fd, int optval);
/* Set the SOL_REUSEADDR otion */
int sock_set_reuseaddr(int fd, int optval);
/* Set the tcp_keepidle option */
int sock_set_tcp_keepidle(int fd, int optval);
/* Set the tcp_keepintvl option */
int sock_set_tcp_keepintvl(int fd, int optval);
/* Set the tcp_keepcnt option */
int sock_set_tcp_keepcnt(int fd, int optval);
/* Set the tcp_nodelay option */
int sock_set_tcp_nodelay(int fd, int optval);
/* Set the tcp_cork option */
int sock_set_tcp_cork(int fd, int optval);
int sock_set_nonblock(int fd, int optval);
/* Attempt to bind the fd to the sockaddr, retrying common transient failures */
int sock_try_bind(int fd, const struct sockaddr *sa);
/* Try to call select(), retrying EINTR */
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
fd_set * exceptfds, struct timeval *timeout);
/* Try to call connect(), timing out after wait seconds */
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
int wait);
/* Try to call close(), retrying EINTR */
int sock_try_close(int fd);
#endif

91
src/common/util.c Normal file
View File

@@ -0,0 +1,91 @@
#include <stdarg.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <time.h>
#include "util.h"
pthread_key_t cleanup_handler_key;
int log_level = 2;
char *log_context = "";
void error_init(void)
{
pthread_key_create(&cleanup_handler_key, free);
}
void error_handler(int fatal)
{
DECLARE_ERROR_CONTEXT(context);
if (context) {
longjmp(context->jmp, fatal ? 1 : 2);
} else {
if (fatal) {
abort();
} else {
pthread_exit((void *) 1);
}
}
}
void exit_err(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
void mylog(int line_level, const char *format, ...)
{
if (line_level < log_level) {
return;
}
va_list argptr;
va_start(argptr, format);
vfprintf(stderr, format, argptr);
va_end(argptr);
}
uint64_t monotonic_time_ms()
{
struct timespec ts;
uint64_t seconds_ms, nanoseconds_ms;
FATAL_IF_NEGATIVE(clock_gettime(CLOCK_MONOTONIC, &ts),
SHOW_ERRNO("clock_gettime failed")
);
seconds_ms = ts.tv_sec;
seconds_ms = seconds_ms * 1000;
nanoseconds_ms = ts.tv_nsec;
nanoseconds_ms = nanoseconds_ms / 1000000;
return seconds_ms + nanoseconds_ms;
}
void *xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes",
ptr ? "realloc" : "malloc", size);
return p;
}
void *xmalloc(size_t size)
{
void *p = xrealloc(NULL, size);
memset(p, 0, size);
return p;
}

165
src/common/util.h Normal file
View File

@@ -0,0 +1,165 @@
#ifndef __UTIL_H
#define __UTIL_H
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <inttypes.h>
void *xrealloc(void *ptr, size_t size);
void *xmalloc(size_t size);
typedef void (cleanup_handler) (void * /* context */ ,
int /* is fatal? */ );
/* set from 0 - 5 depending on what level of verbosity you want */
extern int log_level;
/* set up the error globals */
void error_init(void);
/* some context for the overall process that appears on each log line */
extern char *log_context;
void exit_err(const char *);
/* error_set_handler must be a macro not a function due to setjmp stack rules */
#include <setjmp.h>
struct error_handler_context {
jmp_buf jmp;
cleanup_handler *handler;
void *data;
};
#define DECLARE_ERROR_CONTEXT(name) \
struct error_handler_context *name = (struct error_handler_context*) \
pthread_getspecific(cleanup_handler_key);
/* clean up with the given function & data when error_handler() is invoked,
* non-fatal errors will also return here (if that's dangerous, use fatal()
* instead of error()).
*
* error handlers are thread-local, so you need to call this when starting a
* new thread.
*/
extern pthread_key_t cleanup_handler_key;
#define error_set_handler(cleanfn, cleandata) \
{ \
DECLARE_ERROR_CONTEXT(old); \
struct error_handler_context *context = \
xmalloc(sizeof(struct error_handler_context)); \
context->handler = (cleanfn); \
context->data = (cleandata); \
\
switch (setjmp(context->jmp)) \
{ \
case 0: /* setup time */ \
if (old) { free(old); }\
if( EINVAL == pthread_setspecific(cleanup_handler_key, context) ) { \
fprintf(stderr, "Tried to set an error handler at %s:%d" \
" without calling error_init().\n", \
__FILE__, __LINE__ );\
abort();\
}\
break; \
case 1: /* fatal error, terminate process */ \
debug( "Fatal error in thread %p", pthread_self() ); \
abort(); \
/* abort() can't return, so we can't fall through */ \
case 2: /* non-fatal error, call cleanup and terminate thread */ \
debug( "Error in thread %p", pthread_self() ); \
context->handler(context->data, 0); \
pthread_exit((void*) 1);\
/* pthread_exit() can't return, so we can't fall through
* */\
default: \
abort(); \
} \
}
/* invoke the error handler - longjmps away, don't use directly */
void error_handler(int fatal);
/* mylog a line at the given level (0 being most verbose) */
void mylog(int line_level, const char *format, ...);
/* Returns the current time, in milliseconds, from CLOCK_MONOTONIC */
uint64_t monotonic_time_ms(void);
#define levstr(i) (i==0?'D':(i==1?'I':(i==2?'W':(i==3?'E':'F'))))
#define myloglev(level, msg, ...) mylog( level, "%"PRIu64":%c:%d %p %s %s:%d: "msg"\n", monotonic_time_ms(), levstr(level), getpid(),pthread_self(), log_context, __FILE__, __LINE__, ##__VA_ARGS__ )
#ifdef DEBUG
#define debug(msg, ...) myloglev(0, msg, ##__VA_ARGS__)
#else
#define debug(msg, ...) /* no-op */
#endif
/* informational message, not expected to be compiled out */
#define info(msg, ...) myloglev(1, msg, ##__VA_ARGS__)
/* messages that might indicate a problem */
#define warn(msg, ...) myloglev(2, msg, ##__VA_ARGS__)
/* mylog a message and invoke the error handler to recover */
#define error(msg, ...) do { \
myloglev(3, msg, ##__VA_ARGS__); \
error_handler(0); \
} while(0)
/* mylog a message and invoke the error handler to kill the current thread */
#define fatal(msg, ...) do { \
myloglev(4, msg, ##__VA_ARGS__); \
error_handler(1); \
exit(1); /* never-reached, this is to make static code analizer happy */ \
} while(0)
#define ERROR_IF( test, msg, ... ) do { if ((test)) { error(msg, ##__VA_ARGS__); } } while(0)
#define FATAL_IF( test, msg, ... ) do { if ((test)) { fatal(msg, ##__VA_ARGS__); } } while(0)
#define ERROR_UNLESS( test, msg, ... ) ERROR_IF( !(test), msg, ##__VA_ARGS__ )
#define FATAL_UNLESS( test, msg, ... ) FATAL_IF( !(test), msg, ##__VA_ARGS__ )
#define ERROR_IF_NULL(value, msg, ...) \
ERROR_IF( NULL == value, msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno) )
#define FATAL_IF_NULL(value, msg, ...) \
FATAL_IF( NULL == value, msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno) )
#define ERROR_IF_NEGATIVE( value, msg, ... ) ERROR_IF( value < 0, msg, ##__VA_ARGS__ )
#define FATAL_IF_NEGATIVE( value, msg, ... ) FATAL_IF( value < 0, msg, ##__VA_ARGS__ )
#define ERROR_IF_ZERO( value, msg, ... ) ERROR_IF( 0 == value, msg, ##__VA_ARGS__ )
#define FATAL_IF_ZERO( value, msg, ... ) FATAL_IF( 0 == value, msg, ##__VA_ARGS__ )
#define ERROR_UNLESS_NULL(value, msg, ...) \
ERROR_UNLESS( NULL == value, msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno) )
#define FATAL_UNLESS_NULL(value, msg, ...) \
FATAL_UNLESS( NULL == value, msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno) )
#define ERROR_UNLESS_NEGATIVE( value, msg, ... ) ERROR_UNLESS( value < 0, msg, ##__VA_ARGS__ )
#define FATAL_UNLESS_NEGATIVE( value, msg, ... ) FATAL_UNLESS( value < 0, msg, ##__VA_ARGS__ )
#define ERROR_UNLESS_ZERO( value, msg, ... ) ERROR_UNLESS( 0 == value, msg, ##__VA_ARGS__ )
#define FATAL_UNLESS_ZERO( value, msg, ... ) FATAL_UNLESS( 0 == value, msg, ##__VA_ARGS__ )
#define NULLCHECK(value) FATAL_IF_NULL(value, "BUG: " #value " is null")
#define SHOW_ERRNO( msg, ... ) msg ": %s (%i)", ##__VA_ARGS__, ( errno == 0 ? "EOF" : strerror(errno) ), errno
#define WARN_IF_NEGATIVE( value, msg, ... ) if ( value < 0 ) { warn( msg, ##__VA_ARGS__ ); }
#endif

21
src/main.c Normal file
View File

@@ -0,0 +1,21 @@
#include "util.h"
#include "mode.h"
#include <signal.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
signal(SIGPIPE, SIG_IGN); /* calls to splice() unhelpfully throw this */
error_init();
srand(time(NULL));
if (argc < 2) {
exit_err(help_help_text);
}
mode(argv[1], argc - 1, argv + 1); /* never returns */
return 0;
}

165
src/proxy-main.c Normal file
View File

@@ -0,0 +1,165 @@
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include "mode.h"
#include "util.h"
#include "proxy.h"
static struct option proxy_options[] = {
GETOPT_HELP,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_CONNECT_ADDR,
GETOPT_CONNECT_PORT,
GETOPT_BIND,
GETOPT_CACHE,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char proxy_short_options[] = "hl:p:C:P:b:" SOPT_QUIET SOPT_VERBOSE;
static char proxy_help_text[] =
"Usage: flexnbd-proxy <options>\n\n"
"Resiliently proxy an NBD connection between client and server\n"
"We can listen on TCP or UNIX socket, but only connect to TCP servers.\n\n"
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address we will bind to as a proxy.\n"
"\t--" OPT_PORT
",-p <PORT>\tThe port we will bind to as a proxy, if required.\n"
"\t--" OPT_CONNECT_ADDR ",-C <ADDR>\tAddress of the proxied server.\n"
"\t--" OPT_CONNECT_PORT ",-P <PORT>\tPort of the proxied server.\n"
"\t--" OPT_BIND
",-b <ADDR>\tThe address we connect from, as a proxy.\n" "\t--"
OPT_CACHE
",-c[=<CACHE-BYTES>]\tUse a RAM read cache of the given size.\n"
QUIET_LINE VERBOSE_LINE;
static char proxy_default_cache_size[] = "4096";
void read_proxy_param(int c,
char **downstream_addr,
char **downstream_port,
char **upstream_addr,
char **upstream_port,
char **bind_addr, char **cache_bytes)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", proxy_help_text);
exit(0);
case 'l':
*downstream_addr = optarg;
break;
case 'p':
*downstream_port = optarg;
break;
case 'C':
*upstream_addr = optarg;
break;
case 'P':
*upstream_port = optarg;
break;
case 'b':
*bind_addr = optarg;
break;
case 'c':
*cache_bytes = optarg ? optarg : proxy_default_cache_size;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(proxy_help_text);
break;
}
}
struct proxier *proxy = NULL;
void my_exit(int signum)
{
info("Exit signalled (%i)", signum);
if (NULL != proxy) {
proxy_cleanup(proxy);
};
exit(0);
}
int main(int argc, char *argv[])
{
int c;
char *downstream_addr = NULL;
char *downstream_port = NULL;
char *upstream_addr = NULL;
char *upstream_port = NULL;
char *bind_addr = NULL;
char *cache_bytes = NULL;
int success;
sigset_t mask;
struct sigaction exit_action;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGINT);
exit_action.sa_handler = my_exit;
exit_action.sa_mask = mask;
exit_action.sa_flags = 0;
srand(time(NULL));
while (1) {
c = getopt_long(argc, argv, proxy_short_options, proxy_options,
NULL);
if (-1 == c) {
break;
}
read_proxy_param(c,
&downstream_addr,
&downstream_port,
&upstream_addr,
&upstream_port, &bind_addr, &cache_bytes);
}
if (NULL == downstream_addr) {
fprintf(stderr, "--addr is required.\n");
exit_err(proxy_help_text);
} else if (NULL == upstream_addr || NULL == upstream_port) {
fprintf(stderr,
"both --conn-addr and --conn-port are required.\n");
exit_err(proxy_help_text);
}
proxy = proxy_create(downstream_addr,
downstream_port,
upstream_addr,
upstream_port, bind_addr, cache_bytes);
/* Set these *after* proxy has been assigned to */
sigaction(SIGTERM, &exit_action, NULL);
sigaction(SIGQUIT, &exit_action, NULL);
sigaction(SIGINT, &exit_action, NULL);
signal(SIGPIPE, SIG_IGN); /* calls to splice() unhelpfully throw this */
if (NULL != downstream_port) {
info("Proxying between %s %s (downstream) and %s %s (upstream)",
downstream_addr, downstream_port, upstream_addr,
upstream_port);
} else {
info("Proxying between %s (downstream) and %s %s (upstream)",
downstream_addr, upstream_addr, upstream_port);
}
success = do_proxy(proxy);
proxy_destroy(proxy);
return success ? 0 : 1;
}

78
src/proxy/prefetch.c Normal file
View File

@@ -0,0 +1,78 @@
#include "prefetch.h"
#include "util.h"
struct prefetch *prefetch_create(size_t size_bytes)
{
struct prefetch *out = xmalloc(sizeof(struct prefetch));
NULLCHECK(out);
out->buffer = xmalloc(size_bytes);
NULLCHECK(out->buffer);
out->size = size_bytes;
out->is_full = 0;
out->from = 0;
out->len = 0;
return out;
}
void prefetch_destroy(struct prefetch *prefetch)
{
if (prefetch) {
free(prefetch->buffer);
free(prefetch);
}
}
size_t prefetch_size(struct prefetch *prefetch)
{
if (prefetch) {
return prefetch->size;
} else {
return 0;
}
}
void prefetch_set_is_empty(struct prefetch *prefetch)
{
prefetch_set_full(prefetch, 0);
}
void prefetch_set_is_full(struct prefetch *prefetch)
{
prefetch_set_full(prefetch, 1);
}
void prefetch_set_full(struct prefetch *prefetch, int val)
{
if (prefetch) {
prefetch->is_full = val;
}
}
int prefetch_is_full(struct prefetch *prefetch)
{
if (prefetch) {
return prefetch->is_full;
} else {
return 0;
}
}
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
uint32_t len)
{
NULLCHECK(prefetch);
return from >= prefetch->from &&
from + len <= prefetch->from + prefetch->len;
}
char *prefetch_offset(struct prefetch *prefetch, uint64_t from)
{
NULLCHECK(prefetch);
return prefetch->buffer + (from - prefetch->from);
}

34
src/proxy/prefetch.h Normal file
View File

@@ -0,0 +1,34 @@
#ifndef PREFETCH_H
#define PREFETCH_H
#include <stdint.h>
#include <stddef.h>
#define PREFETCH_BUFSIZE 4096
struct prefetch {
/* True if there is data in the buffer. */
int is_full;
/* The start point of the current content of buffer */
uint64_t from;
/* The length of the current content of buffer */
uint32_t len;
/* The total size of the buffer, in bytes. */
size_t size;
char *buffer;
};
struct prefetch *prefetch_create(size_t size_bytes);
void prefetch_destroy(struct prefetch *prefetch);
size_t prefetch_size(struct prefetch *);
void prefetch_set_is_empty(struct prefetch *prefetch);
void prefetch_set_is_full(struct prefetch *prefetch);
void prefetch_set_full(struct prefetch *prefetch, int val);
int prefetch_is_full(struct prefetch *prefetch);
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
uint32_t len);
char *prefetch_offset(struct prefetch *prefetch, uint64_t from);
#endif

988
src/proxy/proxy.c Normal file
View File

@@ -0,0 +1,988 @@
#include "proxy.h"
#include "readwrite.h"
#include "prefetch.h"
#include "ioutil.h"
#include "sockutil.h"
#include "util.h"
#include <errno.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
struct proxier *proxy_create(char *s_downstream_address,
char *s_downstream_port,
char *s_upstream_address,
char *s_upstream_port,
char *s_upstream_bind, char *s_cache_bytes)
{
struct proxier *out;
out = xmalloc(sizeof(struct proxier));
FATAL_IF_NULL(s_downstream_address, "Listen address not specified");
NULLCHECK(s_downstream_address);
FATAL_UNLESS(parse_to_sockaddr
(&out->listen_on.generic, s_downstream_address),
"Couldn't parse downstream address %s");
if (out->listen_on.family != AF_UNIX) {
FATAL_IF_NULL(s_downstream_port, "Downstream port not specified");
NULLCHECK(s_downstream_port);
parse_port(s_downstream_port, &out->listen_on.v4);
}
FATAL_IF_NULL(s_upstream_address, "Upstream address not specified");
NULLCHECK(s_upstream_address);
FATAL_UNLESS(parse_ip_to_sockaddr
(&out->connect_to.generic, s_upstream_address),
"Couldn't parse upstream address '%s'",
s_upstream_address);
FATAL_IF_NULL(s_upstream_port, "Upstream port not specified");
NULLCHECK(s_upstream_port);
parse_port(s_upstream_port, &out->connect_to.v4);
if (s_upstream_bind) {
FATAL_IF_ZERO(parse_ip_to_sockaddr
(&out->connect_from.generic, s_upstream_bind),
"Couldn't parse bind address '%s'", s_upstream_bind);
out->bind = 1;
}
out->listen_fd = -1;
out->downstream_fd = -1;
out->upstream_fd = -1;
out->prefetch = NULL;
if (s_cache_bytes) {
int cache_bytes = atoi(s_cache_bytes);
/* leaving this off or setting a cache size of zero or
* less results in no cache.
*/
if (cache_bytes >= 0) {
out->prefetch = prefetch_create(cache_bytes);
}
}
out->init.buf = xmalloc(sizeof(struct nbd_init_raw));
/* Add on the request / reply size to our malloc to accommodate both
* the struct and the data
*/
out->req.buf = xmalloc(NBD_MAX_SIZE + NBD_REQUEST_SIZE);
out->rsp.buf = xmalloc(NBD_MAX_SIZE + NBD_REPLY_SIZE);
log_context =
xmalloc(strlen(s_upstream_address) + strlen(s_upstream_port) + 2);
sprintf(log_context, "%s:%s", s_upstream_address, s_upstream_port);
return out;
}
int proxy_prefetches(struct proxier *proxy)
{
NULLCHECK(proxy);
return proxy->prefetch != NULL;
}
int proxy_prefetch_bufsize(struct proxier *proxy)
{
NULLCHECK(proxy);
return prefetch_size(proxy->prefetch);
}
void proxy_destroy(struct proxier *proxy)
{
free(proxy->init.buf);
free(proxy->req.buf);
free(proxy->rsp.buf);
prefetch_destroy(proxy->prefetch);
free(proxy);
}
/* Shared between our two different connect_to_upstream paths */
void proxy_finish_connect_to_upstream(struct proxier *proxy, uint64_t size,
uint32_t flags);
/* Try to establish a connection to our upstream server. Return 1 on success,
* 0 on failure. this is a blocking call that returns a non-blocking socket.
*/
int proxy_connect_to_upstream(struct proxier *proxy)
{
struct sockaddr *connect_from = NULL;
if (proxy->bind) {
connect_from = &proxy->connect_from.generic;
}
int fd = socket_connect(&proxy->connect_to.generic, connect_from);
uint64_t size = 0;
uint32_t flags = 0;
if (-1 == fd) {
return 0;
}
if (!socket_nbd_read_hello(fd, &size, &flags)) {
WARN_IF_NEGATIVE(sock_try_close(fd),
"Couldn't close() after failed read of NBD hello on fd %i",
fd);
return 0;
}
proxy->upstream_fd = fd;
sock_set_nonblock(fd, 1);
proxy_finish_connect_to_upstream(proxy, size, flags);
return 1;
}
/* First half of non-blocking connection to upstream. Gets as far as calling
* connect() on a non-blocking socket.
*/
void proxy_start_connect_to_upstream(struct proxier *proxy)
{
int fd, result;
struct sockaddr *from = NULL;
struct sockaddr *to = &proxy->connect_to.generic;
if (proxy->bind) {
from = &proxy->connect_from.generic;
}
fd = socket(to->sa_family, SOCK_STREAM, 0);
if (fd < 0) {
warn(SHOW_ERRNO
("Couldn't create socket to reconnect to upstream"));
return;
}
info("Beginning non-blocking connection to upstream on fd %i", fd);
if (NULL != from) {
if (0 > bind(fd, from, sockaddr_size(from))) {
warn(SHOW_ERRNO("bind() to source address failed"));
}
}
result = sock_set_nonblock(fd, 1);
if (result == -1) {
warn(SHOW_ERRNO("Failed to set upstream fd %i non-blocking", fd));
goto error;
}
result = connect(fd, to, sockaddr_size(to));
if (result == -1 && errno != EINPROGRESS) {
warn(SHOW_ERRNO("Failed to start connect()ing to upstream!"));
goto error;
}
proxy->upstream_fd = fd;
return;
error:
if (sock_try_close(fd) == -1) {
/* Non-fatal leak, although still nasty */
warn(SHOW_ERRNO("Failed to close fd for upstream %i", fd));
}
return;
}
void proxy_finish_connect_to_upstream(struct proxier *proxy, uint64_t size,
uint32_t flags)
{
if (proxy->upstream_size == 0) {
info("Size of upstream image is %" PRIu64 " bytes", size);
} else if (proxy->upstream_size != size) {
warn("Size changed from %" PRIu64 " to %" PRIu64 " bytes",
proxy->upstream_size, size);
}
proxy->upstream_size = size;
if (proxy->upstream_flags == 0) {
info("Upstream transmission flags set to %" PRIu32 "", flags);
} else if (proxy->upstream_flags != flags) {
warn("Upstream transmission flags changed from %" PRIu32 " to %"
PRIu32 "", proxy->upstream_flags, flags);
}
proxy->upstream_flags = flags;
if (AF_UNIX != proxy->connect_to.family) {
if (sock_set_tcp_nodelay(proxy->upstream_fd, 1) == -1) {
warn(SHOW_ERRNO("Failed to set TCP_NODELAY"));
}
}
info("Connected to upstream on fd %i", proxy->upstream_fd);
return;
}
void proxy_disconnect_from_upstream(struct proxier *proxy)
{
if (-1 != proxy->upstream_fd) {
info("Closing upstream connection on fd %i", proxy->upstream_fd);
/* TODO: An NBD disconnect would be pleasant here */
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
"Failed to close() fd %i when disconnecting from upstream",
proxy->upstream_fd);
proxy->upstream_fd = -1;
}
}
/** Prepares a listening socket for the NBD server, binding etc. */
void proxy_open_listen_socket(struct proxier *params)
{
NULLCHECK(params);
params->listen_fd = socket(params->listen_on.family, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(params->listen_fd,
SHOW_ERRNO("Couldn't create listen socket")
);
/* Allow us to restart quickly */
FATAL_IF_NEGATIVE(sock_set_reuseaddr(params->listen_fd, 1),
SHOW_ERRNO("Couldn't set SO_REUSEADDR")
);
if (AF_UNIX != params->listen_on.family) {
FATAL_IF_NEGATIVE(sock_set_tcp_nodelay(params->listen_fd, 1),
SHOW_ERRNO("Couldn't set TCP_NODELAY")
);
}
FATAL_UNLESS_ZERO(sock_try_bind
(params->listen_fd, &params->listen_on.generic),
SHOW_ERRNO("Failed to bind to listening socket")
);
/* We're only serving one client at a time, hence backlog of 1 */
FATAL_IF_NEGATIVE(listen(params->listen_fd, 1),
SHOW_ERRNO("Failed to listen on listening socket")
);
info("Now listening for incoming connections");
return;
}
typedef enum {
EXIT,
WRITE_TO_DOWNSTREAM,
READ_FROM_DOWNSTREAM,
CONNECT_TO_UPSTREAM,
READ_INIT_FROM_UPSTREAM,
WRITE_TO_UPSTREAM,
READ_FROM_UPSTREAM
} proxy_session_states;
static char *proxy_session_state_names[] = {
"EXIT",
"WRITE_TO_DOWNSTREAM",
"READ_FROM_DOWNSTREAM",
"CONNECT_TO_UPSTREAM",
"READ_INIT_FROM_UPSTREAM",
"WRITE_TO_UPSTREAM",
"READ_FROM_UPSTREAM"
};
static inline int proxy_state_upstream(int state)
{
return state == CONNECT_TO_UPSTREAM || state == READ_INIT_FROM_UPSTREAM
|| state == WRITE_TO_UPSTREAM || state == READ_FROM_UPSTREAM;
}
int proxy_prefetch_for_request(struct proxier *proxy, int state)
{
NULLCHECK(proxy);
struct nbd_request *req = &proxy->req_hdr;
struct nbd_reply *rsp = &proxy->rsp_hdr;
struct nbd_request_raw *req_raw =
(struct nbd_request_raw *) proxy->req.buf;
struct nbd_reply_raw *rsp_raw =
(struct nbd_reply_raw *) proxy->rsp.buf;
int is_read = req->type == REQUEST_READ;
if (is_read) {
/* See if we can respond with what's in our prefetch
* cache */
if (prefetch_is_full(proxy->prefetch) &&
prefetch_contains(proxy->prefetch, req->from, req->len)) {
/* HUZZAH! A match! */
debug("Prefetch hit!");
/* First build a reply header */
rsp->magic = REPLY_MAGIC;
rsp->error = 0;
memcpy(&rsp->handle, &req->handle, 8);
/* now copy it into the response */
nbd_h2r_reply(rsp, rsp_raw);
/* and the data */
memcpy(proxy->rsp.buf + NBD_REPLY_SIZE,
prefetch_offset(proxy->prefetch, req->from), req->len);
proxy->rsp.size = NBD_REPLY_SIZE + req->len;
proxy->rsp.needle = 0;
/* return early, our work here is done */
return WRITE_TO_DOWNSTREAM;
}
} else {
/* Safety catch. If we're sending a write request, we
* blow away the cache. This is very pessimistic, but
* it's simpler (and therefore safer) than working out
* whether we can keep it or not.
*/
debug("Blowing away prefetch cache on type %d request.",
req->type);
prefetch_set_is_empty(proxy->prefetch);
}
debug("Prefetch cache MISS!");
uint64_t prefetch_start = req->from;
/* We prefetch what we expect to be the next request. */
uint64_t prefetch_end = req->from + (req->len * 2);
/* We only want to consider prefetching if we know we're not
* getting too much data back, if it's a read request, and if
* the prefetch won't try to read past the end of the file.
*/
int prefetching =
req->len <= prefetch_size(proxy->prefetch) &&
is_read &&
prefetch_start < prefetch_end &&
prefetch_end <= proxy->upstream_size;
/* We pull the request out of the proxy struct, rewrite the
* request size, and write it back.
*/
if (prefetching) {
proxy->is_prefetch_req = 1;
proxy->prefetch_req_orig_len = req->len;
req->len *= 2;
debug("Prefetching additional %" PRIu32 " bytes",
req->len - proxy->prefetch_req_orig_len);
nbd_h2r_request(req, req_raw);
}
return state;
}
int proxy_prefetch_for_reply(struct proxier *proxy, int state)
{
size_t prefetched_bytes;
if (!proxy->is_prefetch_req) {
return state;
}
prefetched_bytes = proxy->req_hdr.len - proxy->prefetch_req_orig_len;
debug("Prefetched additional %d bytes", prefetched_bytes);
memcpy(proxy->prefetch->buffer,
proxy->rsp.buf + proxy->prefetch_req_orig_len + NBD_REPLY_SIZE,
prefetched_bytes);
proxy->prefetch->from =
proxy->req_hdr.from + proxy->prefetch_req_orig_len;
proxy->prefetch->len = prefetched_bytes;
/* We've finished with proxy->req by now, so don't need to alter it to make
* it look like the request was before prefetch */
/* Truncate the bytes we'll write downstream */
proxy->req_hdr.len = proxy->prefetch_req_orig_len;
proxy->rsp.size -= prefetched_bytes;
/* And we need to reset these */
prefetch_set_is_full(proxy->prefetch);
proxy->is_prefetch_req = 0;
return state;
}
int proxy_read_from_downstream(struct proxier *proxy, int state)
{
ssize_t count;
struct nbd_request_raw *request_raw =
(struct nbd_request_raw *) proxy->req.buf;
struct nbd_request *request = &(proxy->req_hdr);
// assert( state == READ_FROM_DOWNSTREAM );
count =
iobuf_read(proxy->downstream_fd, &proxy->req, NBD_REQUEST_SIZE);
if (count == -1) {
warn(SHOW_ERRNO("Couldn't read request from downstream"));
return EXIT;
}
if (proxy->req.needle == NBD_REQUEST_SIZE) {
nbd_r2h_request(request_raw, request);
if (request->type == REQUEST_DISCONNECT) {
info("Received disconnect request from client");
return EXIT;
}
/* Simple validations -- the request / reply size have already
* been taken into account in the xmalloc, so no need to worry
* about them here
*/
if (request->type == REQUEST_READ) {
if (request->len > NBD_MAX_SIZE) {
warn("NBD read request size %" PRIu32 " too large",
request->len);
return EXIT;
}
}
if (request->type == REQUEST_WRITE) {
if (request->len > NBD_MAX_SIZE) {
warn("NBD write request size %" PRIu32 " too large",
request->len);
return EXIT;
}
proxy->req.size += request->len;
}
}
if (proxy->req.needle == proxy->req.size) {
debug("Received NBD request from downstream. type=%" PRIu16
" flags=%" PRIu16 " from=%" PRIu64 " len=%" PRIu32,
request->type, request->flags, request->from, request->len);
/* Finished reading, so advance state. Leave size untouched so the next
* state knows how many bytes to write */
proxy->req.needle = 0;
return WRITE_TO_UPSTREAM;
}
return state;
}
int proxy_continue_connecting_to_upstream(struct proxier *proxy, int state)
{
int error, result;
socklen_t len = sizeof(error);
// assert( state == CONNECT_TO_UPSTREAM );
result =
getsockopt(proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (result == -1) {
warn(SHOW_ERRNO("Failed to tell if connected to upstream"));
return state;
}
if (error != 0) {
errno = error;
warn(SHOW_ERRNO("Failed to connect to upstream"));
return state;
}
/* Data may have changed while we were disconnected */
prefetch_set_is_empty(proxy->prefetch);
info("Connected to upstream on fd %i", proxy->upstream_fd);
return READ_INIT_FROM_UPSTREAM;
}
int proxy_read_init_from_upstream(struct proxier *proxy, int state)
{
ssize_t count;
// assert( state == READ_INIT_FROM_UPSTREAM );
count =
iobuf_read(proxy->upstream_fd, &proxy->init,
sizeof(struct nbd_init_raw));
if (count == -1) {
warn(SHOW_ERRNO("Failed to read init from upstream"));
goto disconnect;
}
if (proxy->init.needle == proxy->init.size) {
uint64_t upstream_size;
uint32_t upstream_flags;
if (!nbd_check_hello
((struct nbd_init_raw *) proxy->init.buf, &upstream_size,
&upstream_flags)) {
warn("Upstream sent invalid init");
goto disconnect;
}
/* record the flags, and log the reconnection, set TCP_NODELAY */
proxy_finish_connect_to_upstream(proxy, upstream_size,
upstream_flags);
/* Currently, we only get disconnected from upstream (so needing to come
* here) when we have an outstanding request. If that becomes false,
* we'll need to choose the right state to return to here */
proxy->init.needle = 0;
return WRITE_TO_UPSTREAM;
}
return state;
disconnect:
proxy->init.needle = 0;
proxy->init.size = 0;
return CONNECT_TO_UPSTREAM;
}
int proxy_write_to_upstream(struct proxier *proxy, int state)
{
ssize_t count;
// assert( state == WRITE_TO_UPSTREAM );
/* FIXME: We may set cork=1 multiple times as a result of this idiom.
* Not a serious problem, but we could do better
*/
if (proxy->req.needle == 0 && AF_UNIX != proxy->connect_to.family) {
if (sock_set_tcp_cork(proxy->upstream_fd, 1) == -1) {
warn(SHOW_ERRNO("Failed to set TCP_CORK"));
}
}
count = iobuf_write(proxy->upstream_fd, &proxy->req);
if (count == -1) {
warn(SHOW_ERRNO("Failed to send request to upstream"));
proxy->req.needle = 0;
// We're throwing the socket away so no need to uncork
return CONNECT_TO_UPSTREAM;
}
if (proxy->req.needle == proxy->req.size) {
/* Request sent. Advance to reading the response from upstream. We might
* still need req.size if reading the reply fails - we disconnect
* and resend the reply in that case - so keep it around for now. */
proxy->req.needle = 0;
if (AF_UNIX != proxy->connect_to.family) {
if (sock_set_tcp_cork(proxy->upstream_fd, 0) == -1) {
warn(SHOW_ERRNO("Failed to unset TCP_CORK"));
// TODO: should we return to CONNECT_TO_UPSTREAM in this instance?
}
}
return READ_FROM_UPSTREAM;
}
return state;
}
int proxy_read_from_upstream(struct proxier *proxy, int state)
{
ssize_t count;
struct nbd_reply *reply = &(proxy->rsp_hdr);
struct nbd_reply_raw *reply_raw =
(struct nbd_reply_raw *) proxy->rsp.buf;
/* We can't assume the NBD_REPLY_SIZE + req->len is what we'll get back */
count = iobuf_read(proxy->upstream_fd, &proxy->rsp, NBD_REPLY_SIZE);
if (count == -1) {
warn(SHOW_ERRNO("Failed to get reply from upstream"));
goto disconnect;
}
if (proxy->rsp.needle == NBD_REPLY_SIZE) {
nbd_r2h_reply(reply_raw, reply);
if (reply->magic != REPLY_MAGIC) {
warn("Reply magic is incorrect");
goto disconnect;
}
if (proxy->req_hdr.type == REQUEST_READ) {
/* Get the read reply data too. */
proxy->rsp.size += proxy->req_hdr.len;
}
}
if (proxy->rsp.size == proxy->rsp.needle) {
debug("NBD reply received from upstream.");
proxy->rsp.needle = 0;
return WRITE_TO_DOWNSTREAM;
}
return state;
disconnect:
proxy->rsp.needle = 0;
proxy->rsp.size = 0;
return CONNECT_TO_UPSTREAM;
}
int proxy_write_to_downstream(struct proxier *proxy, int state)
{
ssize_t count;
// assert( state == WRITE_TO_DOWNSTREAM );
if (!proxy->hello_sent) {
info("Writing init to downstream");
}
count = iobuf_write(proxy->downstream_fd, &proxy->rsp);
if (count == -1) {
warn(SHOW_ERRNO("Failed to write to downstream"));
return EXIT;
}
if (proxy->rsp.needle == proxy->rsp.size) {
if (!proxy->hello_sent) {
info("Hello message sent to client");
proxy->hello_sent = 1;
} else {
debug("Reply sent");
proxy->req_count++;
}
/* We're done with the request & response buffers now */
proxy->req.size = 0;
proxy->req.needle = 0;
proxy->rsp.size = 0;
proxy->rsp.needle = 0;
return READ_FROM_DOWNSTREAM;
}
return state;
}
/* Non-blocking proxy session. Simple(ish) state machine. We read from d/s until
* we have a full request, then try to write that request u/s. If writing fails,
* we reconnect to upstream and retry. Once we've successfully written, we
* attempt to read the reply. If that fails or times out (we give it 30 seconds)
* then we disconnect from u/s and go back to trying to reconnect and resend.
*
* This is the second-simplest NBD proxy I can think of. The first version was
* non-blocking I/O, but it was getting impossible to manage exceptional stuff
*/
void proxy_session(struct proxier *proxy)
{
uint64_t state_started = monotonic_time_ms();
int old_state = EXIT;
int state;
int connect_to_upstream_cooldown = 0;
/* First action: Write hello to downstream */
nbd_hello_to_buf((struct nbd_init_raw *) proxy->rsp.buf,
proxy->upstream_size, proxy->upstream_flags);
proxy->rsp.size = sizeof(struct nbd_init_raw);
proxy->rsp.needle = 0;
state = WRITE_TO_DOWNSTREAM;
info("Beginning proxy session on fd %i", proxy->downstream_fd);
while (state != EXIT) {
struct timeval select_timeout = {
.tv_sec = 0,
.tv_usec = 0
};
struct timeval *select_timeout_ptr = NULL;
int result; /* used by select() */
fd_set rfds;
fd_set wfds;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
if (state != old_state) {
state_started = monotonic_time_ms();
debug("State transition from %s to %s",
proxy_session_state_names[old_state],
proxy_session_state_names[state]
);
} else {
debug("Proxy is in state %s", proxy_session_state_names[state],
state);
}
old_state = state;
switch (state) {
case READ_FROM_DOWNSTREAM:
FD_SET(proxy->downstream_fd, &rfds);
break;
case WRITE_TO_DOWNSTREAM:
FD_SET(proxy->downstream_fd, &wfds);
break;
case WRITE_TO_UPSTREAM:
select_timeout.tv_sec = 15;
FD_SET(proxy->upstream_fd, &wfds);
break;
case CONNECT_TO_UPSTREAM:
/* upstream_fd is now -1 */
proxy_disconnect_from_upstream(proxy);
if (connect_to_upstream_cooldown) {
connect_to_upstream_cooldown = 0;
select_timeout.tv_sec = 3;
} else {
proxy_start_connect_to_upstream(proxy);
if (proxy->upstream_fd == -1) {
warn(SHOW_ERRNO("Error acquiring socket to upstream"));
continue;
}
FD_SET(proxy->upstream_fd, &wfds);
select_timeout.tv_sec = 15;
}
break;
case READ_INIT_FROM_UPSTREAM:
case READ_FROM_UPSTREAM:
select_timeout.tv_sec = 15;
FD_SET(proxy->upstream_fd, &rfds);
break;
};
if (select_timeout.tv_sec > 0) {
select_timeout_ptr = &select_timeout;
}
result =
sock_try_select(FD_SETSIZE, &rfds, &wfds, NULL,
select_timeout_ptr);
if (result == -1) {
warn(SHOW_ERRNO("select() failed: "));
break;
}
/* Happens after failed reconnect. Avoid SIGBUS on FD_ISSET() */
if (proxy->upstream_fd == -1) {
continue;
}
switch (state) {
case READ_FROM_DOWNSTREAM:
if (FD_ISSET(proxy->downstream_fd, &rfds)) {
state = proxy_read_from_downstream(proxy, state);
/* Check if we can fulfil the request from prefetch, or
* rewrite the request to fill the prefetch buffer if needed
*/
if (proxy_prefetches(proxy) && state == WRITE_TO_UPSTREAM) {
state = proxy_prefetch_for_request(proxy, state);
}
}
break;
case CONNECT_TO_UPSTREAM:
if (FD_ISSET(proxy->upstream_fd, &wfds)) {
state =
proxy_continue_connecting_to_upstream(proxy, state);
}
/* Leaving state untouched will retry connecting to upstream -
* so introduce a bit of sleep */
if (state == CONNECT_TO_UPSTREAM) {
connect_to_upstream_cooldown = 1;
}
break;
case READ_INIT_FROM_UPSTREAM:
state = proxy_read_init_from_upstream(proxy, state);
if (state == CONNECT_TO_UPSTREAM) {
connect_to_upstream_cooldown = 1;
}
break;
case WRITE_TO_UPSTREAM:
if (FD_ISSET(proxy->upstream_fd, &wfds)) {
state = proxy_write_to_upstream(proxy, state);
}
break;
case READ_FROM_UPSTREAM:
if (FD_ISSET(proxy->upstream_fd, &rfds)) {
state = proxy_read_from_upstream(proxy, state);
}
/* Fill the prefetch buffer and rewrite the reply, if needed */
if (proxy_prefetches(proxy) && state == WRITE_TO_DOWNSTREAM) {
state = proxy_prefetch_for_reply(proxy, state);
}
break;
case WRITE_TO_DOWNSTREAM:
if (FD_ISSET(proxy->downstream_fd, &wfds)) {
state = proxy_write_to_downstream(proxy, state);
}
break;
}
/* In these states, we're interested in restarting after a timeout.
*/
if (old_state == state && proxy_state_upstream(state)) {
if ((monotonic_time_ms()) - state_started > UPSTREAM_TIMEOUT) {
warn("Timed out in state %s while communicating with upstream", proxy_session_state_names[state]
);
state = CONNECT_TO_UPSTREAM;
/* Since we've timed out, we won't have gone through the timeout logic
* in the various state handlers that resets these appropriately... */
proxy->init.size = 0;
proxy->init.needle = 0;
proxy->rsp.size = 0;
proxy->rsp.needle = 0;
}
}
}
info("Finished proxy session on fd %i after %" PRIu64
" successful request(s)", proxy->downstream_fd, proxy->req_count);
/* Reset these two for the next session */
proxy->req_count = 0;
proxy->hello_sent = 0;
return;
}
/** Accept an NBD socket connection, dispatch appropriately */
int proxy_accept(struct proxier *params)
{
NULLCHECK(params);
int client_fd;
fd_set fds;
union mysockaddr client_address;
socklen_t socklen = sizeof(client_address);
info("Waiting for client connection");
FD_ZERO(&fds);
FD_SET(params->listen_fd, &fds);
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
SHOW_ERRNO("select() failed")
);
if (FD_ISSET(params->listen_fd, &fds)) {
client_fd =
accept(params->listen_fd, &client_address.generic, &socklen);
if (client_address.family != AF_UNIX) {
if (sock_set_tcp_nodelay(client_fd, 1) == -1) {
warn(SHOW_ERRNO("Failed to set TCP_NODELAY"));
}
}
info("Accepted nbd client socket fd %d", client_fd);
sock_set_nonblock(client_fd, 1);
params->downstream_fd = client_fd;
proxy_session(params);
WARN_IF_NEGATIVE(sock_try_close(params->downstream_fd),
"Couldn't close() downstram fd %i after proxy session",
params->downstream_fd);
params->downstream_fd = -1;
}
return 1; /* We actually expect to be interrupted by signal handlers */
}
void proxy_accept_loop(struct proxier *params)
{
NULLCHECK(params);
while (proxy_accept(params));
}
/** Closes sockets */
void proxy_cleanup(struct proxier *proxy)
{
NULLCHECK(proxy);
info("Cleaning up");
if (-1 != proxy->listen_fd) {
if (AF_UNIX == proxy->listen_on.family) {
if (-1 == unlink(proxy->listen_on.un.sun_path)) {
warn(SHOW_ERRNO
("Failed to unlink %s",
proxy->listen_on.un.sun_path));
}
}
WARN_IF_NEGATIVE(sock_try_close(proxy->listen_fd),
SHOW_ERRNO("Failed to close() listen fd %i",
proxy->listen_fd)
);
proxy->listen_fd = -1;
}
if (-1 != proxy->downstream_fd) {
WARN_IF_NEGATIVE(sock_try_close(proxy->downstream_fd),
SHOW_ERRNO("Failed to close() downstream fd %i",
proxy->downstream_fd)
);
proxy->downstream_fd = -1;
}
if (-1 != proxy->upstream_fd) {
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
SHOW_ERRNO("Failed to close() upstream fd %i",
proxy->upstream_fd)
);
proxy->upstream_fd = -1;
}
info("Cleanup done");
}
/** Full lifecycle of the proxier */
int do_proxy(struct proxier *params)
{
NULLCHECK(params);
info("Ensuring upstream server is open");
if (!proxy_connect_to_upstream(params)) {
warn("Couldn't connect to upstream server during initialization, exiting");
proxy_cleanup(params);
return 1;
};
proxy_open_listen_socket(params);
proxy_accept_loop(params);
proxy_cleanup(params);
return 0;
}

97
src/proxy/proxy.h Normal file
View File

@@ -0,0 +1,97 @@
#ifndef PROXY_H
#define PROXY_H
#include <sys/types.h>
#include <unistd.h>
#include "ioutil.h"
#include "parse.h"
#include "nbdtypes.h"
#include "self_pipe.h"
#ifdef PREFETCH
#include "prefetch.h"
#endif
/** UPSTREAM_TIMEOUT
* How long ( in ms ) to allow for upstream to respond. If it takes longer
* than this, we will cancel the current request-response to them and resubmit
*/
#define UPSTREAM_TIMEOUT 30 * 1000
struct proxier {
/** address/port to bind to */
union mysockaddr listen_on;
/** address/port to connect to */
union mysockaddr connect_to;
/** address to bind to when making outgoing connections */
union mysockaddr connect_from;
int bind; /* Set to true if we should use it */
/* The socket we listen() on and accept() against */
int listen_fd;
/* The socket returned by accept() that we receive requests from and send
* responses to
*/
int downstream_fd;
/* The socket returned by connect() that we send requests to and receive
* responses from
*/
int upstream_fd;
/* This is the size we advertise to the downstream server */
uint64_t upstream_size;
/* These are the transmission flags sent as part of the handshake */
uint32_t upstream_flags;
/* We transform the raw request header into here */
struct nbd_request req_hdr;
/* We transform the raw reply header into here */
struct nbd_reply rsp_hdr;
/* Used for our non-blocking negotiation with upstream. TODO: maybe use
* for downstream as well ( we currently overload rsp ) */
struct iobuf init;
/* The current NBD request from downstream */
struct iobuf req;
/* The current NBD reply from upstream */
struct iobuf rsp;
/* It's starting to feel like we need an object for a single proxy session.
* These two track how many requests we've sent so far, and whether the
* NBD_INIT code has been sent to the client yet.
*/
uint64_t req_count;
int hello_sent;
/** These are only used if we pass --cache on the command line */
/* While the in-flight request has been munged by prefetch, these two are
* set to true, and the original length of the request, respectively */
int is_prefetch_req;
uint32_t prefetch_req_orig_len;
/* And here, we actually store the prefetched data once it's returned */
struct prefetch *prefetch;
/** */
};
struct proxier *proxy_create(char *s_downstream_address,
char *s_downstream_port,
char *s_upstream_address,
char *s_upstream_port,
char *s_upstream_bind, char *s_cache_bytes);
int do_proxy(struct proxier *proxy);
void proxy_cleanup(struct proxier *proxy);
void proxy_destroy(struct proxier *proxy);
#endif

109
src/server/acl.c Normal file
View File

@@ -0,0 +1,109 @@
#include <stdlib.h>
#include "util.h"
#include "parse.h"
#include "acl.h"
struct acl *acl_create(int len, char **lines, int default_deny)
{
struct acl *acl;
acl = (struct acl *) xmalloc(sizeof(struct acl));
acl->len = parse_acl(&acl->entries, len, lines);
acl->default_deny = default_deny;
return acl;
}
static int testmasks[9] = { 0, 128, 192, 224, 240, 248, 252, 254, 255 };
/** Test whether AF_INET or AF_INET6 sockaddr is included in the given access
* control list, returning 1 if it is, and 0 if not.
*/
static int is_included_in_acl(int list_length,
struct ip_and_mask (*list)[],
union mysockaddr *test)
{
NULLCHECK(test);
int i;
for (i = 0; i < list_length; i++) {
struct ip_and_mask *entry = &(*list)[i];
int testbits;
unsigned char *raw_address1 = NULL, *raw_address2 = NULL;
debug("checking acl entry %d (%d/%d)", i, test->generic.sa_family,
entry->ip.family);
if (test->generic.sa_family != entry->ip.family) {
continue;
}
if (test->generic.sa_family == AF_INET) {
debug("it's an AF_INET");
raw_address1 = (unsigned char *) &test->v4.sin_addr;
raw_address2 = (unsigned char *) &entry->ip.v4.sin_addr;
} else if (test->generic.sa_family == AF_INET6) {
debug("it's an AF_INET6");
raw_address1 = (unsigned char *) &test->v6.sin6_addr;
raw_address2 = (unsigned char *) &entry->ip.v6.sin6_addr;
} else {
fatal("Can't check an ACL for this address type.");
}
debug("testbits=%d", entry->mask);
for (testbits = entry->mask; testbits > 0; testbits -= 8) {
debug("testbits=%d, c1=%02x, c2=%02x", testbits,
raw_address1[0], raw_address2[0]);
if (testbits >= 8) {
if (raw_address1[0] != raw_address2[0]) {
goto no_match;
}
} else {
if ((raw_address1[0] & testmasks[testbits % 8]) !=
(raw_address2[0] & testmasks[testbits % 8])) {
goto no_match;
}
}
raw_address1++;
raw_address2++;
}
return 1;
no_match:;
debug("no match");
}
return 0;
}
int acl_includes(struct acl *acl, union mysockaddr *addr)
{
NULLCHECK(acl);
if (0 == acl->len) {
return !(acl->default_deny);
} else {
return is_included_in_acl(acl->len, acl->entries, addr);
}
}
int acl_default_deny(struct acl *acl)
{
NULLCHECK(acl);
return acl->default_deny;
}
void acl_destroy(struct acl *acl)
{
free(acl->entries);
acl->len = 0;
acl->entries = NULL;
free(acl);
}

37
src/server/acl.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef ACL_H
#define ACL_H
#include "parse.h"
struct acl {
int len;
int default_deny;
struct ip_and_mask (*entries)[];
};
/** Allocate a new acl structure, parsing the given lines to sockaddr
* structures in the process. After allocation, acl->len might not
* equal len. In that case, there was an error in parsing and acl->len
* will be the index of the failed entry in lines.
*
* default_deny controls the behaviour of an empty list: if true, all
* requests will be denied. If true, all requests will be accepted.
*/
struct acl *acl_create(int len, char **lines, int default_deny);
/** Check to see whether an address is allowed by an acl.
* See acl_create for how the default_deny setting affects this.
*/
int acl_includes(struct acl *, union mysockaddr *);
/** Get the default_deny status */
int acl_default_deny(struct acl *);
/** Free the acl structure and the internal acl entries table.
*/
void acl_destroy(struct acl *);
#endif

441
src/server/bitset.h Normal file
View File

@@ -0,0 +1,441 @@
#ifndef BITSET_H
#define BITSET_H
#include "util.h"
#include <inttypes.h>
#include <string.h>
#include <pthread.h>
/*
* Make the bitfield words 'opaque' to prevent code
* poking at the bits directly without using these
* accessors/macros
*/
typedef uint64_t bitfield_word_t;
typedef bitfield_word_t *bitfield_p;
#define BITFIELD_WORD_SIZE sizeof(bitfield_word_t)
#define BITS_PER_WORD (BITFIELD_WORD_SIZE * 8)
#define BIT_MASK(_idx) \
(1LL << ((_idx) & (BITS_PER_WORD - 1)))
#define BIT_WORD(_b, _idx) \
((bitfield_word_t*)(_b))[(_idx) / BITS_PER_WORD]
/* Calculates the number of words needed to store _bytes number of bytes
* this is added to accommodate code that wants to use bytes sizes
*/
#define BIT_WORDS_FOR_SIZE(_bytes) \
((_bytes + (BITFIELD_WORD_SIZE-1)) / BITFIELD_WORD_SIZE)
/** Return the bit value ''idx'' in array ''b'' */
static inline int bit_get(bitfield_p b, uint64_t idx)
{
return (BIT_WORD(b, idx) >> (idx & (BITS_PER_WORD - 1))) & 1;
}
/** Return 1 if the bit at ''idx'' in array ''b'' is set */
static inline int bit_is_set(bitfield_p b, uint64_t idx)
{
return bit_get(b, idx);
}
/** Return 1 if the bit at ''idx'' in array ''b'' is clear */
static inline int bit_is_clear(bitfield_p b, uint64_t idx)
{
return !bit_get(b, idx);
}
/** Tests whether the bit at ''idx'' in array ''b'' has value ''value'' */
static inline int bit_has_value(bitfield_p b, uint64_t idx, int value)
{
return bit_get(b, idx) == ! !value;
}
/** Sets the bit ''idx'' in array ''b'' */
static inline void bit_set(bitfield_p b, uint64_t idx)
{
BIT_WORD(b, idx) |= BIT_MASK(idx);
}
/** Clears the bit ''idx'' in array ''b'' */
static inline void bit_clear(bitfield_p b, uint64_t idx)
{
BIT_WORD(b, idx) &= ~BIT_MASK(idx);
}
/** Sets ''len'' bits in array ''b'' starting at offset ''from'' */
static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t len)
{
for (; (from % BITS_PER_WORD) != 0 && len > 0; len--) {
bit_set(b, from++);
}
if (len >= BITS_PER_WORD) {
memset(&BIT_WORD(b, from), 0xff, len / 8);
from += len;
len = len % BITS_PER_WORD;
from -= len;
}
for (; len > 0; len--) {
bit_set(b, from++);
}
}
/** Clears ''len'' bits in array ''b'' starting at offset ''from'' */
static inline void bit_clear_range(bitfield_p b, uint64_t from,
uint64_t len)
{
for (; (from % BITS_PER_WORD) != 0 && len > 0; len--) {
bit_clear(b, from++);
}
if (len >= BITS_PER_WORD) {
memset(&BIT_WORD(b, from), 0, len / 8);
from += len;
len = len % BITS_PER_WORD;
from -= len;
}
for (; len > 0; len--) {
bit_clear(b, from++);
}
}
/** Counts the number of contiguous bits in array ''b'', starting at ''from''
* up to a maximum number of bits ''len''. Returns the number of contiguous
* bits that are the same as the first one specified. If ''run_is_set'' is
* non-NULL, the value of that bit is placed into it.
*/
static inline uint64_t bit_run_count(bitfield_p b, uint64_t from,
uint64_t len, int *run_is_set)
{
uint64_t count = 0;
int first_value = bit_get(b, from);
bitfield_word_t word_match = first_value ? -1 : 0;
if (run_is_set != NULL) {
*run_is_set = first_value;
}
for (; ((from + count) % BITS_PER_WORD) != 0 && len > 0; len--) {
if (bit_has_value(b, from + count, first_value)) {
count++;
} else {
return count;
}
}
for (; len >= BITS_PER_WORD; len -= BITS_PER_WORD) {
if (BIT_WORD(b, from + count) == word_match) {
count += BITS_PER_WORD;
} else {
break;
}
}
for (; len > 0; len--) {
if (bit_has_value(b, from + count, first_value)) {
count++;
}
}
return count;
}
enum bitset_stream_events {
BITSET_STREAM_UNSET = 0,
BITSET_STREAM_SET = 1,
BITSET_STREAM_ON = 2,
BITSET_STREAM_OFF = 3
};
#define BITSET_STREAM_EVENTS_ENUM_SIZE 4
struct bitset_stream_entry {
enum bitset_stream_events event;
uint64_t from;
uint64_t len;
};
/** Limit the stream size to 1MB for now.
*
* If this is too small, it'll cause requests to stall as the migration lags
* behind the changes made by those requests.
*/
#define BITSET_STREAM_SIZE ( ( 1024 * 1024 ) / sizeof( struct bitset_stream_entry ) )
struct bitset_stream {
struct bitset_stream_entry entries[BITSET_STREAM_SIZE];
int in;
int out;
int size;
pthread_mutex_t mutex;
pthread_cond_t cond_not_full;
pthread_cond_t cond_not_empty;
uint64_t queued_bytes[BITSET_STREAM_EVENTS_ENUM_SIZE];
};
/** An application of a bitset - a bitset mapping represents a file of ''size''
* broken down into ''resolution''-sized chunks. The bit set is assumed to
* represent one bit per chunk. We also bundle a lock so that the set can be
* written reliably by multiple threads.
*/
struct bitset {
pthread_mutex_t lock;
uint64_t size;
int resolution;
struct bitset_stream *stream;
int stream_enabled;
bitfield_word_t bits[];
};
/** Allocate a bitset for a file of the given size, and chunks of the
* given resolution.
*/
static inline struct bitset *bitset_alloc(uint64_t size, int resolution)
{
// calculate a size to allocate that is a multiple of the size of the
// bitfield word
size_t bitfield_size =
BIT_WORDS_FOR_SIZE(((size + resolution -
1) / resolution)) * sizeof(bitfield_word_t);
struct bitset *bitset =
xmalloc(sizeof(struct bitset) + (bitfield_size / 8));
bitset->size = size;
bitset->resolution = resolution;
/* don't actually need to call pthread_mutex_destroy ' */
pthread_mutex_init(&bitset->lock, NULL);
bitset->stream = xmalloc(sizeof(struct bitset_stream));
pthread_mutex_init(&bitset->stream->mutex, NULL);
/* Technically don't need to call pthread_cond_destroy either */
pthread_cond_init(&bitset->stream->cond_not_full, NULL);
pthread_cond_init(&bitset->stream->cond_not_empty, NULL);
return bitset;
}
static inline void bitset_free(struct bitset *set)
{
/* TODO: free our mutex... */
free(set->stream);
set->stream = NULL;
free(set);
}
#define INT_FIRST_AND_LAST \
uint64_t first = from/set->resolution, \
last = ((from+len)-1)/set->resolution, \
bitlen = (last-first)+1
#define BITSET_LOCK \
FATAL_IF_NEGATIVE(pthread_mutex_lock(&set->lock), "Error locking bitset")
#define BITSET_UNLOCK \
FATAL_IF_NEGATIVE(pthread_mutex_unlock(&set->lock), "Error unlocking bitset")
static inline void bitset_stream_enqueue(struct bitset *set,
enum bitset_stream_events event,
uint64_t from, uint64_t len)
{
struct bitset_stream *stream = set->stream;
pthread_mutex_lock(&stream->mutex);
while (stream->size == BITSET_STREAM_SIZE) {
pthread_cond_wait(&stream->cond_not_full, &stream->mutex);
}
stream->entries[stream->in].event = event;
stream->entries[stream->in].from = from;
stream->entries[stream->in].len = len;
stream->queued_bytes[event] += len;
stream->size++;
stream->in++;
stream->in %= BITSET_STREAM_SIZE;
pthread_mutex_unlock(&stream->mutex);
pthread_cond_signal(&stream->cond_not_empty);
return;
}
static inline void bitset_stream_dequeue(struct bitset *set,
struct bitset_stream_entry *out)
{
struct bitset_stream *stream = set->stream;
struct bitset_stream_entry *dequeued;
pthread_mutex_lock(&stream->mutex);
while (stream->size == 0) {
pthread_cond_wait(&stream->cond_not_empty, &stream->mutex);
}
dequeued = &stream->entries[stream->out];
if (out != NULL) {
out->event = dequeued->event;
out->from = dequeued->from;
out->len = dequeued->len;
}
stream->queued_bytes[dequeued->event] -= dequeued->len;
stream->size--;
stream->out++;
stream->out %= BITSET_STREAM_SIZE;
pthread_mutex_unlock(&stream->mutex);
pthread_cond_signal(&stream->cond_not_full);
return;
}
static inline size_t bitset_stream_size(struct bitset *set)
{
size_t size;
pthread_mutex_lock(&set->stream->mutex);
size = set->stream->size;
pthread_mutex_unlock(&set->stream->mutex);
return size;
}
static inline uint64_t bitset_stream_queued_bytes(struct bitset *set,
enum bitset_stream_events
event)
{
uint64_t total;
pthread_mutex_lock(&set->stream->mutex);
total = set->stream->queued_bytes[event];
pthread_mutex_unlock(&set->stream->mutex);
return total;
}
static inline void bitset_enable_stream(struct bitset *set)
{
BITSET_LOCK;
set->stream_enabled = 1;
bitset_stream_enqueue(set, BITSET_STREAM_ON, 0, set->size);
BITSET_UNLOCK;
}
static inline void bitset_disable_stream(struct bitset *set)
{
BITSET_LOCK;
bitset_stream_enqueue(set, BITSET_STREAM_OFF, 0, set->size);
set->stream_enabled = 0;
BITSET_UNLOCK;
}
/** Set the bits in a bitset which correspond to the given bytes in the larger
* file.
*/
static inline void bitset_set_range(struct bitset *set,
uint64_t from, uint64_t len)
{
INT_FIRST_AND_LAST;
BITSET_LOCK;
bit_set_range(set->bits, first, bitlen);
if (set->stream_enabled) {
bitset_stream_enqueue(set, BITSET_STREAM_SET, from, len);
}
BITSET_UNLOCK;
}
/** Set every bit in the bitset. */
static inline void bitset_set(struct bitset *set)
{
bitset_set_range(set, 0, set->size);
}
/** Clear the bits in a bitset which correspond to the given bytes in the
* larger file.
*/
static inline void bitset_clear_range(struct bitset *set,
uint64_t from, uint64_t len)
{
INT_FIRST_AND_LAST;
BITSET_LOCK;
bit_clear_range(set->bits, first, bitlen);
if (set->stream_enabled) {
bitset_stream_enqueue(set, BITSET_STREAM_UNSET, from, len);
}
BITSET_UNLOCK;
}
/** Clear every bit in the bitset. */
static inline void bitset_clear(struct bitset *set)
{
bitset_clear_range(set, 0, set->size);
}
/** As per bitset_run_count but also tells you whether the run it found was set
* or unset, atomically.
*/
static inline uint64_t bitset_run_count_ex(struct bitset *set,
uint64_t from,
uint64_t len, int *run_is_set)
{
uint64_t run;
/* Clip our requests to the end of the bitset, avoiding uint underflow. */
if (from > set->size) {
return 0;
}
len = (len + from) > set->size ? (set->size - from) : len;
INT_FIRST_AND_LAST;
BITSET_LOCK;
run =
bit_run_count(set->bits, first, bitlen,
run_is_set) * set->resolution;
run -= (from % set->resolution);
BITSET_UNLOCK;
return run;
}
/** Counts the number of contiguous bytes that are represented as a run in
* the bit field.
*/
static inline uint64_t bitset_run_count(struct bitset *set,
uint64_t from, uint64_t len)
{
return bitset_run_count_ex(set, from, len, NULL);
}
/** Tests whether the bit field is clear for the given file offset.
*/
static inline int bitset_is_clear_at(struct bitset *set, uint64_t at)
{
return bit_is_clear(set->bits, at / set->resolution);
}
/** Tests whether the bit field is set for the given file offset.
*/
static inline int bitset_is_set_at(struct bitset *set, uint64_t at)
{
return bit_is_set(set->bits, at / set->resolution);
}
#endif

726
src/server/client.c Normal file
View File

@@ -0,0 +1,726 @@
#include "client.h"
#include "serve.h"
#include "ioutil.h"
#include "sockutil.h"
#include "util.h"
#include "bitset.h"
#include "nbdtypes.h"
#include "self_pipe.h"
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// When this signal is invoked, we call shutdown() on the client fd, which
// results in the thread being wound up
void client_killswitch_hit(int signal
__attribute__ ((unused)), siginfo_t * info,
void *ptr __attribute__ ((unused)))
{
int fd = info->si_value.sival_int;
warn("Killswitch for fd %i activated, calling shutdown on socket", fd);
FATAL_IF(-1 == shutdown(fd, SHUT_RDWR),
SHOW_ERRNO
("Failed to shutdown() the socket, killing the server")
);
}
struct client *client_create(struct server *serve, int socket)
{
NULLCHECK(serve);
struct client *c;
struct sigevent evp = {
.sigev_notify = SIGEV_SIGNAL,
.sigev_signo = CLIENT_KILLSWITCH_SIGNAL
};
/*
* Our killswitch closes this socket, forcing read() and write() calls
* blocked on it to return with an error. The thread then close()s the
* socket itself, avoiding races.
*/
evp.sigev_value.sival_int = socket;
c = xmalloc(sizeof(struct client));
c->stopped = 0;
c->socket = socket;
c->serve = serve;
c->stop_signal = self_pipe_create();
FATAL_IF_NEGATIVE(timer_create
(CLOCK_MONOTONIC, &evp, &(c->killswitch)),
SHOW_ERRNO("Failed to create killswitch timer")
);
debug("Alloced client %p with socket %d", c, socket);
return c;
}
void client_signal_stop(struct client *c)
{
NULLCHECK(c);
debug("client %p: signal stop (%d, %d)", c, c->stop_signal->read_fd,
c->stop_signal->write_fd);
self_pipe_signal(c->stop_signal);
}
void client_destroy(struct client *client)
{
NULLCHECK(client);
FATAL_IF_NEGATIVE(timer_delete(client->killswitch),
SHOW_ERRNO("Couldn't delete killswitch")
);
debug("Destroying stop signal for client %p", client);
self_pipe_destroy(client->stop_signal);
debug("Freeing client %p", client);
free(client);
}
/**
* So waiting on client->socket is len bytes of data, and we must write it all
* to client->mapped. However while doing do we must consult the bitmap
* client->serve->allocation_map, which is a bitmap where one bit represents
* block_allocation_resolution bytes. Where a bit isn't set, there are no
* disc blocks allocated for that portion of the file, and we'd like to keep
* it that way.
*
* If the bitmap shows that every block in our prospective write is already
* allocated, we can proceed as normal and make one call to writeloop.
*
*/
void write_not_zeroes(struct client *client, uint64_t from, uint64_t len)
{
NULLCHECK(client);
NULLCHECK(client->serve);
NULLCHECK(client->serve->allocation_map);
struct bitset *map = client->serve->allocation_map;
while (len > 0) {
/* so we have to calculate how much of our input to consider
* next based on the bitmap of allocated blocks. This will be
* at a coarser resolution than the actual write, which may
* not fall on a block boundary at either end. So we look up
* how many blocks our write covers, then cut off the start
* and end to get the exact number of bytes.
*/
uint64_t run = bitset_run_count(map, from, len);
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len,
run);
if (run > len) {
run = len;
debug("(run adjusted to %d)", run);
}
/*
// Useful but expensive
if (0)
{
uint64_t i;
fprintf(stderr, "full map resolution=%d: ", map->resolution);
for (i=0; i<client->serve->size; i+=map->resolution) {
int here = (from >= i && from < i+map->resolution);
if (here) { fprintf(stderr, ">"); }
fprintf(stderr, bitset_is_set_at(map, i) ? "1" : "0");
if (here) { fprintf(stderr, "<"); }
}
fprintf(stderr, "\n");
}
*/
#define DO_READ(dst, len) ERROR_IF_NEGATIVE( \
readloop( \
client->socket, \
(dst), \
(len) \
), \
SHOW_ERRNO("read failed %ld+%d", from, (len)) \
)
if (bitset_is_set_at(map, from)) {
debug("writing the lot: from=%ld, run=%d", from, run);
/* already allocated, just write it all */
DO_READ(client->mapped + from, run);
/* We know from our earlier call to bitset_run_count that the
* bitset is all-1s at this point, but we need to dirty it for the
* sake of the event stream - the actual bytes have changed, and we
* are interested in that fact.
*/
bitset_set_range(map, from, run);
len -= run;
from += run;
} else {
char zerobuffer[block_allocation_resolution];
/* not allocated, read in block_allocation_resoution */
while (run > 0) {
uint64_t blockrun = block_allocation_resolution -
(from % block_allocation_resolution);
if (blockrun > run)
blockrun = run;
DO_READ(zerobuffer, blockrun);
/* This reads the buffer twice in the worst case
* but we're leaning on memcmp failing early
* and memcpy being fast, rather than try to
* hand-optimized something specific.
*/
int all_zeros = (zerobuffer[0] == 0) &&
(0 ==
memcmp(zerobuffer, zerobuffer + 1, blockrun - 1));
if (!all_zeros) {
memcpy(client->mapped + from, zerobuffer, blockrun);
bitset_set_range(map, from, blockrun);
/* at this point we could choose to
* short-cut the rest of the write for
* faster I/O but by continuing to do it
* the slow way we preserve as much
* sparseness as possible.
*/
}
/* When the block is all_zeroes, no bytes have changed, so we
* don't need to put an event into the bitset stream. This may
* be surprising in the future.
*/
len -= blockrun;
run -= blockrun;
from += blockrun;
}
}
}
}
int fd_read_request(int fd, struct nbd_request_raw *out_request)
{
return readloop(fd, out_request, sizeof(struct nbd_request_raw));
}
/* Returns 1 if *request was filled with a valid request which we should
* try to honour. 0 otherwise. */
int client_read_request(struct client *client,
struct nbd_request *out_request, int *disconnected)
{
NULLCHECK(client);
NULLCHECK(out_request);
struct nbd_request_raw request_raw;
if (fd_read_request(client->socket, &request_raw) == -1) {
*disconnected = 1;
switch (errno) {
case 0:
warn(SHOW_ERRNO("EOF while reading request"));
return 0;
case ECONNRESET:
warn(SHOW_ERRNO("Connection reset while reading request"));
return 0;
case ETIMEDOUT:
warn(SHOW_ERRNO("Connection timed out while reading request"));
return 0;
default:
/* FIXME: I've seen this happen, but I
* couldn't reproduce it so I'm leaving
* it here with a better debug output in
* the hope it'll spontaneously happen
* again. It should *probably* be an
* error() call, but I want to be sure.
* */
fatal(SHOW_ERRNO("Error reading request"));
}
}
nbd_r2h_request(&request_raw, out_request);
return 1;
}
int fd_write_reply(int fd, uint64_t handle, int error)
{
struct nbd_reply reply;
struct nbd_reply_raw reply_raw;
reply.magic = REPLY_MAGIC;
reply.error = error;
reply.handle.w = handle;
nbd_h2r_reply(&reply, &reply_raw);
debug("Replying with handle=0x%08X, error=%" PRIu32, handle, error);
if (-1 == writeloop(fd, &reply_raw, sizeof(reply_raw))) {
switch (errno) {
case ECONNRESET:
error(SHOW_ERRNO("Connection reset while writing reply"));
break;
case EBADF:
fatal(SHOW_ERRNO
("Tried to write to an invalid file descriptor"));
break;
case EPIPE:
error(SHOW_ERRNO("Remote end closed"));
break;
default:
fatal(SHOW_ERRNO("Unhandled error while writing"));
}
}
return 1;
}
/* Writes a reply to request *request, with error, to the client's
* socket.
* Returns 1; we don't check for errors on the write.
* TODO: Check for errors on the write.
*/
int client_write_reply(struct client *client, struct nbd_request *request,
int error)
{
return fd_write_reply(client->socket, request->handle.w, error);
}
void client_write_init(struct client *client, uint64_t size)
{
struct nbd_init init = { {0} };
struct nbd_init_raw init_raw = { {0} };
memcpy(init.passwd, INIT_PASSWD, sizeof(init.passwd));
init.magic = INIT_MAGIC;
init.size = size;
/* As more features are implemented, this is the place to advertise
* them.
*/
init.flags = FLAG_HAS_FLAGS | FLAG_SEND_FLUSH | FLAG_SEND_FUA;
memset(init.reserved, 0, 124);
nbd_h2r_init(&init, &init_raw);
ERROR_IF_NEGATIVE(writeloop
(client->socket, &init_raw, sizeof(init_raw)),
SHOW_ERRNO("Couldn't send hello"));
}
/* Remove len bytes from the client socket. This is needed when the
* client sends a write we can't honour - we need to get rid of the
* bytes they've already written before we can look for another request.
*/
void client_flush(struct client *client, size_t len)
{
int devnull = open("/dev/null", O_WRONLY);
FATAL_IF_NEGATIVE(devnull, SHOW_ERRNO("Couldn't open /dev/null"));
int pipes[2];
pipe(pipes);
const unsigned int flags = SPLICE_F_MORE | SPLICE_F_MOVE;
size_t spliced = 0;
while (spliced < len) {
ssize_t received = splice(client->socket, NULL,
pipes[1], NULL,
len - spliced, flags);
FATAL_IF_NEGATIVE(received, SHOW_ERRNO("splice error"));
ssize_t junked = 0;
while (junked < received) {
ssize_t junk;
junk = splice(pipes[0], NULL, devnull, NULL, received, flags);
FATAL_IF_NEGATIVE(junk, SHOW_ERRNO("splice error"));
junked += junk;
}
spliced += received;
}
debug("Flushed %d bytes", len);
close(devnull);
}
/* Check to see if the client's request needs a reply constructing.
* Returns 1 if we do, 0 otherwise.
* request_err is set to 0 if the client sent a bad request, in which
* case we drop the connection.
*/
int client_request_needs_reply(struct client *client,
struct nbd_request request)
{
/* The client is stupid, but don't take down the whole server as a result.
* We send a reply before disconnecting so that at least some indication of
* the problem is visible, and so proxies don't retry the same (bad) request
* forever.
*/
if (request.magic != REQUEST_MAGIC) {
warn("Bad magic 0x%08X from client", request.magic);
client_write_reply(client, &request, EBADMSG);
client->disconnect = 1; // no need to flush
return 0;
}
debug("request type=%" PRIu16 ", flags=%" PRIu16 ", from=%" PRIu64
", len=%" PRIu32 ", handle=0x%08X", request.type, request.flags,
request.from, request.len, request.handle);
/* check it's not out of range. NBD protocol requires ENOSPC to be
* returned in this instance
*/
if (request.from + request.len > client->serve->size) {
warn("write request %" PRIu64 "+%" PRIu32 " out of range",
request.from, request.len);
if (request.type == REQUEST_WRITE) {
client_flush(client, request.len);
}
client_write_reply(client, &request, ENOSPC);
client->disconnect = 0;
return 0;
}
switch (request.type) {
case REQUEST_READ:
break;
case REQUEST_WRITE:
break;
case REQUEST_DISCONNECT:
debug("request disconnect");
client->disconnect = 1;
return 0;
case REQUEST_FLUSH:
break;
default:
/* NBD prototcol says servers SHOULD return EINVAL to unknown
* commands */
warn("Unknown request 0x%08X", request.type);
client_write_reply(client, &request, EINVAL);
client->disconnect = 0;
return 0;
}
return 1;
}
void client_reply_to_read(struct client *client,
struct nbd_request request)
{
off64_t offset;
debug("request read %ld+%d", request.from, request.len);
sock_set_tcp_cork(client->socket, 1);
client_write_reply(client, &request, 0);
offset = request.from;
/* If we get cut off partway through this sendfile, we don't
* want to kill the server. This should be an error.
*/
ERROR_IF_NEGATIVE(sendfileloop(client->socket,
client->fileno,
&offset,
request.len),
"sendfile failed from=%ld, len=%d",
offset, request.len);
sock_set_tcp_cork(client->socket, 0);
}
void client_reply_to_write(struct client *client,
struct nbd_request request)
{
debug("request write from=%" PRIu64 ", len=%" PRIu32 ", handle=0x%08X",
request.from, request.len, request.handle);
if (client->serve->allocation_map_built) {
write_not_zeroes(client, request.from, request.len);
} else {
debug("No allocation map, writing directly.");
/* If we get cut off partway through reading this data:
* */
ERROR_IF_NEGATIVE(readloop(client->socket,
client->mapped + request.from,
request.len),
SHOW_ERRNO("reading write data failed from=%ld, len=%d",
request.from, request.len));
/* the allocation_map is shared between client threads, and may be
* being built. We need to reflect the write in it, as it may be in
* a position the builder has already gone over.
*/
bitset_set_range(client->serve->allocation_map, request.from,
request.len);
}
// Only flush if FUA is set -- overridden for now to force flush after each
// write.
// if (request.flags & CMD_FLAG_FUA) {
if (1) {
/* multiple of page size */
uint64_t from_rounded =
request.from & (~(sysconf(_SC_PAGE_SIZE) - 1));
uint64_t len_rounded = request.len + (request.from - from_rounded);
debug("Calling msync from=%" PRIu64 ", len=%" PRIu64 "",
from_rounded, len_rounded);
FATAL_IF_NEGATIVE(msync(client->mapped + from_rounded,
len_rounded,
MS_SYNC | MS_INVALIDATE),
"msync failed %ld %ld", request.from,
request.len);
}
client_write_reply(client, &request, 0);
}
void client_reply_to_flush(struct client *client,
struct nbd_request request)
{
debug("request flush from=%" PRIu64 ", len=%" PRIu32 ", handle=0x%08X",
request.from, request.len, request.handle);
ERROR_IF_NEGATIVE(msync
(client->mapped, client->mapped_size,
MS_SYNC | MS_INVALIDATE), "flush failed");
client_write_reply(client, &request, 0);
}
void client_reply(struct client *client, struct nbd_request request)
{
switch (request.type) {
case REQUEST_READ:
client_reply_to_read(client, request);
break;
case REQUEST_WRITE:
client_reply_to_write(client, request);
break;
case REQUEST_FLUSH:
client_reply_to_flush(client, request);
break;
}
}
/* Starts a timer that will kill the whole process if disarm is not called
* within a timeout (see CLIENT_HANDLE_TIMEOUT).
*/
void client_arm_killswitch(struct client *client)
{
struct itimerspec its = {
.it_value = {.tv_nsec = 0,.tv_sec = CLIENT_HANDLER_TIMEOUT},
.it_interval = {.tv_nsec = 0,.tv_sec = 0}
};
if (!client->serve->use_killswitch) {
return;
}
debug("Arming killswitch");
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
SHOW_ERRNO("Failed to arm killswitch")
);
return;
}
void client_disarm_killswitch(struct client *client)
{
struct itimerspec its = {
.it_value = {.tv_nsec = 0,.tv_sec = 0},
.it_interval = {.tv_nsec = 0,.tv_sec = 0}
};
if (!client->serve->use_killswitch) {
return;
}
debug("Disarming killswitch");
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
SHOW_ERRNO("Failed to disarm killswitch")
);
return;
}
/* Returns 0 if we should continue trying to serve requests */
int client_serve_request(struct client *client)
{
struct nbd_request request = { 0 };
int stop = 1;
int disconnected = 0;
fd_set rfds, efds;
int fd_count;
/* wait until there are some bytes on the fd before committing to reads
* FIXME: this whole scheme is broken because we're using blocking reads.
* read() can block directly after a select anyway, and it's possible that,
* without the killswitch, we'd hang forever. With the killswitch, we just
* hang for "a while". The Right Thing to do is to rewrite client.c to be
* non-blocking.
*/
FD_ZERO(&rfds);
FD_SET(client->socket, &rfds);
self_pipe_fd_set(client->stop_signal, &rfds);
FD_ZERO(&efds);
FD_SET(client->socket, &efds);
fd_count = sock_try_select(FD_SETSIZE, &rfds, NULL, &efds, NULL);
if (fd_count == 0) {
/* This "can't ever happen" */
fatal("No FDs selected, and no timeout!");
} else if (fd_count < 0) {
fatal("Select failed");
}
if (self_pipe_fd_isset(client->stop_signal, &rfds)) {
debug("Client received stop signal.");
return 1; // Don't try to serve more requests
}
if (FD_ISSET(client->socket, &efds)) {
debug("Client connection closed");
return 1;
}
/* We arm / disarm around the whole request cycle. The reason for this is
* that the remote peer could uncleanly die at any point; if we're stuck on
* a blocking read(), then that will hang for (almost) forever. This is bad
* in general, makes the server respond only to kill -9, and breaks
* outward mirroring in a most unpleasant way.
*
* Don't forget to disarm before exiting, no matter what!
*
* The replication is simple: open a connection to the flexnbd server, write
* a single byte, and then wait.
*
*/
client_arm_killswitch(client);
if (!client_read_request(client, &request, &disconnected)) {
client_disarm_killswitch(client);
return stop;
}
if (disconnected) {
client_disarm_killswitch(client);
return stop;
}
if (!client_request_needs_reply(client, request)) {
client_disarm_killswitch(client);
return client->disconnect;
}
{
if (!server_is_closed(client->serve)) {
client_reply(client, request);
stop = 0;
}
}
client_disarm_killswitch(client);
return stop;
}
void client_send_hello(struct client *client)
{
client_write_init(client, client->serve->size);
}
void client_cleanup(struct client *client,
int fatal __attribute__ ((unused)))
{
info("client cleanup for client %p", client);
/* If the thread hits an error, we need to ensure this is off */
client_disarm_killswitch(client);
if (client->socket) {
FATAL_IF_NEGATIVE(close(client->socket),
"Error closing client socket %d",
client->socket);
debug("Closed client socket fd %d", client->socket);
client->socket = -1;
}
if (client->mapped) {
munmap(client->mapped, client->serve->size);
}
if (client->fileno) {
FATAL_IF_NEGATIVE(close(client->fileno),
"Error closing file %d", client->fileno);
debug("Closed client file fd %d", client->fileno);
client->fileno = -1;
}
if (server_acl_locked(client->serve)) {
server_unlock_acl(client->serve);
}
}
void *client_serve(void *client_uncast)
{
struct client *client = (struct client *) client_uncast;
error_set_handler((cleanup_handler *) client_cleanup, client);
info("client: mmaping file");
FATAL_IF_NEGATIVE(open_and_mmap(client->serve->filename,
&client->fileno,
&client->mapped_size,
(void **) &client->mapped),
SHOW_ERRNO("Couldn't open/mmap file %s",
client->serve->filename)
);
FATAL_IF_NEGATIVE(madvise
(client->mapped, client->serve->size, MADV_RANDOM),
SHOW_ERRNO("Failed to madvise() %s",
client->serve->filename)
);
debug("Opened client file fd %d", client->fileno);
debug("client: sending hello");
client_send_hello(client);
debug("client: serving requests");
while (client_serve_request(client) == 0);
debug("client: stopped serving requests");
client->stopped = 1;
if (client->disconnect) {
debug("client: control arrived");
server_control_arrived(client->serve);
}
debug("Cleaning client %p up normally in thread %p", client,
pthread_self());
client_cleanup(client, 0);
debug("Client thread done");
return NULL;
}

58
src/server/client.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef CLIENT_H
#define CLIENT_H
#include <signal.h>
#include <time.h>
#include <inttypes.h>
/** CLIENT_HANDLER_TIMEOUT
* This is the length of time (in seconds) any request can be outstanding for.
* If we spend longer than this in a request, the whole server is killed.
*/
#define CLIENT_HANDLER_TIMEOUT 120
/** CLIENT_KILLSWITCH_SIGNAL
* The signal number we use to kill the server when *any* killswitch timer
* fires. The handler gets the fd of the client socket to work with.
*/
#define CLIENT_KILLSWITCH_SIGNAL ( SIGRTMIN + 1 )
struct client {
/* When we call pthread_join, if the thread is already dead
* we can get an ESRCH. Since we have no other way to tell
* if that ESRCH is from a dead thread or a thread that never
* existed, we use a `stopped` flag to indicate a thread which
* did exist, but went away. Only check this after a
* pthread_join call.
*/
int stopped;
int socket;
int fileno;
char *mapped;
uint64_t mapped_size;
struct self_pipe *stop_signal;
struct server *serve; /* FIXME: remove above duplication */
/* Have we seen a REQUEST_DISCONNECT message? */
int disconnect;
/* kill the whole server if a request has been outstanding too long,
* assuming use_killswitch is set in serve
*/
timer_t killswitch;
};
void client_killswitch_hit(int signal, siginfo_t * info, void *ptr);
void *client_serve(void *client_uncast);
struct client *client_create(struct server *serve, int socket);
void client_destroy(struct client *client);
void client_signal_stop(struct client *client);
#endif

613
src/server/control.c Normal file
View File

@@ -0,0 +1,613 @@
/* FlexNBD server (C) Bytemark Hosting 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** The control server responds on a UNIX socket and services our "remote"
* commands which are used for changing the access control list, initiating
* a mirror process, or asking for status. The protocol is pretty simple -
* after connecting the client sends a series of LF-terminated lines, followed
* by a blank line (i.e. double LF). The first line is taken to be the command
* name to invoke, and the lines before the double LF are its arguments.
*
* These commands can be invoked remotely from the command line, with the
* client code to be found in remote.c
*/
#include "control.h"
#include "mirror.h"
#include "serve.h"
#include "util.h"
#include "ioutil.h"
#include "parse.h"
#include "readwrite.h"
#include "bitset.h"
#include "self_pipe.h"
#include "acl.h"
#include "status.h"
#include "mbox.h"
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
struct control *control_create(struct flexnbd *flexnbd, const char *csn)
{
struct control *control = xmalloc(sizeof(struct control));
NULLCHECK(csn);
control->flexnbd = flexnbd;
control->socket_name = csn;
control->open_signal = self_pipe_create();
control->close_signal = self_pipe_create();
control->mirror_state_mbox = mbox_create();
return control;
}
void control_signal_close(struct control *control)
{
NULLCHECK(control);
self_pipe_signal(control->close_signal);
}
void control_destroy(struct control *control)
{
NULLCHECK(control);
mbox_destroy(control->mirror_state_mbox);
self_pipe_destroy(control->close_signal);
self_pipe_destroy(control->open_signal);
free(control);
}
struct control_client *control_client_create(struct flexnbd *flexnbd,
int client_fd,
struct mbox *state_mbox)
{
NULLCHECK(flexnbd);
struct control_client *control_client =
xmalloc(sizeof(struct control_client));
control_client->socket = client_fd;
control_client->flexnbd = flexnbd;
control_client->mirror_state_mbox = state_mbox;
return control_client;
}
void control_client_destroy(struct control_client *client)
{
NULLCHECK(client);
free(client);
}
void control_respond(struct control_client *client);
void control_handle_client(struct control *control, int client_fd)
{
NULLCHECK(control);
NULLCHECK(control->flexnbd);
struct control_client *control_client =
control_client_create(control->flexnbd,
client_fd,
control->mirror_state_mbox);
/* We intentionally don't spawn a thread for the client here.
* This is to avoid having more than one thread potentially
* waiting on the migration commit status.
*/
control_respond(control_client);
}
void control_accept_client(struct control *control)
{
int client_fd;
union mysockaddr client_address;
socklen_t addrlen = sizeof(union mysockaddr);
client_fd =
accept(control->control_fd, &client_address.generic, &addrlen);
FATAL_IF(-1 == client_fd, "control accept failed");
control_handle_client(control, client_fd);
}
int control_accept(struct control *control)
{
NULLCHECK(control);
fd_set fds;
FD_ZERO(&fds);
FD_SET(control->control_fd, &fds);
self_pipe_fd_set(control->close_signal, &fds);
debug("Control thread selecting");
FATAL_UNLESS(0 < select(FD_SETSIZE, &fds, NULL, NULL, NULL),
"Control select failed.");
if (self_pipe_fd_isset(control->close_signal, &fds)) {
return 0;
}
if (FD_ISSET(control->control_fd, &fds)) {
control_accept_client(control);
}
return 1;
}
void control_accept_loop(struct control *control)
{
while (control_accept(control));
}
int open_control_socket(const char *socket_name)
{
struct sockaddr_un bind_address;
int control_fd;
if (!socket_name) {
fatal("Tried to open a control socket without a socket name");
}
control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(control_fd, "Couldn't create control socket");
memset(&bind_address, 0, sizeof(struct sockaddr_un));
bind_address.sun_family = AF_UNIX;
strncpy(bind_address.sun_path, socket_name,
sizeof(bind_address.sun_path) - 1);
//unlink(socket_name); /* ignore failure */
FATAL_IF_NEGATIVE(bind
(control_fd, &bind_address, sizeof(bind_address)),
"Couldn't bind control socket to %s: %s",
socket_name, strerror(errno)
);
FATAL_IF_NEGATIVE(listen(control_fd, 5),
"Couldn't listen on control socket");
return control_fd;
}
void control_listen(struct control *control)
{
NULLCHECK(control);
control->control_fd = open_control_socket(control->socket_name);
}
void control_wait_for_open_signal(struct control *control)
{
fd_set fds;
FD_ZERO(&fds);
self_pipe_fd_set(control->open_signal, &fds);
FATAL_IF_NEGATIVE(select(FD_SETSIZE, &fds, NULL, NULL, NULL),
"select() failed");
self_pipe_signal_clear(control->open_signal);
}
void control_serve(struct control *control)
{
NULLCHECK(control);
control_wait_for_open_signal(control);
control_listen(control);
while (control_accept(control));
}
void control_cleanup(struct control *control,
int fatal __attribute__ ((unused)))
{
NULLCHECK(control);
unlink(control->socket_name);
close(control->control_fd);
}
void *control_runner(void *control_uncast)
{
debug("Control thread");
NULLCHECK(control_uncast);
struct control *control = (struct control *) control_uncast;
error_set_handler((cleanup_handler *) control_cleanup, control);
control_serve(control);
control_cleanup(control, 0);
pthread_exit(NULL);
}
#define write_socket(msg) write(client_fd, (msg "\n"), strlen((msg))+1)
void control_write_mirror_response(enum mirror_state mirror_state,
int client_fd)
{
switch (mirror_state) {
case MS_INIT:
case MS_UNKNOWN:
write_socket("1: Mirror failed to initialise");
fatal("Impossible mirror state: %d", mirror_state);
case MS_FAIL_CONNECT:
write_socket("1: Mirror failed to connect");
break;
case MS_FAIL_REJECTED:
write_socket("1: Mirror was rejected");
break;
case MS_FAIL_NO_HELLO:
write_socket("1: Remote server failed to respond");
break;
case MS_FAIL_SIZE_MISMATCH:
write_socket("1: Remote size does not match local size");
break;
case MS_ABANDONED:
write_socket("1: Mirroring abandoned");
break;
case MS_GO:
case MS_DONE: /* Yes, I know we know better, but it's simpler this way */
write_socket("0: Mirror started");
break;
default:
fatal("Unhandled mirror state: %d", mirror_state);
}
}
#undef write_socket
/* Call this in the thread where you want to receive the mirror state */
enum mirror_state control_client_mirror_wait(struct control_client *client)
{
NULLCHECK(client);
NULLCHECK(client->mirror_state_mbox);
struct mbox *mbox = client->mirror_state_mbox;
enum mirror_state mirror_state;
enum mirror_state *contents;
contents = (enum mirror_state *) mbox_receive(mbox);
NULLCHECK(contents);
mirror_state = *contents;
free(contents);
return mirror_state;
}
#define write_socket(msg) write(client->socket, (msg "\n"), strlen((msg))+1)
/** Command parser to start mirror process from socket input */
int control_mirror(struct control_client *client, int linesc, char **lines)
{
NULLCHECK(client);
struct flexnbd *flexnbd = client->flexnbd;
union mysockaddr *connect_to = xmalloc(sizeof(union mysockaddr));
union mysockaddr *connect_from = NULL;
uint64_t max_Bps = UINT64_MAX;
int action_at_finish;
int raw_port;
if (linesc < 2) {
write_socket("1: mirror takes at least two parameters");
return -1;
}
if (parse_ip_to_sockaddr(&connect_to->generic, lines[0]) == 0) {
write_socket("1: bad IP address");
return -1;
}
raw_port = atoi(lines[1]);
if (raw_port < 0 || raw_port > 65535) {
write_socket("1: bad IP port number");
return -1;
}
connect_to->v4.sin_port = htobe16(raw_port);
action_at_finish = ACTION_EXIT;
if (linesc > 2) {
if (strcmp("exit", lines[2]) == 0) {
action_at_finish = ACTION_EXIT;
} else if (strcmp("unlink", lines[2]) == 0) {
action_at_finish = ACTION_UNLINK;
} else if (strcmp("nothing", lines[2]) == 0) {
action_at_finish = ACTION_NOTHING;
} else {
write_socket("1: action must be 'exit' or 'nothing'");
return -1;
}
}
if (linesc > 3) {
connect_from = xmalloc(sizeof(union mysockaddr));
if (parse_ip_to_sockaddr(&connect_from->generic, lines[3]) == 0) {
write_socket("1: bad bind address");
return -1;
}
}
if (linesc > 4) {
errno = 0;
max_Bps = strtoull(lines[4], NULL, 10);
if (errno == ERANGE) {
write_socket("1: max_bps out of range");
return -1;
} else if (errno != 0) {
write_socket("1: max_bps couldn't be parsed");
return -1;
}
}
if (linesc > 5) {
write_socket("1: unrecognised parameters to mirror");
return -1;
}
struct server *serve = flexnbd_server(flexnbd);
server_lock_start_mirror(serve);
{
if (server_mirror_can_start(serve)) {
serve->mirror_super = mirror_super_create(serve->filename,
connect_to,
connect_from,
max_Bps,
action_at_finish,
client->
mirror_state_mbox);
serve->mirror = serve->mirror_super->mirror;
server_prevent_mirror_start(serve);
} else {
if (serve->mirror_super) {
warn("Tried to start a second mirror run");
write_socket("1: mirror already running");
} else {
warn("Cannot start mirroring, shutting down");
write_socket("1: shutting down");
}
}
}
server_unlock_start_mirror(serve);
/* Do this outside the lock to minimise the length of time the
* sighandler can block the serve thread
*/
if (serve->mirror_super) {
FATAL_IF(0 != pthread_create(&serve->mirror_super->thread,
NULL,
mirror_super_runner,
serve),
"Failed to create mirror thread");
debug("Control thread mirror super waiting");
enum mirror_state state = control_client_mirror_wait(client);
debug("Control thread writing response");
control_write_mirror_response(state, client->socket);
}
debug("Control thread going away.");
return 0;
}
int control_mirror_max_bps(struct control_client *client, int linesc,
char **lines)
{
NULLCHECK(client);
NULLCHECK(client->flexnbd);
struct server *serve = flexnbd_server(client->flexnbd);
uint64_t max_Bps;
if (!serve->mirror_super) {
write_socket("1: Not currently mirroring");
return -1;
}
if (linesc != 1) {
write_socket("1: Bad format");
return -1;
}
errno = 0;
max_Bps = strtoull(lines[0], NULL, 10);
if (errno == ERANGE) {
write_socket("1: max_bps out of range");
return -1;
} else if (errno != 0) {
write_socket("1: max_bps couldn't be parsed");
return -1;
}
serve->mirror->max_bytes_per_second = max_Bps;
write_socket("0: updated");
return 0;
}
#undef write_socket
/** Command parser to alter access control list from socket input */
int control_acl(struct control_client *client, int linesc, char **lines)
{
NULLCHECK(client);
NULLCHECK(client->flexnbd);
struct flexnbd *flexnbd = client->flexnbd;
int default_deny = flexnbd_default_deny(flexnbd);
struct acl *new_acl = acl_create(linesc, lines, default_deny);
if (new_acl->len != linesc) {
warn("Bad ACL spec: %s", lines[new_acl->len]);
write(client->socket, "1: bad spec: ", 13);
write(client->socket, lines[new_acl->len],
strlen(lines[new_acl->len]));
write(client->socket, "\n", 1);
acl_destroy(new_acl);
} else {
flexnbd_replace_acl(flexnbd, new_acl);
info("ACL set");
write(client->socket, "0: updated\n", 11);
}
return 0;
}
int control_break(struct control_client *client,
int linesc __attribute__ ((unused)),
char **lines __attribute__ ((unused))
)
{
NULLCHECK(client);
NULLCHECK(client->flexnbd);
int result = 0;
struct flexnbd *flexnbd = client->flexnbd;
struct server *serve = flexnbd_server(flexnbd);
server_lock_start_mirror(serve);
{
if (server_is_mirroring(serve)) {
info("Signaling to abandon mirror");
server_abandon_mirror(serve);
debug("Abandon signaled");
if (server_is_closed(serve)) {
info("Mirror completed while canceling");
write(client->socket, "1: mirror completed\n", 20);
} else {
info("Mirror successfully stopped.");
write(client->socket, "0: mirror stopped\n", 18);
result = 1;
}
} else {
warn("Not mirroring.");
write(client->socket, "1: not mirroring\n", 17);
}
}
server_unlock_start_mirror(serve);
return result;
}
/** FIXME: add some useful statistics */
int control_status(struct control_client *client,
int linesc __attribute__ ((unused)),
char **lines __attribute__ ((unused))
)
{
NULLCHECK(client);
NULLCHECK(client->flexnbd);
struct status *status = flexnbd_status_create(client->flexnbd);
write(client->socket, "0: ", 3);
status_write(status, client->socket);
status_destroy(status);
return 0;
}
void control_client_cleanup(struct control_client *client,
int fatal __attribute__ ((unused)))
{
if (client->socket) {
close(client->socket);
}
/* This is wrongness */
if (server_acl_locked(client->flexnbd->serve)) {
server_unlock_acl(client->flexnbd->serve);
}
control_client_destroy(client);
}
/** Master command parser for control socket connections, delegates quickly */
void control_respond(struct control_client *client)
{
char **lines = NULL;
error_set_handler((cleanup_handler *) control_client_cleanup, client);
int i, linesc;
linesc = read_lines_until_blankline(client->socket, 256, &lines);
if (linesc < 1) {
write(client->socket, "9: missing command\n", 19);
/* ignore failure */
} else if (strcmp(lines[0], "acl") == 0) {
info("acl command received");
if (control_acl(client, linesc - 1, lines + 1) < 0) {
debug("acl command failed");
}
} else if (strcmp(lines[0], "mirror") == 0) {
info("mirror command received");
if (control_mirror(client, linesc - 1, lines + 1) < 0) {
debug("mirror command failed");
}
} else if (strcmp(lines[0], "break") == 0) {
info("break command received");
if (control_break(client, linesc - 1, lines + 1) < 0) {
debug("break command failed");
}
} else if (strcmp(lines[0], "status") == 0) {
info("status command received");
if (control_status(client, linesc - 1, lines + 1) < 0) {
debug("status command failed");
}
} else if (strcmp(lines[0], "mirror_max_bps") == 0) {
info("mirror_max_bps command received");
if (control_mirror_max_bps(client, linesc - 1, lines + 1) < 0) {
debug("mirror_max_bps command failed");
}
} else {
write(client->socket, "10: unknown command\n", 23);
}
for (i = 0; i < linesc; i++) {
free(lines[i]);
}
free(lines);
control_client_cleanup(client, 0);
debug("control command handled");
}

58
src/server/control.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef CONTROL_H
#define CONTROL_H
/* We need this to avoid a complaint about struct server * in
* void accept_control_connection
*/
struct server;
#include "parse.h"
#include "mirror.h"
#include "serve.h"
#include "flexnbd.h"
#include "mbox.h"
struct control {
struct flexnbd *flexnbd;
int control_fd;
const char *socket_name;
pthread_t thread;
struct self_pipe *open_signal;
struct self_pipe *close_signal;
/* This is owned by the control object, and used by a
* mirror_super to communicate the state of a mirror attempt as
* early as feasible. It can't be owned by the mirror_super
* object because the mirror_super object can be freed at any
* time (including while the control_client is waiting on it),
* whereas the control object lasts for the lifetime of the
* process (and we can only have a mirror thread if the control
* thread has started it).
*/
struct mbox *mirror_state_mbox;
};
struct control_client {
int socket;
struct flexnbd *flexnbd;
/* Passed in on creation. We know it's all right to do this
* because we know there's only ever one control_client.
*/
struct mbox *mirror_state_mbox;
};
struct control *control_create(struct flexnbd *,
const char *control_socket_name);
void control_signal_close(struct control *);
void control_destroy(struct control *);
void *control_runner(void *);
void accept_control_connection(struct server *params, int client_fd,
union mysockaddr *client_address);
void serve_open_control_socket(struct server *params);
#endif

248
src/server/flexnbd.c Normal file
View File

@@ -0,0 +1,248 @@
/* FlexNBD server (C) Bytemark Hosting 2012
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** main() function for parsing and dispatching commands. Each mode has
* a corresponding structure which is filled in and passed to a do_ function
* elsewhere in the program.
*/
#include "flexnbd.h"
#include "serve.h"
#include "util.h"
#include "control.h"
#include "status.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signalfd.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include "acl.h"
int flexnbd_build_signal_fd(void)
{
sigset_t mask;
int sfd;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGINT);
FATAL_UNLESS(0 == pthread_sigmask(SIG_BLOCK, &mask, NULL),
"Signal blocking failed");
sfd = signalfd(-1, &mask, 0);
FATAL_IF(-1 == sfd, "Failed to get a signal fd");
return sfd;
}
void flexnbd_create_shared(struct flexnbd *flexnbd,
const char *s_ctrl_sock)
{
NULLCHECK(flexnbd);
if (s_ctrl_sock) {
flexnbd->control = control_create(flexnbd, s_ctrl_sock);
} else {
flexnbd->control = NULL;
}
flexnbd->signal_fd = flexnbd_build_signal_fd();
}
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
int default_deny,
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch)
{
struct flexnbd *flexnbd = xmalloc(sizeof(struct flexnbd));
flexnbd->serve = server_create(flexnbd,
s_ip_address,
s_port,
s_file,
default_deny,
acl_entries,
s_acl_entries,
max_nbd_clients, use_killswitch, 1);
flexnbd_create_shared(flexnbd, s_ctrl_sock);
// Beats installing one handler per client instance
if (use_killswitch) {
struct sigaction act = {
.sa_sigaction = client_killswitch_hit,
.sa_flags = SA_RESTART | SA_SIGINFO
};
FATAL_UNLESS(0 == sigaction(CLIENT_KILLSWITCH_SIGNAL, &act, NULL),
"Installing client killswitch signal failed");
}
return flexnbd;
}
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
int default_deny,
int acl_entries,
char **s_acl_entries)
{
struct flexnbd *flexnbd = xmalloc(sizeof(struct flexnbd));
flexnbd->serve = server_create(flexnbd,
s_ip_address,
s_port,
s_file,
default_deny,
acl_entries, s_acl_entries, 1, 0, 0);
flexnbd_create_shared(flexnbd, s_ctrl_sock);
// listen can't use killswitch, as mirror may pause on sending things
// for a very long time.
return flexnbd;
}
void flexnbd_spawn_control(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
NULLCHECK(flexnbd->control);
pthread_t *control_thread = &flexnbd->control->thread;
FATAL_UNLESS(0 == pthread_create(control_thread,
NULL,
control_runner,
flexnbd->control),
"Couldn't create the control thread");
}
void flexnbd_stop_control(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
NULLCHECK(flexnbd->control);
control_signal_close(flexnbd->control);
pthread_t tid = flexnbd->control->thread;
FATAL_UNLESS(0 == pthread_join(tid, NULL),
"Failed joining the control thread");
debug("Control thread %p pthread_join returned", tid);
}
int flexnbd_signal_fd(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
return flexnbd->signal_fd;
}
void flexnbd_destroy(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
if (flexnbd->control) {
control_destroy(flexnbd->control);
}
close(flexnbd->signal_fd);
free(flexnbd);
}
struct server *flexnbd_server(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
return flexnbd->serve;
}
void flexnbd_replace_acl(struct flexnbd *flexnbd, struct acl *acl)
{
NULLCHECK(flexnbd);
server_replace_acl(flexnbd_server(flexnbd), acl);
}
struct status *flexnbd_status_create(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
struct status *status;
status = status_create(flexnbd_server(flexnbd));
return status;
}
void flexnbd_set_server(struct flexnbd *flexnbd, struct server *serve)
{
NULLCHECK(flexnbd);
flexnbd->serve = serve;
}
/* Get the default_deny of the current server object. */
int flexnbd_default_deny(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
return server_default_deny(flexnbd->serve);
}
void make_writable(const char *filename)
{
NULLCHECK(filename);
FATAL_IF_NEGATIVE(chmod(filename, S_IWUSR),
"Couldn't chmod %s: %s", filename, strerror(errno));
}
int flexnbd_serve(struct flexnbd *flexnbd)
{
NULLCHECK(flexnbd);
int success;
struct self_pipe *open_signal = NULL;
if (flexnbd->control) {
debug("Spawning control thread");
flexnbd_spawn_control(flexnbd);
open_signal = flexnbd->control->open_signal;
}
success = do_serve(flexnbd->serve, open_signal);
debug("do_serve success is %d", success);
if (flexnbd->control) {
debug("Stopping control thread");
flexnbd_stop_control(flexnbd);
debug("Control thread stopped");
}
return success;
}

63
src/server/flexnbd.h Normal file
View File

@@ -0,0 +1,63 @@
#ifndef FLEXNBD_H
#define FLEXNBD_H
#include "acl.h"
#include "mirror.h"
#include "serve.h"
#include "proxy.h"
#include "client.h"
#include "self_pipe.h"
#include "mbox.h"
#include "control.h"
#include "flexthread.h"
/* Carries the "globals". */
struct flexnbd {
/* Our serve pointer should never be dereferenced outside a
* flexnbd_switch_lock/unlock pair.
*/
struct server *serve;
/* We only have a control object if a control socket name was
* passed on the command line.
*/
struct control *control;
/* File descriptor for a signalfd(2) signal stream. */
int signal_fd;
};
struct flexnbd *flexnbd_create(void);
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
int default_deny,
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch);
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
int default_deny,
int acl_entries,
char **s_acl_entries);
void flexnbd_destroy(struct flexnbd *);
enum mirror_state;
enum mirror_state flexnbd_get_mirror_state(struct flexnbd *);
int flexnbd_default_deny(struct flexnbd *);
void flexnbd_set_server(struct flexnbd *flexnbd, struct server *serve);
int flexnbd_signal_fd(struct flexnbd *flexnbd);
int flexnbd_serve(struct flexnbd *flexnbd);
int flexnbd_proxy(struct flexnbd *flexnbd);
struct server *flexnbd_server(struct flexnbd *flexnbd);
void flexnbd_replace_acl(struct flexnbd *flexnbd, struct acl *acl);
struct status *flexnbd_status_create(struct flexnbd *flexnbd);
#endif

73
src/server/flexthread.c Normal file
View File

@@ -0,0 +1,73 @@
#include "flexthread.h"
#include "util.h"
#include <pthread.h>
struct flexthread_mutex *flexthread_mutex_create(void)
{
struct flexthread_mutex *ftm =
xmalloc(sizeof(struct flexthread_mutex));
FATAL_UNLESS(0 == pthread_mutex_init(&ftm->mutex, NULL),
"Mutex initialisation failed");
return ftm;
}
void flexthread_mutex_destroy(struct flexthread_mutex *ftm)
{
NULLCHECK(ftm);
if (flexthread_mutex_held(ftm)) {
flexthread_mutex_unlock(ftm);
} else if ((pthread_t) NULL != ftm->holder) {
/* This "should never happen": if we can try to destroy
* a mutex currently held by another thread, there's a
* logic bug somewhere. I know the test here is racy,
* but there's not a lot we can do about it at this
* point.
*/
fatal("Attempted to destroy a flexthread_mutex"
" held by another thread!");
}
FATAL_UNLESS(0 == pthread_mutex_destroy(&ftm->mutex),
"Mutex destroy failed");
free(ftm);
}
int flexthread_mutex_lock(struct flexthread_mutex *ftm)
{
NULLCHECK(ftm);
int failure = pthread_mutex_lock(&ftm->mutex);
if (0 == failure) {
ftm->holder = pthread_self();
}
return failure;
}
int flexthread_mutex_unlock(struct flexthread_mutex *ftm)
{
NULLCHECK(ftm);
pthread_t orig = ftm->holder;
ftm->holder = (pthread_t) NULL;
int failure = pthread_mutex_unlock(&ftm->mutex);
if (0 != failure) {
ftm->holder = orig;
}
return failure;
}
int flexthread_mutex_held(struct flexthread_mutex *ftm)
{
NULLCHECK(ftm);
return pthread_self() == ftm->holder;
}

29
src/server/flexthread.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef FLEXTHREAD_H
#define FLEXTHREAD_H
#include <pthread.h>
/* Define a mutex wrapper object. This wrapper allows us to easily
* track whether or not we currently hold the wrapped mutex. If we hold
* the mutex when we destroy it, then we first release it.
*
* These are specifically for the case where an ERROR_* handler gets
* called when we might (or might not) have a mutex held. The
* flexthread_mutex_held() function will tell you if your thread
* currently holds the given mutex. It's not safe to make any other
* comparisons.
*/
struct flexthread_mutex {
pthread_mutex_t mutex;
pthread_t holder;
};
struct flexthread_mutex *flexthread_mutex_create(void);
void flexthread_mutex_destroy(struct flexthread_mutex *);
int flexthread_mutex_lock(struct flexthread_mutex *);
int flexthread_mutex_unlock(struct flexthread_mutex *);
int flexthread_mutex_held(struct flexthread_mutex *);
#endif

77
src/server/mbox.c Normal file
View File

@@ -0,0 +1,77 @@
#include "mbox.h"
#include "util.h"
#include <pthread.h>
struct mbox *mbox_create(void)
{
struct mbox *mbox = xmalloc(sizeof(struct mbox));
FATAL_UNLESS(0 == pthread_cond_init(&mbox->filled_cond, NULL),
"Failed to initialise a condition variable");
FATAL_UNLESS(0 == pthread_cond_init(&mbox->emptied_cond, NULL),
"Failed to initialise a condition variable");
FATAL_UNLESS(0 == pthread_mutex_init(&mbox->mutex, NULL),
"Failed to initialise a mutex");
return mbox;
}
void mbox_post(struct mbox *mbox, void *contents)
{
pthread_mutex_lock(&mbox->mutex);
{
if (mbox->full) {
pthread_cond_wait(&mbox->emptied_cond, &mbox->mutex);
}
mbox->contents = contents;
mbox->full = 1;
while (0 != pthread_cond_signal(&mbox->filled_cond));
}
pthread_mutex_unlock(&mbox->mutex);
}
void *mbox_contents(struct mbox *mbox)
{
return mbox->contents;
}
int mbox_is_full(struct mbox *mbox)
{
return mbox->full;
}
void *mbox_receive(struct mbox *mbox)
{
NULLCHECK(mbox);
void *result;
pthread_mutex_lock(&mbox->mutex);
{
if (!mbox->full) {
pthread_cond_wait(&mbox->filled_cond, &mbox->mutex);
}
mbox->full = 0;
result = mbox->contents;
mbox->contents = NULL;
while (0 != pthread_cond_signal(&mbox->emptied_cond));
}
pthread_mutex_unlock(&mbox->mutex);
return result;
}
void mbox_destroy(struct mbox *mbox)
{
NULLCHECK(mbox);
while (0 != pthread_cond_destroy(&mbox->emptied_cond));
while (0 != pthread_cond_destroy(&mbox->filled_cond));
while (0 != pthread_mutex_destroy(&mbox->mutex));
free(mbox);
}

55
src/server/mbox.h Normal file
View File

@@ -0,0 +1,55 @@
#ifndef MBOX_H
#define MBOX_H
/** mbox
* A thread sync object. Put a void * into the mbox in one thread, and
* get it out in another. The receiving thread will block if there's
* nothing in the mbox, and the sending thread will block if there is.
* The mbox doesn't assume any responsibility for the pointer it's
* passed - you must free it yourself if it's malloced.
*/
#include <pthread.h>
struct mbox {
void *contents;
/** Marker to tell us if there's content in the box.
* Keeping this separate allows us to use NULL for the contents.
*/
int full;
/** This gets signaled by mbox_post, and waited on by
* mbox_receive */
pthread_cond_t filled_cond;
/** This is signaled by mbox_receive, and waited on by mbox_post */
pthread_cond_t emptied_cond;
pthread_mutex_t mutex;
};
/* Create an mbox. */
struct mbox *mbox_create(void);
/* Put something in the mbox, blocking if it's already full.
* That something can be NULL if you want.
*/
void mbox_post(struct mbox *, void *);
/* See what's in the mbox. This isn't thread-safe. */
void *mbox_contents(struct mbox *);
/* See if anything has been put into the mbox. This isn't thread-safe.
* */
int mbox_is_full(struct mbox *);
/* Get the contents from the mbox, blocking if there's nothing there. */
void *mbox_receive(struct mbox *);
/* Free the mbox and destroy the associated pthread bits. */
void mbox_destroy(struct mbox *);
#endif

1071
src/server/mirror.c Normal file

File diff suppressed because it is too large Load Diff

139
src/server/mirror.h Normal file
View File

@@ -0,0 +1,139 @@
#ifndef MIRROR_H
#define MIRROR_H
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include "bitset.h"
#include "self_pipe.h"
enum mirror_state;
#include "serve.h"
#include "mbox.h"
/* MS_CONNECT_TIME_SECS
* The length of time after which the sender will assume a connect() to
* the destination has failed.
*/
#define MS_CONNECT_TIME_SECS 60
/* MS_MAX_DOWNTIME_SECS
* The length of time a migration must be estimated to have remaining for us to
* disconnect clients for convergence
*
* TODO: Make this configurable so refusing-to-converge clients can be manually
* fixed.
* TODO: Make this adaptive - 5 seconds is fine, as long as we can guarantee
* that all migrations will be able to converge in time. We'd add a new
* state between open and closed, where gradually-increasing latency is
* added to client requests to allow the mirror to be faster.
*/
#define MS_CONVERGE_TIME_SECS 5
/* MS_HELLO_TIME_SECS
* The length of time the sender will wait for the NBD hello message
* after connect() before aborting the connection attempt.
*/
#define MS_HELLO_TIME_SECS 5
/* MS_RETRY_DELAY_SECS
* The delay after a failed migration attempt before launching another
* thread to try again.
*/
#define MS_RETRY_DELAY_SECS 1
/* MS_REQUEST_LIMIT_SECS
* We must receive a reply to a request within this time. For a read
* request, this is the time between the end of the NBD request and the
* start of the NBD reply. For a write request, this is the time
* between the end of the written data and the start of the NBD reply.
* Can be overridden by the environment variable:
* FLEXNBD_MS_REQUEST_LIMIT_SECS
*/
#define MS_REQUEST_LIMIT_SECS 60
#define MS_REQUEST_LIMIT_SECS_F 60.0
enum mirror_finish_action {
ACTION_EXIT,
ACTION_UNLINK,
ACTION_NOTHING
};
enum mirror_state {
MS_UNKNOWN,
MS_INIT,
MS_GO,
MS_ABANDONED,
MS_DONE,
MS_FAIL_CONNECT,
MS_FAIL_REJECTED,
MS_FAIL_NO_HELLO,
MS_FAIL_SIZE_MISMATCH
};
struct mirror {
pthread_t thread;
/* Signal to this then join the thread if you want to abandon mirroring */
struct self_pipe *abandon_signal;
union mysockaddr *connect_to;
union mysockaddr *connect_from;
int client;
const char *filename;
/* Limiter, used to restrict migration speed Only dirty bytes (those going
* over the network) are considered */
uint64_t max_bytes_per_second;
enum mirror_finish_action action_at_finish;
char *mapped;
/* We need to send every byte at least once; we do so by */
uint64_t offset;
enum mirror_state commit_state;
/* commit_signal is sent immediately after attempting to connect
* and checking the remote size, whether successful or not.
*/
struct mbox *commit_signal;
/* The time (from monotonic_time_ms()) the migration was started. Can be
* used to calculate bps, etc. */
uint64_t migration_started;
/* Running count of all bytes we've transferred */
uint64_t all_dirty;
};
struct mirror_super {
struct mirror *mirror;
pthread_t thread;
struct mbox *state_mbox;
};
/* We need these declaration to get around circular dependencies in the
* .h's
*/
struct server;
struct flexnbd;
struct mirror_super *mirror_super_create(const char *filename,
union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
enum mirror_finish_action
action_at_finish,
struct mbox *state_mbox);
void *mirror_super_runner(void *serve_uncast);
#endif

889
src/server/mode.c Normal file
View File

@@ -0,0 +1,889 @@
#include "mode.h"
#include "flexnbd.h"
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static struct option serve_options[] = {
GETOPT_HELP,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_FILE,
GETOPT_SOCK,
GETOPT_DENY,
GETOPT_QUIET,
GETOPT_KILLSWITCH,
GETOPT_VERBOSE,
{0}
};
static char serve_short_options[] = "hl:p:f:s:dk" SOPT_QUIET SOPT_VERBOSE;
static char serve_help_text[] =
"Usage: flexnbd " CMD_SERVE " <options> [<acl address>*]\n\n"
"Serve FILE from ADDR:PORT, with an optional control socket at SOCK.\n\n"
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to serve on.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to serve on.\n"
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
"\t--" OPT_KILLSWITCH
",-k \tKill the server if a request takes 120 seconds.\n" SOCK_LINE
VERBOSE_LINE QUIET_LINE;
static struct option listen_options[] = {
GETOPT_HELP,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_FILE,
GETOPT_SOCK,
GETOPT_DENY,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char listen_short_options[] = "hl:p:f:s:d" SOPT_QUIET SOPT_VERBOSE;
static char listen_help_text[] =
"Usage: flexnbd " CMD_LISTEN " <options> [<acl_address>*]\n\n"
"Listen for an incoming migration on ADDR:PORT."
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to listen on.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to listen on.\n"
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option read_options[] = {
GETOPT_HELP,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_FROM,
GETOPT_SIZE,
GETOPT_BIND,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char read_short_options[] = "hl:p:F:S:b:" SOPT_QUIET SOPT_VERBOSE;
static char read_help_text[] =
"Usage: flexnbd " CMD_READ " <options>\n\n"
"Read SIZE bytes from a server at ADDR:PORT to stdout, starting at OFFSET.\n\n"
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to read from.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to read from.\n"
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to read from.\n"
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to read.\n"
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option *write_options = read_options;
static char *write_short_options = read_short_options;
static char write_help_text[] =
"Usage: flexnbd " CMD_WRITE " <options>\n\n"
"Write SIZE bytes from stdin to a server at ADDR:PORT, starting at OFFSET.\n\n"
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to write to.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to write to.\n"
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to write from.\n"
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to write.\n"
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option acl_options[] = {
GETOPT_HELP,
GETOPT_SOCK,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char acl_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char acl_help_text[] =
"Usage: flexnbd " CMD_ACL " <options> [<acl address>+]\n\n"
"Set the access control list for a server with control socket SOCK.\n\n"
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option mirror_speed_options[] = {
GETOPT_HELP,
GETOPT_SOCK,
GETOPT_MAX_SPEED,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char mirror_speed_short_options[] = "hs:m:" SOPT_QUIET SOPT_VERBOSE;
static char mirror_speed_help_text[] =
"Usage: flexnbd " CMD_MIRROR_SPEED " <options>\n\n"
"Set the maximum speed of a migration from a mirring server listening on SOCK.\n\n"
HELP_LINE SOCK_LINE MAX_SPEED_LINE VERBOSE_LINE QUIET_LINE;
static struct option mirror_options[] = {
GETOPT_HELP,
GETOPT_SOCK,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_UNLINK,
GETOPT_BIND,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char mirror_short_options[] = "hs:l:p:ub:" SOPT_QUIET SOPT_VERBOSE;
static char mirror_help_text[] =
"Usage: flexnbd " CMD_MIRROR " <options>\n\n"
"Start mirroring from the server with control socket SOCK to one at ADDR:PORT.\n\n"
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to mirror to.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to mirror to.\n"
SOCK_LINE
"\t--" OPT_UNLINK ",-u\tUnlink the local file when done.\n"
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option break_options[] = {
GETOPT_HELP,
GETOPT_SOCK,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char break_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char break_help_text[] =
"Usage: flexnbd " CMD_BREAK " <options>\n\n"
"Stop mirroring from the server with control socket SOCK.\n\n"
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option status_options[] = {
GETOPT_HELP,
GETOPT_SOCK,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char status_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char status_help_text[] =
"Usage: flexnbd " CMD_STATUS " <options>\n\n"
"Get the status for a server with control socket SOCK.\n\n"
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
char help_help_text_arr[] =
"Usage: flexnbd <cmd> [cmd options]\n\n"
"Commands:\n"
"\tflexnbd serve\n"
"\tflexnbd listen\n"
"\tflexnbd read\n"
"\tflexnbd write\n"
"\tflexnbd acl\n"
"\tflexnbd mirror\n"
"\tflexnbd mirror-speed\n"
"\tflexnbd break\n"
"\tflexnbd status\n"
"\tflexnbd help\n\n" "See flexnbd help <cmd> for further info\n";
/* Slightly odd array/pointer pair to stop the compiler from complaining
* about symbol sizes
*/
char *help_help_text = help_help_text_arr;
void do_read(struct mode_readwrite_params *params);
void do_write(struct mode_readwrite_params *params);
void do_remote_command(char *command, char *mode, int argc, char **argv);
void read_serve_param(int c, char **ip_addr, char **ip_port, char **file,
char **sock, int *default_deny, int *use_killswitch)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", serve_help_text);
exit(0);
case 'l':
*ip_addr = optarg;
break;
case 'p':
*ip_port = optarg;
break;
case 'f':
*file = optarg;
break;
case 's':
*sock = optarg;
break;
case 'd':
*default_deny = 1;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
case 'k':
*use_killswitch = 1;
break;
default:
exit_err(serve_help_text);
break;
}
}
void read_listen_param(int c,
char **ip_addr,
char **ip_port,
char **file, char **sock, int *default_deny)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", listen_help_text);
exit(0);
case 'l':
*ip_addr = optarg;
break;
case 'p':
*ip_port = optarg;
break;
case 'f':
*file = optarg;
break;
case 's':
*sock = optarg;
break;
case 'd':
*default_deny = 1;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(listen_help_text);
break;
}
}
void read_readwrite_param(int c, char **ip_addr, char **ip_port,
char **bind_addr, char **from, char **size,
char *err_text)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", err_text);
exit(0);
case 'l':
*ip_addr = optarg;
break;
case 'p':
*ip_port = optarg;
break;
case 'F':
*from = optarg;
break;
case 'S':
*size = optarg;
break;
case 'b':
*bind_addr = optarg;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(err_text);
break;
}
}
void read_sock_param(int c, char **sock, char *help_text)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", help_text);
exit(0);
case 's':
*sock = optarg;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(help_text);
break;
}
}
void read_acl_param(int c, char **sock)
{
read_sock_param(c, sock, acl_help_text);
}
void read_mirror_speed_param(int c, char **sock, char **max_speed)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", mirror_speed_help_text);
exit(0);
case 's':
*sock = optarg;
break;
case 'm':
*max_speed = optarg;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(mirror_speed_help_text);
break;
}
}
void read_mirror_param(int c,
char **sock,
char **ip_addr,
char **ip_port, int *unlink, char **bind_addr)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", mirror_help_text);
exit(0);
case 's':
*sock = optarg;
break;
case 'l':
*ip_addr = optarg;
break;
case 'p':
*ip_port = optarg;
break;
case 'u':
*unlink = 1;
break;
case 'b':
*bind_addr = optarg;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(mirror_help_text);
break;
}
}
void read_break_param(int c, char **sock)
{
switch (c) {
case 'h':
fprintf(stdout, "%s\n", break_help_text);
exit(0);
case 's':
*sock = optarg;
break;
case 'q':
log_level = QUIET_LOG_LEVEL;
break;
case 'v':
log_level = VERBOSE_LOG_LEVEL;
break;
default:
exit_err(break_help_text);
break;
}
}
void read_status_param(int c, char **sock)
{
read_sock_param(c, sock, status_help_text);
}
int mode_serve(int argc, char *argv[])
{
int c;
char *ip_addr = NULL;
char *ip_port = NULL;
char *file = NULL;
char *sock = NULL;
int default_deny = 0; // not on by default
int use_killswitch = 0;
int err = 0;
int success;
struct flexnbd *flexnbd;
while (1) {
c = getopt_long(argc, argv, serve_short_options, serve_options,
NULL);
if (c == -1) {
break;
}
read_serve_param(c, &ip_addr, &ip_port, &file, &sock,
&default_deny, &use_killswitch);
}
if (NULL == ip_addr || NULL == ip_port) {
err = 1;
fprintf(stderr, "both --addr and --port are required.\n");
}
if (NULL == file) {
err = 1;
fprintf(stderr, "--file is required\n");
}
if (err) {
exit_err(serve_help_text);
}
flexnbd =
flexnbd_create_serving(ip_addr, ip_port, file, sock, default_deny,
argc - optind, argv + optind,
MAX_NBD_CLIENTS, use_killswitch);
info("Serving file %s", file);
success = flexnbd_serve(flexnbd);
flexnbd_destroy(flexnbd);
return success ? 0 : 1;
}
int mode_listen(int argc, char *argv[])
{
int c;
char *ip_addr = NULL;
char *ip_port = NULL;
char *file = NULL;
char *sock = NULL;
int default_deny = 0; // not on by default
int err = 0;
int success;
struct flexnbd *flexnbd;
while (1) {
c = getopt_long(argc, argv, listen_short_options, listen_options,
NULL);
if (c == -1) {
break;
}
read_listen_param(c, &ip_addr, &ip_port,
&file, &sock, &default_deny);
}
if (NULL == ip_addr || NULL == ip_port) {
err = 1;
fprintf(stderr, "both --addr and --port are required.\n");
}
if (NULL == file) {
err = 1;
fprintf(stderr, "--file is required\n");
}
if (err) {
exit_err(listen_help_text);
}
flexnbd = flexnbd_create_listening(ip_addr,
ip_port,
file,
sock,
default_deny,
argc - optind, argv + optind);
success = flexnbd_serve(flexnbd);
flexnbd_destroy(flexnbd);
return success ? 0 : 1;
}
/* TODO: Separate this function.
* It should be:
* params_read( struct mode_readwrite_params* out,
* char *s_ip_address,
* char *s_port,
* char *s_from,
* char *s_length )
* params_write( struct mode_readwrite_params* out,
* char *s_ip_address,
* char *s_port,
* char *s_from,
* char *s_length,
* char *s_filename )
*/
void params_readwrite(int write_not_read,
struct mode_readwrite_params *out,
char *s_ip_address,
char *s_port,
char *s_bind_address,
char *s_from, char *s_length_or_filename)
{
FATAL_IF_NULL(s_ip_address, "No IP address supplied");
FATAL_IF_NULL(s_port, "No port number supplied");
FATAL_IF_NULL(s_from, "No from supplied");
FATAL_IF_NULL(s_length_or_filename, "No length supplied");
FATAL_IF_ZERO(parse_ip_to_sockaddr
(&out->connect_to.generic, s_ip_address),
"Couldn't parse connection address '%s'", s_ip_address);
if (s_bind_address != NULL &&
parse_ip_to_sockaddr(&out->connect_from.generic,
s_bind_address) == 0) {
fatal("Couldn't parse bind address '%s'", s_bind_address);
}
parse_port(s_port, &out->connect_to.v4);
long signed_from = atol(s_from);
FATAL_IF_NEGATIVE(signed_from,
"Can't read from a negative offset %d.",
signed_from);
out->from = signed_from;
if (write_not_read) {
if (s_length_or_filename[0] - 48 < 10) {
out->len = atol(s_length_or_filename);
out->data_fd = 0;
} else {
out->data_fd = open(s_length_or_filename, O_RDONLY);
FATAL_IF_NEGATIVE(out->data_fd,
"Couldn't open %s", s_length_or_filename);
off64_t signed_len = lseek64(out->data_fd, 0, SEEK_END);
FATAL_IF_NEGATIVE(signed_len,
"Couldn't find length of %s",
s_length_or_filename);
out->len = signed_len;
FATAL_IF_NEGATIVE(lseek64(out->data_fd, 0, SEEK_SET),
"Couldn't rewind %s", s_length_or_filename);
}
} else {
out->len = atol(s_length_or_filename);
out->data_fd = 1;
}
}
int mode_read(int argc, char *argv[])
{
int c;
char *ip_addr = NULL;
char *ip_port = NULL;
char *bind_addr = NULL;
char *from = NULL;
char *size = NULL;
int err = 0;
struct mode_readwrite_params readwrite;
while (1) {
c = getopt_long(argc, argv, read_short_options, read_options,
NULL);
if (c == -1) {
break;
}
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
&size, read_help_text);
}
if (NULL == ip_addr || NULL == ip_port) {
err = 1;
fprintf(stderr, "both --addr and --port are required.\n");
}
if (NULL == from || NULL == size) {
err = 1;
fprintf(stderr, "both --from and --size are required.\n");
}
if (err) {
exit_err(read_help_text);
}
memset(&readwrite, 0, sizeof(readwrite));
params_readwrite(0, &readwrite, ip_addr, ip_port, bind_addr, from,
size);
do_read(&readwrite);
return 0;
}
int mode_write(int argc, char *argv[])
{
int c;
char *ip_addr = NULL;
char *ip_port = NULL;
char *bind_addr = NULL;
char *from = NULL;
char *size = NULL;
int err = 0;
struct mode_readwrite_params readwrite;
while (1) {
c = getopt_long(argc, argv, write_short_options, write_options,
NULL);
if (c == -1) {
break;
}
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
&size, write_help_text);
}
if (NULL == ip_addr || NULL == ip_port) {
err = 1;
fprintf(stderr, "both --addr and --port are required.\n");
}
if (NULL == from || NULL == size) {
err = 1;
fprintf(stderr, "both --from and --size are required.\n");
}
if (err) {
exit_err(write_help_text);
}
memset(&readwrite, 0, sizeof(readwrite));
params_readwrite(1, &readwrite, ip_addr, ip_port, bind_addr, from,
size);
do_write(&readwrite);
return 0;
}
int mode_acl(int argc, char *argv[])
{
int c;
char *sock = NULL;
while (1) {
c = getopt_long(argc, argv, acl_short_options, acl_options, NULL);
if (c == -1) {
break;
}
read_acl_param(c, &sock);
}
if (NULL == sock) {
fprintf(stderr, "--sock is required.\n");
exit_err(acl_help_text);
}
/* Don't use the CMD_ACL macro here, "acl" is the remote command
* name, not the cli option
*/
do_remote_command("acl", sock, argc - optind, argv + optind);
return 0;
}
int mode_mirror_speed(int argc, char *argv[])
{
int c;
char *sock = NULL;
char *speed = NULL;
while (1) {
c = getopt_long(argc, argv, mirror_speed_short_options,
mirror_speed_options, NULL);
if (-1 == c) {
break;
}
read_mirror_speed_param(c, &sock, &speed);
}
if (NULL == sock) {
fprintf(stderr, "--sock is required.\n");
exit_err(mirror_speed_help_text);
}
if (NULL == speed) {
fprintf(stderr, "--max-speed is required.\n");
exit_err(mirror_speed_help_text);
}
do_remote_command("mirror_max_bps", sock, 1, &speed);
return 0;
}
int mode_mirror(int argc, char *argv[])
{
int c;
char *sock = NULL;
char *remote_argv[4] = { 0 };
int err = 0;
int unlink = 0;
remote_argv[2] = "exit";
while (1) {
c = getopt_long(argc, argv, mirror_short_options, mirror_options,
NULL);
if (-1 == c) {
break;
}
read_mirror_param(c,
&sock,
&remote_argv[0],
&remote_argv[1], &unlink, &remote_argv[3]);
}
if (NULL == sock) {
fprintf(stderr, "--sock is required.\n");
err = 1;
}
if (NULL == remote_argv[0] || NULL == remote_argv[1]) {
fprintf(stderr, "both --addr and --port are required.\n");
err = 1;
}
if (err) {
exit_err(mirror_help_text);
}
if (unlink) {
remote_argv[2] = "unlink";
}
if (remote_argv[3] == NULL) {
do_remote_command("mirror", sock, 3, remote_argv);
} else {
do_remote_command("mirror", sock, 4, remote_argv);
}
return 0;
}
int mode_break(int argc, char *argv[])
{
int c;
char *sock = NULL;
while (1) {
c = getopt_long(argc, argv, break_short_options, break_options,
NULL);
if (-1 == c) {
break;
}
read_break_param(c, &sock);
}
if (NULL == sock) {
fprintf(stderr, "--sock is required.\n");
exit_err(break_help_text);
}
do_remote_command("break", sock, argc - optind, argv + optind);
return 0;
}
int mode_status(int argc, char *argv[])
{
int c;
char *sock = NULL;
while (1) {
c = getopt_long(argc, argv, status_short_options, status_options,
NULL);
if (-1 == c) {
break;
}
read_status_param(c, &sock);
}
if (NULL == sock) {
fprintf(stderr, "--sock is required.\n");
exit_err(status_help_text);
}
do_remote_command("status", sock, argc - optind, argv + optind);
return 0;
}
int mode_help(int argc, char *argv[])
{
char *cmd;
char *help_text = NULL;
if (argc < 1) {
help_text = help_help_text;
} else {
cmd = argv[0];
if (IS_CMD(CMD_SERVE, cmd)) {
help_text = serve_help_text;
} else if (IS_CMD(CMD_LISTEN, cmd)) {
help_text = listen_help_text;
} else if (IS_CMD(CMD_READ, cmd)) {
help_text = read_help_text;
} else if (IS_CMD(CMD_WRITE, cmd)) {
help_text = write_help_text;
} else if (IS_CMD(CMD_ACL, cmd)) {
help_text = acl_help_text;
} else if (IS_CMD(CMD_MIRROR, cmd)) {
help_text = mirror_help_text;
} else if (IS_CMD(CMD_STATUS, cmd)) {
help_text = status_help_text;
} else {
exit_err(help_help_text);
}
}
fprintf(stdout, "%s\n", help_text);
return 0;
}
void mode(char *mode, int argc, char **argv)
{
if (IS_CMD(CMD_SERVE, mode)) {
exit(mode_serve(argc, argv));
} else if (IS_CMD(CMD_LISTEN, mode)) {
exit(mode_listen(argc, argv));
} else if (IS_CMD(CMD_READ, mode)) {
mode_read(argc, argv);
} else if (IS_CMD(CMD_WRITE, mode)) {
mode_write(argc, argv);
} else if (IS_CMD(CMD_ACL, mode)) {
mode_acl(argc, argv);
} else if (IS_CMD(CMD_MIRROR_SPEED, mode)) {
mode_mirror_speed(argc, argv);
} else if (IS_CMD(CMD_MIRROR, mode)) {
mode_mirror(argc, argv);
} else if (IS_CMD(CMD_BREAK, mode)) {
mode_break(argc, argv);
} else if (IS_CMD(CMD_STATUS, mode)) {
mode_status(argc, argv);
} else if (IS_CMD(CMD_HELP, mode)) {
mode_help(argc - 1, argv + 1);
} else {
mode_help(argc - 1, argv + 1);
exit(1);
}
exit(0);
}

988
src/server/serve.c Normal file
View File

@@ -0,0 +1,988 @@
#include "serve.h"
#include "client.h"
#include "nbdtypes.h"
#include "ioutil.h"
#include "sockutil.h"
#include "util.h"
#include "bitset.h"
#include "control.h"
#include "self_pipe.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/un.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
struct server *server_create(struct flexnbd *flexnbd,
char *s_ip_address,
char *s_port,
char *s_file,
int default_deny,
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch, int success)
{
NULLCHECK(flexnbd);
struct server *out;
out = xmalloc(sizeof(struct server));
out->flexnbd = flexnbd;
out->success = success;
out->max_nbd_clients = max_nbd_clients;
out->use_killswitch = use_killswitch;
server_allow_new_clients(out);
out->nbd_client =
xmalloc(max_nbd_clients * sizeof(struct client_tbl_entry));
out->tcp_backlog = 10; /* does this need to be settable? */
FATAL_IF_NULL(s_ip_address, "No IP address supplied");
FATAL_IF_NULL(s_port, "No port number supplied");
FATAL_IF_NULL(s_file, "No filename supplied");
NULLCHECK(s_ip_address);
FATAL_IF_ZERO(parse_ip_to_sockaddr
(&out->bind_to.generic, s_ip_address),
"Couldn't parse server address '%s' (use 0 if "
"you want to bind to all IPs)", s_ip_address);
out->acl = acl_create(acl_entries, s_acl_entries, default_deny);
if (out->acl && out->acl->len != acl_entries) {
fatal("Bad ACL entry '%s'", s_acl_entries[out->acl->len]);
}
parse_port(s_port, &out->bind_to.v4);
out->filename = s_file;
out->l_acl = flexthread_mutex_create();
out->l_start_mirror = flexthread_mutex_create();
out->mirror_can_start = 1;
out->close_signal = self_pipe_create();
out->acl_updated_signal = self_pipe_create();
NULLCHECK(out->close_signal);
NULLCHECK(out->acl_updated_signal);
log_context = s_file;
return out;
}
void server_destroy(struct server *serve)
{
self_pipe_destroy(serve->acl_updated_signal);
serve->acl_updated_signal = NULL;
self_pipe_destroy(serve->close_signal);
serve->close_signal = NULL;
flexthread_mutex_destroy(serve->l_start_mirror);
flexthread_mutex_destroy(serve->l_acl);
if (serve->acl) {
acl_destroy(serve->acl);
serve->acl = NULL;
}
free(serve->nbd_client);
free(serve);
}
void server_unlink(struct server *serve)
{
NULLCHECK(serve);
NULLCHECK(serve->filename);
FATAL_IF_NEGATIVE(unlink(serve->filename),
"Failed to unlink %s: %s",
serve->filename, strerror(errno));
}
#define SERVER_LOCK( s, f, msg ) \
do { NULLCHECK( s ); \
FATAL_IF( 0 != flexthread_mutex_lock( s->f ), msg ); } while (0)
#define SERVER_UNLOCK( s, f, msg ) \
do { NULLCHECK( s ); \
FATAL_IF( 0 != flexthread_mutex_unlock( s->f ), msg ); } while (0)
void server_lock_acl(struct server *serve)
{
debug("ACL locking");
SERVER_LOCK(serve, l_acl, "Problem with ACL lock");
}
void server_unlock_acl(struct server *serve)
{
debug("ACL unlocking");
SERVER_UNLOCK(serve, l_acl, "Problem with ACL unlock");
}
int server_acl_locked(struct server *serve)
{
NULLCHECK(serve);
return flexthread_mutex_held(serve->l_acl);
}
void server_lock_start_mirror(struct server *serve)
{
debug("Mirror start locking");
SERVER_LOCK(serve, l_start_mirror, "Problem with start mirror lock");
}
void server_unlock_start_mirror(struct server *serve)
{
debug("Mirror start unlocking");
SERVER_UNLOCK(serve, l_start_mirror,
"Problem with start mirror unlock");
}
int server_start_mirror_locked(struct server *serve)
{
NULLCHECK(serve);
return flexthread_mutex_held(serve->l_start_mirror);
}
/** Return the actual port the server bound to. This is used because we
* are allowed to pass "0" on the command-line.
*/
int server_port(struct server *server)
{
NULLCHECK(server);
union mysockaddr addr;
socklen_t len = sizeof(addr.v4);
if (getsockname(server->server_fd, &addr.v4, &len) < 0) {
fatal("Failed to get the port number.");
}
return be16toh(addr.v4.sin_port);
}
/** Prepares a listening socket for the NBD server, binding etc. */
void serve_open_server_socket(struct server *params)
{
NULLCHECK(params);
params->server_fd =
socket(params->bind_to.generic.sa_family ==
AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(params->server_fd, "Couldn't create server socket");
/* We need SO_REUSEADDR so that when we switch from listening to
* serving we don't have to change address if we don't want to.
*
* If this fails, it's not necessarily bad in principle, but at
* this point in the code we can't tell if it's going to be a
* problem. It's also indicative of something odd going on, so
* we barf.
*/
FATAL_IF_NEGATIVE(sock_set_reuseaddr(params->server_fd, 1),
"Couldn't set SO_REUSEADDR");
/* TCP_NODELAY makes everything not be slow. If we can't set
* this, again, there's something odd going on which we don't
* understand.
*/
FATAL_IF_NEGATIVE(sock_set_tcp_nodelay(params->server_fd, 1),
"Couldn't set TCP_NODELAY");
/* If we can't bind, presumably that's because someone else is
* squatting on our ip/port combo, or the ip isn't yet
* configured. Ideally we want to retry this. */
FATAL_UNLESS_ZERO(sock_try_bind
(params->server_fd, &params->bind_to.generic),
SHOW_ERRNO("Failed to bind() socket")
);
FATAL_IF_NEGATIVE(listen(params->server_fd, params->tcp_backlog),
"Couldn't listen on server socket");
}
int tryjoin_client_thread(struct client_tbl_entry *entry,
int (*joinfunc) (pthread_t, void **))
{
NULLCHECK(entry);
NULLCHECK(joinfunc);
int was_closed = 0;
void *status = NULL;
if (entry->thread != 0) {
char s_client_address[128];
sockaddr_address_string(&entry->address.generic,
&s_client_address[0], 128);
debug("%s(%p,...)",
joinfunc == pthread_join ? "joining" : "tryjoining",
entry->thread);
int join_errno = joinfunc(entry->thread, &status);
/* join_errno can legitimately be ESRCH if the thread is
* already dead, but the client still needs tidying up. */
if (join_errno != 0 && !entry->client->stopped) {
debug("join_errno was %s, stopped was %d",
strerror(join_errno), entry->client->stopped);
FATAL_UNLESS(join_errno == EBUSY,
"Problem with joining thread %p: %s",
entry->thread, strerror(join_errno));
} else if (join_errno == 0) {
debug("nbd thread %016x exited (%s) with status %ld",
entry->thread, s_client_address, (uintptr_t) status);
client_destroy(entry->client);
entry->client = NULL;
entry->thread = 0;
was_closed = 1;
}
}
return was_closed;
}
/**
* Check to see if a client thread has finished, and if so, tidy up
* after it.
* Returns 1 if the thread was cleaned up and the slot freed, 0
* otherwise.
*
* It's important that client_destroy gets called in the same thread
* which signals the client threads to stop. This avoids the
* possibility of sending a stop signal via a signal which has already
* been destroyed. However, it means that stopped client threads,
* including their signal pipes, won't be cleaned up until the next new
* client connection attempt.
*/
int cleanup_client_thread(struct client_tbl_entry *entry)
{
return tryjoin_client_thread(entry, pthread_tryjoin_np);
}
void cleanup_client_threads(struct client_tbl_entry *entries,
size_t entries_len)
{
size_t i;
for (i = 0; i < entries_len; i++) {
cleanup_client_thread(&entries[i]);
}
}
/**
* Join a client thread after having sent a stop signal to it.
* This function will not return until pthread_join has returned, so
* ensures that the client thread is dead.
*/
int join_client_thread(struct client_tbl_entry *entry)
{
return tryjoin_client_thread(entry, pthread_join);
}
/** We can only accommodate MAX_NBD_CLIENTS connections at once. This function
* goes through the current list, waits for any threads that have finished
* and returns the next slot free (or -1 if there are none).
*/
int cleanup_and_find_client_slot(struct server *params)
{
NULLCHECK(params);
int slot = -1, i;
cleanup_client_threads(params->nbd_client, params->max_nbd_clients);
for (i = 0; i < params->max_nbd_clients; i++) {
if (params->nbd_client[i].thread == 0 && slot == -1) {
slot = i;
break;
}
}
return slot;
}
int server_count_clients(struct server *params)
{
NULLCHECK(params);
int i, count = 0;
cleanup_client_threads(params->nbd_client, params->max_nbd_clients);
for (i = 0; i < params->max_nbd_clients; i++) {
if (params->nbd_client[i].thread != 0) {
count++;
}
}
return count;
}
/** Check whether the address client_address is allowed or not according
* to the current acl. If params->acl is NULL, the result will be 1,
* otherwise it will be the result of acl_includes().
*/
int server_acl_accepts(struct server *params,
union mysockaddr *client_address)
{
NULLCHECK(params);
NULLCHECK(client_address);
struct acl *acl;
int accepted;
server_lock_acl(params);
{
acl = params->acl;
accepted = acl ? acl_includes(acl, client_address) : 1;
}
server_unlock_acl(params);
return accepted;
}
int server_should_accept_client(struct server *params,
union mysockaddr *client_address,
char *s_client_address,
size_t s_client_address_len)
{
NULLCHECK(params);
NULLCHECK(client_address);
NULLCHECK(s_client_address);
const char *result =
sockaddr_address_string(&client_address->generic, s_client_address,
s_client_address_len);
if (NULL == result) {
warn("Rejecting client %s: Bad client_address", s_client_address);
return 0;
}
if (!server_acl_accepts(params, client_address)) {
warn("Rejecting client %s: Access control error",
s_client_address);
debug("We %s have an acl, and default_deny is %s",
(params->acl ? "do" : "do not"),
(params->acl->default_deny ? "true" : "false"));
return 0;
}
return 1;
}
int spawn_client_thread(struct client *client_params,
pthread_t * out_thread)
{
int result =
pthread_create(out_thread, NULL, client_serve, client_params);
return result;
}
/** Dispatch function for accepting an NBD connection and starting a thread
* to handle it. Rejects the connection if there is an ACL, and the far end's
* address doesn't match, or if there are too many clients already connected.
*/
void accept_nbd_client(struct server *params,
int client_fd, union mysockaddr *client_address)
{
NULLCHECK(params);
NULLCHECK(client_address);
struct client *client_params;
int slot;
char s_client_address[64] = { 0 };
FATAL_IF_NEGATIVE(sock_set_keepalive_params
(client_fd, CLIENT_KEEPALIVE_TIME,
CLIENT_KEEPALIVE_INTVL, CLIENT_KEEPALIVE_PROBES),
"Error setting keepalive parameters on client socket fd %d",
client_fd);
if (!server_should_accept_client
(params, client_address, s_client_address, 64)) {
FATAL_IF_NEGATIVE(close(client_fd),
"Error closing client socket fd %d", client_fd);
debug("Closed client socket fd %d", client_fd);
return;
}
slot = cleanup_and_find_client_slot(params);
if (slot < 0) {
warn("too many clients to accept connection");
FATAL_IF_NEGATIVE(close(client_fd),
"Error closing client socket fd %d", client_fd);
debug("Closed client socket fd %d", client_fd);
return;
}
info("Client %s accepted on fd %d.", s_client_address, client_fd);
client_params = client_create(params, client_fd);
params->nbd_client[slot].client = client_params;
memcpy(&params->nbd_client[slot].address, client_address,
sizeof(union mysockaddr));
pthread_t *thread = &params->nbd_client[slot].thread;
if (0 != spawn_client_thread(client_params, thread)) {
debug("Thread creation problem.");
client_destroy(client_params);
FATAL_IF_NEGATIVE(close(client_fd),
"Error closing client socket fd %d", client_fd);
debug("Closed client socket fd %d", client_fd);
return;
}
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread,
s_client_address);
}
void server_audit_clients(struct server *serve)
{
NULLCHECK(serve);
int i;
struct client_tbl_entry *entry;
/* There's an apparent race here. If the acl updates while
* we're traversing the nbd_clients array, the earlier entries
* won't have been audited against the later acl. This isn't a
* problem though, because in order to update the acl
* server_replace_acl must have been called, so the
* server_accept loop will see a second acl_updated signal as
* soon as it hits select, and a second audit will be run.
*/
for (i = 0; i < serve->max_nbd_clients; i++) {
entry = &serve->nbd_client[i];
if (0 == entry->thread) {
continue;
}
if (server_acl_accepts(serve, &entry->address)) {
continue;
}
client_signal_stop(entry->client);
}
}
int server_is_closed(struct server *serve)
{
NULLCHECK(serve);
return fd_is_closed(serve->server_fd);
}
void server_close_clients(struct server *params)
{
NULLCHECK(params);
info("closing all clients");
int i; /* , j; */
struct client_tbl_entry *entry;
for (i = 0; i < params->max_nbd_clients; i++) {
entry = &params->nbd_client[i];
if (entry->thread != 0) {
debug("Stop signaling client %p", entry->client);
client_signal_stop(entry->client);
}
}
/* We don't join the clients here. When we enter the final
* mirror pass, we get the IO lock, then wait for the server_fd
* to close before sending the data, to be sure that no new
* clients can be accepted which might think they've written
* to the disc. However, an existing client thread can be
* waiting for the IO lock already, so if we try to join it
* here, we deadlock.
*
* The client threads will be joined in serve_cleanup.
*
*/
}
/** Replace the current acl with a new one. The old one will be thrown
* away.
*/
void server_replace_acl(struct server *serve, struct acl *new_acl)
{
NULLCHECK(serve);
NULLCHECK(new_acl);
/* We need to lock around updates to the acl in case we try to
* destroy the old acl while checking against it.
*/
server_lock_acl(serve);
{
struct acl *old_acl = serve->acl;
serve->acl = new_acl;
/* We should always have an old_acl, but just in case... */
if (old_acl) {
acl_destroy(old_acl);
}
}
server_unlock_acl(serve);
self_pipe_signal(serve->acl_updated_signal);
}
void server_prevent_mirror_start(struct server *serve)
{
NULLCHECK(serve);
serve->mirror_can_start = 0;
}
void server_allow_mirror_start(struct server *serve)
{
NULLCHECK(serve);
serve->mirror_can_start = 1;
}
/* Only call this with the mirror start lock held */
int server_mirror_can_start(struct server *serve)
{
NULLCHECK(serve);
return serve->mirror_can_start;
}
/* Queries to see if we are currently mirroring. If we are, we need
* to communicate that via the process exit status. because otherwise
* the supervisor will assume the migration completed.
*/
int serve_shutdown_is_graceful(struct server *params)
{
int is_mirroring = 0;
server_lock_start_mirror(params);
{
if (server_is_mirroring(params)) {
is_mirroring = 1;
warn("Stop signal received while mirroring.");
server_prevent_mirror_start(params);
}
}
server_unlock_start_mirror(params);
return !is_mirroring;
}
/** Accept either an NBD or control socket connection, dispatch appropriately */
int server_accept(struct server *params)
{
NULLCHECK(params);
debug("accept loop starting");
union mysockaddr client_address;
fd_set fds;
socklen_t socklen = sizeof(client_address);
/* We select on this fd to receive OS signals (only a few of
* which we're interested in, see flexnbd.c */
int signal_fd = flexnbd_signal_fd(params->flexnbd);
int should_continue = 1;
FD_ZERO(&fds);
FD_SET(params->server_fd, &fds);
if (0 < signal_fd) {
FD_SET(signal_fd, &fds);
}
self_pipe_fd_set(params->close_signal, &fds);
self_pipe_fd_set(params->acl_updated_signal, &fds);
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
SHOW_ERRNO("select() failed")
);
if (self_pipe_fd_isset(params->close_signal, &fds)) {
server_close_clients(params);
should_continue = 0;
}
if (0 < signal_fd && FD_ISSET(signal_fd, &fds)) {
debug("Stop signal received.");
server_close_clients(params);
params->success = params->success
&& serve_shutdown_is_graceful(params);
should_continue = 0;
}
if (self_pipe_fd_isset(params->acl_updated_signal, &fds)) {
self_pipe_signal_clear(params->acl_updated_signal);
server_audit_clients(params);
}
if (FD_ISSET(params->server_fd, &fds)) {
int client_fd =
accept(params->server_fd, &client_address.generic, &socklen);
if (params->allow_new_clients) {
debug("Accepted nbd client socket fd %d", client_fd);
accept_nbd_client(params, client_fd, &client_address);
} else {
debug("New NBD client socket %d not allowed", client_fd);
sock_try_close(client_fd);
}
}
return should_continue;
}
void serve_accept_loop(struct server *params)
{
NULLCHECK(params);
while (server_accept(params));
}
void *build_allocation_map_thread(void *serve_uncast)
{
NULLCHECK(serve_uncast);
struct server *serve = (struct server *) serve_uncast;
NULLCHECK(serve->filename);
NULLCHECK(serve->allocation_map);
int fd = open(serve->filename, O_RDONLY);
FATAL_IF_NEGATIVE(fd, "Couldn't open %s", serve->filename);
if (build_allocation_map(serve->allocation_map, fd)) {
serve->allocation_map_built = 1;
} else {
/* We can operate without it, but we can't free it without a race.
* All that happens if we leave it is that it gradually builds up an
* *incomplete* record of writes. Nobody will use it, as
* allocation_map_built == 0 for the lifetime of the process.
*
* The stream functionality can still be relied on. We don't need to
* worry about mirroring waiting for the allocation map to finish,
* because we already copy every byte at least once. If that changes in
* the future, we'll need to wait for the allocation map to finish or
* fail before we can complete the migration.
*/
serve->allocation_map_not_built = 1;
warn("Didn't build allocation map for %s", serve->filename);
}
close(fd);
return NULL;
}
/** Initialisation function that sets up the initial allocation map, i.e. so
* we know which blocks of the file are allocated.
*/
void serve_init_allocation_map(struct server *params)
{
NULLCHECK(params);
NULLCHECK(params->filename);
int fd = open(params->filename, O_RDONLY);
off64_t size;
FATAL_IF_NEGATIVE(fd, "Couldn't open %s", params->filename);
size = lseek64(fd, 0, SEEK_END);
/* If discs are not in multiples of 512, then odd things happen,
* resulting in reads/writes past the ends of files.
*/
if (size != (size & ~0x1ff)) {
warn("file does not fit into 512-byte sectors; the end of the file will be ignored.");
size &= ~0x1ff;
}
params->size = size;
FATAL_IF_NEGATIVE(size, "Couldn't find size of %s", params->filename);
params->allocation_map =
bitset_alloc(params->size, block_allocation_resolution);
int ok = pthread_create(&params->allocation_map_builder_thread,
NULL,
build_allocation_map_thread,
params);
FATAL_IF_NEGATIVE(ok, "Couldn't create thread");
}
void server_forbid_new_clients(struct server *serve)
{
serve->allow_new_clients = 0;
return;
}
void server_allow_new_clients(struct server *serve)
{
serve->allow_new_clients = 1;
return;
}
void server_join_clients(struct server *serve)
{
int i;
void *status;
for (i = 0; i < serve->max_nbd_clients; i++) {
pthread_t thread_id = serve->nbd_client[i].thread;
if (thread_id != 0) {
debug("joining thread %p", thread_id);
int err = pthread_join(thread_id, &status);
if (0 == err) {
serve->nbd_client[i].thread = 0;
} else {
warn("Error %s (%i) joining thread %p", strerror(err), err,
thread_id);
}
}
}
return;
}
/* Tell the server to close all the things. */
void serve_signal_close(struct server *serve)
{
NULLCHECK(serve);
info("signalling close");
self_pipe_signal(serve->close_signal);
}
/* Block until the server closes the server_fd.
*/
void serve_wait_for_close(struct server *serve)
{
while (!fd_is_closed(serve->server_fd)) {
usleep(10000);
}
}
/* We've just had an DISCONNECT pair, so we need to shut down
* and signal our listener that we can safely take over.
*/
void server_control_arrived(struct server *serve)
{
debug("server_control_arrived");
NULLCHECK(serve);
if (!serve->success) {
serve->success = 1;
serve_signal_close(serve);
}
}
void flexnbd_stop_control(struct flexnbd *flexnbd);
/** Closes sockets, frees memory and waits for all client threads to finish */
void serve_cleanup(struct server *params,
int fatal __attribute__ ((unused)))
{
NULLCHECK(params);
void *status;
info("cleaning up");
if (params->server_fd) {
close(params->server_fd);
}
/* need to stop background build if we're killed very early on */
pthread_cancel(params->allocation_map_builder_thread);
pthread_join(params->allocation_map_builder_thread, &status);
int need_mirror_lock;
need_mirror_lock = !server_start_mirror_locked(params);
if (need_mirror_lock) {
server_lock_start_mirror(params);
}
{
if (server_is_mirroring(params)) {
server_abandon_mirror(params);
}
server_prevent_mirror_start(params);
}
if (need_mirror_lock) {
server_unlock_start_mirror(params);
}
server_join_clients(params);
if (params->allocation_map) {
bitset_free(params->allocation_map);
}
if (server_start_mirror_locked(params)) {
server_unlock_start_mirror(params);
}
if (server_acl_locked(params)) {
server_unlock_acl(params);
}
/* if( params->flexnbd ) { */
/* if ( params->flexnbd->control ) { */
/* flexnbd_stop_control( params->flexnbd ); */
/* } */
/* flexnbd_destroy( params->flexnbd ); */
/* } */
/* server_destroy( params ); */
debug("Cleanup done");
}
int server_is_in_control(struct server *serve)
{
NULLCHECK(serve);
return serve->success;
}
int server_is_mirroring(struct server *serve)
{
NULLCHECK(serve);
return ! !serve->mirror_super;
}
uint64_t server_mirror_bytes_remaining(struct server * serve)
{
if (server_is_mirroring(serve)) {
uint64_t bytes_to_xfer =
bitset_stream_queued_bytes(serve->allocation_map,
BITSET_STREAM_SET) + (serve->size -
serve->
mirror->
offset);
return bytes_to_xfer;
}
return 0;
}
/* Given historic bps measurements and number of bytes left to transfer, give
* an estimate of how many seconds are remaining before the migration is
* complete, assuming no new bytes are written.
*/
uint64_t server_mirror_eta(struct server * serve)
{
if (server_is_mirroring(serve)) {
uint64_t bytes_to_xfer = server_mirror_bytes_remaining(serve);
return bytes_to_xfer / (server_mirror_bps(serve) + 1);
}
return 0;
}
uint64_t server_mirror_bps(struct server * serve)
{
if (server_is_mirroring(serve)) {
uint64_t duration_ms =
monotonic_time_ms() - serve->mirror->migration_started;
return serve->mirror->all_dirty / ((duration_ms / 1000) + 1);
}
return 0;
}
void mirror_super_destroy(struct mirror_super *super);
/* This must only be called with the start_mirror lock held */
void server_abandon_mirror(struct server *serve)
{
NULLCHECK(serve);
if (serve->mirror_super) {
/* FIXME: AWOOGA! RACE!
* We can set abandon_signal after mirror_super has checked it, but
* before the reset. However, mirror_reset doesn't clear abandon_signal
* so it'll just terminate early on the next pass. */
ERROR_UNLESS(self_pipe_signal(serve->mirror->abandon_signal),
"Failed to signal abandon to mirror");
pthread_t tid = serve->mirror_super->thread;
pthread_join(tid, NULL);
debug("Mirror thread %p pthread_join returned", tid);
server_allow_mirror_start(serve);
mirror_super_destroy(serve->mirror_super);
serve->mirror = NULL;
serve->mirror_super = NULL;
debug("Mirror supervisor done.");
}
}
int server_default_deny(struct server *serve)
{
NULLCHECK(serve);
return acl_default_deny(serve->acl);
}
/** Full lifecycle of the server */
int do_serve(struct server *params, struct self_pipe *open_signal)
{
NULLCHECK(params);
int success;
error_set_handler((cleanup_handler *) serve_cleanup, params);
serve_open_server_socket(params);
/* Only signal that we are open for business once the server
socket is open */
if (NULL != open_signal) {
self_pipe_signal(open_signal);
}
serve_init_allocation_map(params);
serve_accept_loop(params);
success = params->success;
serve_cleanup(params, 0);
return success;
}

167
src/server/serve.h Normal file
View File

@@ -0,0 +1,167 @@
#ifndef SERVE_H
#define SERVE_H
#include <sys/types.h>
#include <unistd.h>
#include <signal.h> /* for sig_atomic_t */
#include "flexnbd.h"
#include "parse.h"
#include "acl.h"
static const int block_allocation_resolution = 4096; //128<<10;
struct client_tbl_entry {
pthread_t thread;
union mysockaddr address;
struct client *client;
};
#define MAX_NBD_CLIENTS 16
#define CLIENT_KEEPALIVE_TIME 30
#define CLIENT_KEEPALIVE_INTVL 10
#define CLIENT_KEEPALIVE_PROBES 3
struct server {
/* The flexnbd wrapper this server is attached to */
struct flexnbd *flexnbd;
/** address/port to bind to */
union mysockaddr bind_to;
/** (static) file name to serve */
char *filename;
/** TCP backlog for listen() */
int tcp_backlog;
/** (static) file name of UNIX control socket (or NULL if none) */
char *control_socket_name;
/** size of file */
uint64_t size;
/** to interrupt accept loop and clients, write() to close_signal[1] */
struct self_pipe *close_signal;
/** access control list */
struct acl *acl;
/** acl_updated_signal will be signalled after the acl struct
* has been replaced
*/
struct self_pipe *acl_updated_signal;
/* Claimed around any updates to the ACL. */
struct flexthread_mutex *l_acl;
/* Claimed around starting a mirror so that it doesn't race with
* shutting down on a SIGTERM. */
struct flexthread_mutex *l_start_mirror;
struct mirror *mirror;
struct mirror_super *mirror_super;
/* This is used to stop the mirror from starting after we
* receive a SIGTERM */
int mirror_can_start;
int server_fd;
int control_fd;
/* the allocation_map keeps track of which blocks in the backing file
* have been allocated, or part-allocated on disc, with unallocated
* blocks presumed to contain zeroes (i.e. represented as sparse files
* by the filesystem). We can use this information when receiving
* incoming writes, and avoid writing zeroes to unallocated sections
* of the file which would needlessly increase disc usage. This
* bitmap will start at all-zeroes for an empty file, and tend towards
* all-ones as the file is written to (i.e. we assume that allocated
* blocks can never become unallocated again, as is the case with ext3
* at least).
*/
struct bitset *allocation_map;
/* when starting up, this thread builds the allocation_map */
pthread_t allocation_map_builder_thread;
/* when the thread has finished, it sets this to 1 */
volatile sig_atomic_t allocation_map_built;
volatile sig_atomic_t allocation_map_not_built;
int max_nbd_clients;
struct client_tbl_entry *nbd_client;
/** Should clients use the killswitch? */
int use_killswitch;
/** If this isn't set, newly accepted clients will be closed immediately */
int allow_new_clients;
/* Marker for whether this server has control over the data in
* the file, or if we're waiting to receive it from an inbound
* migration which hasn't yet finished.
*
* It's the value which controls the exit status of a serve or
* listen process.
*/
int success;
};
struct server *server_create(struct flexnbd *flexnbd,
char *s_ip_address,
char *s_port,
char *s_file,
int default_deny,
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch, int success);
void server_destroy(struct server *);
int server_is_closed(struct server *serve);
void serve_signal_close(struct server *serve);
void serve_wait_for_close(struct server *serve);
void server_replace_acl(struct server *serve, struct acl *acl);
void server_control_arrived(struct server *serve);
int server_is_in_control(struct server *serve);
int server_default_deny(struct server *serve);
int server_acl_locked(struct server *serve);
void server_lock_acl(struct server *serve);
void server_unlock_acl(struct server *serve);
void server_lock_start_mirror(struct server *serve);
void server_unlock_start_mirror(struct server *serve);
int server_is_mirroring(struct server *serve);
uint64_t server_mirror_bytes_remaining(struct server *serve);
uint64_t server_mirror_eta(struct server *serve);
uint64_t server_mirror_bps(struct server *serve);
void server_abandon_mirror(struct server *serve);
void server_prevent_mirror_start(struct server *serve);
void server_allow_mirror_start(struct server *serve);
int server_mirror_can_start(struct server *serve);
/* These three functions are used by mirror around the final pass, to close
* existing clients and prevent new ones from being around
*/
void server_forbid_new_clients(struct server *serve);
void server_close_clients(struct server *serve);
void server_join_clients(struct server *serve);
void server_allow_new_clients(struct server *serve);
/* Returns a count (ish) of the number of currently-running client threads */
int server_count_clients(struct server *params);
void server_unlink(struct server *serve);
int do_serve(struct server *, struct self_pipe *);
struct mode_readwrite_params {
union mysockaddr connect_to;
union mysockaddr connect_from;
uint64_t from;
uint32_t len;
int data_fd;
int client;
};
#endif

82
src/server/status.c Normal file
View File

@@ -0,0 +1,82 @@
#include "status.h"
#include "serve.h"
#include "util.h"
struct status *status_create(struct server *serve)
{
NULLCHECK(serve);
struct status *status;
status = xmalloc(sizeof(struct status));
status->pid = getpid();
status->size = serve->size;
status->has_control = serve->success;
status->clients_allowed = serve->allow_new_clients;
status->num_clients = server_count_clients(serve);
server_lock_start_mirror(serve);
status->is_mirroring = NULL != serve->mirror;
if (status->is_mirroring) {
status->migration_duration = monotonic_time_ms();
if ((serve->mirror->migration_started) <
status->migration_duration) {
status->migration_duration -= serve->mirror->migration_started;
} else {
status->migration_duration = 0;
}
status->migration_duration /= 1000;
status->migration_speed = server_mirror_bps(serve);
status->migration_speed_limit =
serve->mirror->max_bytes_per_second;
status->migration_seconds_left = server_mirror_eta(serve);
status->migration_bytes_left =
server_mirror_bytes_remaining(serve);
}
server_unlock_start_mirror(serve);
return status;
}
#define BOOL_S(var) (var ? "true" : "false" )
#define PRINT_BOOL( var ) \
do{dprintf( fd, #var "=%s ", BOOL_S( status->var ) );}while(0)
#define PRINT_INT( var ) \
do{dprintf( fd, #var "=%d ", status->var );}while(0)
#define PRINT_UINT64( var ) \
do{dprintf( fd, #var "=%"PRIu64" ", status->var );}while(0)
int status_write(struct status *status, int fd)
{
PRINT_INT(pid);
PRINT_UINT64(size);
PRINT_BOOL(is_mirroring);
PRINT_BOOL(clients_allowed);
PRINT_INT(num_clients);
PRINT_BOOL(has_control);
if (status->is_mirroring) {
PRINT_UINT64(migration_speed);
PRINT_UINT64(migration_duration);
PRINT_UINT64(migration_seconds_left);
PRINT_UINT64(migration_bytes_left);
if (status->migration_speed_limit < UINT64_MAX) {
PRINT_UINT64(migration_speed_limit);
};
}
dprintf(fd, "\n");
return 1;
}
void status_destroy(struct status *status)
{
NULLCHECK(status);
free(status);
}

103
src/server/status.h Normal file
View File

@@ -0,0 +1,103 @@
#ifndef STATUS_H
#define STATUS_H
/* Status reports
*
* The status will be reported by writing to a file descriptor. The
* status report will be on a single line. The status format will be:
*
* A=B C=D
*
* That is, a space-separated list of label,value pairs, each pair
* separated by an '=' character. Neither ' ' nor '=' will appear in
* either labels or values.
*
* Boolean values will appear as the strings "true" and "false".
*
* The following status fields are defined:
*
* pid:
* The current process ID.
*
* size:
* The size of the backing file being served, in bytes.
*
* has_control:
* This will be false when the server is listening for an incoming
* migration. It will switch to true when the end-of-migration
* handshake is successfully completed.
* If the server is started in "serve" mode, this will never be
* false.
*
* clients_allowed:
* This will be false if the server is not currently allowing new
* connections, for instance, if we're in the migration endgame.
*
* num_clients:
* This tells us how many clients are currently running. If we're in the
* migration endgame, it should be 0
*
* is_migrating:
* This will be false when the server is started in either "listen"
* or "serve" mode. It will become true when a server in "serve"
* mode starts a migration, and will become false again when the
* migration terminates, successfully or not.
* If the server is currently in "listen" mode, this will never be
* true.
*
*
* If is_migrating is true, then a number of other attributes may appear,
* relating to the progress of the migration.
*
* migration_duration:
* How long the migration has been running for, in ms.
*
* migration_speed:
* Network transfer speed, in bytes/second. This only takes dirty bytes
* into account.
*
* migration_speed_limit:
* If set, the speed we're going to try to limit the migration to.
*
* migration_seconds_left:
* Our current best estimate of how many seconds are left before the migration
* migration is finished.
*
* migration_bytes_left:
* The number of bytes remaining to migrate.
*/
#include "serve.h"
#include <sys/types.h>
#include <unistd.h>
struct status {
pid_t pid;
uint64_t size;
int has_control;
int clients_allowed;
int num_clients;
int is_mirroring;
uint64_t migration_duration;
uint64_t migration_speed;
uint64_t migration_speed_limit;
uint64_t migration_seconds_left;
uint64_t migration_bytes_left;
};
/** Create a status object for the given server. */
struct status *status_create(struct server *);
/** Output the given status object to the given file descriptot */
int status_write(struct status *, int fd);
/** Free the status object */
void status_destroy(struct status *);
#endif

View File

@@ -0,0 +1,13 @@
{
avoid_glibc_bug_do_lookup
Memcheck:Addr8
fun:do_lookup_x
obj:*
fun:_dl_lookup_symbol_x
}
{
avoid_glibc_bug_check_match
Memcheck:Addr8
fun:check_match.12149
}

View File

@@ -0,0 +1,150 @@
require 'flexnbd'
require 'file_writer'
class Environment
attr_reader(:blocksize, :filename1, :filename2, :ip,
:port1, :port2, :nbd1, :nbd2, :file1, :file2)
def initialize
@blocksize = 1024
@filename1 = "/tmp/.flexnbd.test.#{$PROCESS_ID}.#{Time.now.to_i}.1"
@filename2 = "/tmp/.flexnbd.test.#{$PROCESS_ID}.#{Time.now.to_i}.2"
@ip = '127.0.0.1'
@available_ports = [*40_000..41_000] - listening_ports
@port1 = @available_ports.shift
@port2 = @available_ports.shift
@nbd1 = FlexNBD::FlexNBD.new('../../build/flexnbd', @ip, @port1)
@nbd2 = FlexNBD::FlexNBD.new('../../build/flexnbd', @ip, @port2)
@fake_pid = nil
end
def blocksize=(b)
raise RuntimeError, "Unable to change blocksize after files have been opened" if @file1 or @file2
@blocksize = b
end
def prefetch_proxy!
@nbd1.prefetch_proxy = true
@nbd2.prefetch_proxy = true
end
def proxy1(port = @port2)
@nbd1.proxy(@ip, port)
end
def proxy2(port = @port1)
@nbd2.proxy(@ip, port)
end
def serve1(*acl)
@nbd1.serve(@filename1, *acl)
end
def serve2(*acl)
@nbd2.serve(@filename2, *acl)
end
def listen1(*acl)
@nbd1.listen(@filename1, *(acl.empty? ? @acl1 : acl))
end
def listen2(*acl)
@nbd2.listen(@filename2, *acl)
end
def break1
@nbd1.break
end
def acl1(*acl)
@nbd1.acl(*acl)
end
def acl2(*acl)
@nbd2.acl(*acl)
end
def status1
@nbd1.status.first
end
def status2
@nbd2.status.first
end
def mirror12
@nbd1.mirror(@nbd2.ip, @nbd2.port)
end
def mirror12_unchecked
@nbd1.mirror_unchecked(@nbd2.ip, @nbd2.port, nil, nil, 10)
end
def mirror12_unlink
@nbd1.mirror_unlink(@nbd2.ip, @nbd2.port, 2)
end
def write1(data)
@nbd1.write(0, data)
end
def writefile1(data)
@file1 = FileWriter.new(@filename1, @blocksize).write(data)
end
def writefile2(data)
@file2 = FileWriter.new(@filename2, @blocksize).write(data)
end
def truncate1(size)
system "truncate -s #{size} #{@filename1}"
end
def listening_ports
`netstat -ltn`
.split("\n")
.map { |x| x.split(/\s+/) }[2..-1]
.map { |l| l[3].split(':')[-1].to_i }
end
def cleanup
if @fake_pid
begin
Process.waitpid2(@fake_pid)
rescue Errno::ESRCH
end
end
@nbd1.can_die(0)
@nbd1.kill
@nbd2.kill
[@filename1, @filename2].each do |f|
File.unlink(f) if File.exist?(f)
end
end
def run_fake(name, addr, port, sock = nil)
fakedir = File.join(File.dirname(__FILE__), 'fakes')
fakeglob = File.join(fakedir, name) + '*'
fake = Dir[fakeglob].sort.find do |fn|
File.executable?(fn)
end
raise "no fake executable at #{fakeglob}" unless fake
raise 'no addr' unless addr
raise 'no port' unless port
@fake_pid = fork do
exec [fake, addr, port, @nbd1.pid, sock].map(&:to_s).join(' ')
end
sleep(0.5)
end
def fake_reports_success
_, status = Process.waitpid2(@fake_pid)
@fake_pid = nil
status.success?
end
end # class Environment

View File

@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
# Open a server, accept a client, then cancel the migration by issuing
# a break command.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port, src_pid, sock = *ARGV
server = FakeDest.new(addr, port)
client = server.accept
ctrl = UNIXSocket.open(sock)
Process.kill('STOP', src_pid.to_i)
ctrl.write("break\n")
ctrl.close_write
client.write_hello
Process.kill('CONT', src_pid.to_i)
raise 'Unexpected control response' unless
ctrl.read =~ /0: mirror stopped/
client2 = nil
begin
client2 = server.accept('Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
client.close
exit(0)

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# Receive a mirror, and disconnect after sending the entrust reply but
# before it can send the disconnect signal.
#
# This test is currently unused: the sender can't detect that the
# write failed.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port, src_pid = *ARGV
server = FakeDest.new(addr, port)
client = server.accept
client.write_hello
while req = client.read_request; req[:type] == 1
client.read_data(req[:len])
client.write_reply(req[:handle])
end
system "kill -STOP #{src_pid}"
client.write_reply(req[:handle])
client.close
system "kill -CONT #{src_pid}"
sleep(0.25)
client2 = server.accept('Timed out waiting for a reconnection')
client2.close
server.close
warn 'done'
exit(0)

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
# Wait for a sender connection, send a correct hello, then disconnect.
# Simulate a server which crashes after sending the hello. We then
# reopen the server socket to check that the sender retries: since the
# command-line has gone away, and can't feed an error back to the
# user, we have to keep trying.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept('Timed out waiting for a connection')
client.write_hello
client.close
new_client = server.accept('Timed out waiting for a reconnection')
new_client.close
server.close
exit 0

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env ruby
# Wait for a sender connection, send a correct hello, wait for a write
# request, then disconnect. Simulate a server which crashes after
# receiving the write, and before it can send a reply. We then reopen
# the server socket to check that the sender retries: since the
# command-line has gone away, and can't feed an error back to the
# user, we have to keep trying.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept('Timed out waiting for a connection')
client.write_hello
client.read_request
client.close
new_client = server.accept('Timed out waiting for a reconnection')
new_client.close
server.close
exit 0

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
# Open a server, accept a client, then we expect a single write
# followed by an entrust. However, we disconnect after the write so
# the entrust will fail. We don't expect a reconnection: the sender
# can't reliably spot a failed send.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port, src_pid = *ARGV
server = FakeDest.new(addr, port)
client = server.accept
client.write_hello
req = client.read_request
data = client.read_data(req[:len])
Process.kill('STOP', src_pid.to_i)
client.write_reply(req[:handle], 0)
client.close
Process.kill('CONT', src_pid.to_i)
exit(0)

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept
client.write_hello
handle = client.read_request[:handle]
client.write_error(handle)
client2 = server.accept('Timed out waiting for a reconnection')
client.close
client2.close
server.close
exit(0)

View File

@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
# Will open a server, accept a single connection, then sleep for 5
# seconds. After that time, the client should have disconnected,
# which we can can't effectively check.
#
# We also expect the client *not* to reconnect, since it could feed back
# an error.
#
# This allows the test runner to check that the command-line sees the
# right error message after the timeout time.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept("Client didn't make a connection")
# Sleep for one second past the timeout (a bit of slop in case ruby
# doesn't launch things quickly)
sleep(FlexNBD::MS_HELLO_TIME_SECS + 1)
client.close
# Invert the sense of the timeout exception, since we *don't* want a
# connection attempt
begin
server.accept('Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
server.close

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# Open a socket, say hello, receive a write, then sleep for >
# MS_REQUEST_LIMIT_SECS seconds. This should tell the source that the
# write has gone MIA, and we expect a reconnect.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client1 = server.accept(server)
client1.write_hello
client1.read_request
t = Thread.start do
client2 = server.accept('Timed out waiting for a reconnection',
FlexNBD::MS_REQUEST_LIMIT_SECS + 2)
client2.close
end
sleep_time = if ENV.key?('FLEXNBD_MS_REQUEST_LIMIT_SECS')
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'].to_f
else
FlexNBD::MS_REQUEST_LIMIT_SECS
end
sleep(sleep_time + 2.0)
client1.close
t.join
server.close
exit(0)

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env ruby
# Simulate a destination which sends the wrong magic.
require 'flexnbd/fake_dest'
include FlexNBD
Thread.abort_on_exception
addr, port = *ARGV
server = FakeDest.new(addr, port)
client1 = server.accept
# We don't expect a reconnection attempt.
t = Thread.new do
begin
client2 = server.accept('Timed out waiting for a reconnection',
FlexNBD::MS_RETRY_DELAY_SECS + 1)
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
end
client1.write_hello(magic: :wrong)
t.join
exit 0

View File

@@ -0,0 +1,38 @@
#!/usr/bin/env ruby
# Simulate a server which has a disc of the wrong size attached: send
# a valid NBD hello with a random size, then check that we have see an
# EOF on read.
require 'flexnbd/fake_dest'
include FlexNBD
Thread.abort_on_exception = true
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept
t = Thread.new do
# The sender *should not reconnect.* Since this is a first-pass
# mirror attempt, the user will have been told that the mirror failed,
# so it makes no sense to continue. This means we have to invert the
# sense of the exception.
begin
client2 = server.accept('Timed out waiting for a reconnection',
FlexNBD::MS_RETRY_DELAY_SECS + 1)
client2.close
raise 'Unexpected reconnection.'
rescue Timeout::Error
end
end
client.write_hello(size: :wrong)
t.join
# Now check that the source closed the first socket (yes, this was an
# actual bug)
raise "Didn't close socket" unless client.disconnected?
exit 0

View File

@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
# Accept a connection, then immediately close it. This simulates an ACL rejection.
# We do not expect a reconnection.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
server.accept.close
begin
server.accept
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
server.close
exit(0)

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
# Wait for a sender connection, send a correct hello, then sigterm the
# sender. We expect the sender to exit with status of 6, which is
# enforced in the test.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port, pid = *ARGV
server = FakeDest.new(addr, port)
client = server.accept('Timed out waiting for a connection')
client.write_hello
Process.kill(15, pid.to_i)
client.close
server.close
exit 0

View File

@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
# Accept a connection, write hello, wait for a write request, read the
# data, then write back a reply with a bad magic field. We then
# expect a reconnect.
require 'flexnbd/fake_dest'
include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept
client.write_hello
req = client.read_request
client.read_data(req[:len])
client.write_reply(req[:handle], 0, magic: :wrong)
client2 = server.accept
client.close
client2.close
exit(0)

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
# Connects to the destination server, then immediately disconnects,
# simulating a source crash.
#
# It then connects again, to check that the destination is still
# listening.
require 'flexnbd/fake_source'
include FlexNBD
addr, port = *ARGV
FakeSource.new(addr, port, 'Failed to connect').close
# Sleep to be sure we don't try to connect too soon. That wouldn't
# be a problem for the destination, but it would prevent us from
# determining success or failure here in the case where we try to
# reconnect before the destination has tidied up after the first
# thread went away.
sleep(0.5)
FakeSource.new(addr, port, 'Failed to reconnect').close
exit 0

View File

@@ -0,0 +1,37 @@
#!/usr/bin/env ruby
# Connect, send a migration, entrust then *immediately* disconnect.
# This simulates a client which fails while the client is blocked.
#
# In this situation we expect the destination to quit with an error
# status.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
client.write_write_request(0, 8)
client.write_data('12345678')
# Use system "kill" rather than Process.kill because Process.kill
# doesn't seem to work
system "kill -STOP #{srv_pid}"
client.write_entrust_request
client.close
system "kill -CONT #{srv_pid}"
sleep(0.25)
begin
client2 = FakeSource.new(addr, port, 'Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
exit(0)

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
# Connect, send a migration, entrust, read the reply, then disconnect.
# This simulates a client which fails while the client is blocked.
#
# We expect the destination to quit with an error status.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
client.write_write_request(0, 8)
client.write_data('12345678')
client.write_entrust_request
client.read_response
client.close
sleep(0.25)
begin
client2 = FakeSource.new(addr, port, 'Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
exit(0)

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
# Connect, read the hello, then immediately disconnect. This
# simulates a sender which dislikes something in the hello message - a
# wrong size, for instance.
# After the disconnect, we reconnect to be sure that the destination
# is still alive.
require 'flexnbd/fake_source'
include FlexNBD
addr, port = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting.')
client.read_hello
client.close
sleep(0.2)
FakeSource.new(addr, port, 'Timed out reconnecting.')
exit(0)

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env ruby
# We connect, pause the server, issue a write request, disconnect,
# then cont the server. This ensures that our disconnect happens
# while the server is trying to read the write data.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
system "kill -STOP #{srv_pid}"
client.write_write_request(0, 8)
client.close
system "kill -CONT #{srv_pid}"
# This sleep ensures that we don't return control to the test runner
# too soon, giving the flexnbd process time to fall over if it's going
# to.
sleep(0.25)
# ...and can we reconnect?
client2 = FakeSource.new(addr, port, 'Timed out connecting')
client2.close
exit(0)

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
# We connect, pause the server, issue a write request, send data,
# disconnect, then cont the server. This ensures that our disconnect
# happens before the server can try to write the reply.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
system "kill -STOP #{srv_pid}"
client.write_write_request(0, 8)
client.write_data('12345678')
client.close
system "kill -CONT #{srv_pid}"
# This sleep ensures that we don't return control to the test runner
# too soon, giving the flexnbd process time to fall over if it's going
# to.
sleep(0.25)
# ...and can we reconnect?
client2 = FakeSource.new(addr, port, 'Timed out reconnecting')
client2.close
exit(0)

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# Connect, but get the protocol wrong: don't read the hello, so we
# close and break the sendfile.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid, newaddr, newport = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
client.write_read_request(0, 8)
client.read_raw(4)
client.close
exit(0)

View File

@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
# Connect to the destination, then hang. Connect a second time to the
# destination. This will trigger the destination's thread clearer. We
# can't really see any error state from here, we just try to trigger
# something the test runner can detect.
require 'flexnbd/fake_source'
include FlexNBD
addr, port = *ARGV
client1 = FakeSource.new(addr, port, 'Timed out connecting')
sleep(0.25)
client2 = FakeSource.new(addr, port, 'Timed out connecting a second time')
# This is the expected source crashing after connect
client1.close
# And this is just a tidy-up.
client2.close
exit(0)

View File

@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# We connect from a local address which should be blocked, sleep for a
# bit, then try to read from the socket. We should get an instant EOF
# as we've been cut off by the destination.
require 'timeout'
require 'flexnbd/fake_source'
include FlexNBD
addr, port = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting', '127.0.0.6')
sleep(0.25)
rsp = client.disconnected? ? 0 : 1
client.close
exit(rsp)

View File

@@ -0,0 +1,39 @@
#!/usr/bin/env ruby
# Simulate the hello message going astray, or the source hanging after
# receiving it.
#
# We then connect again, to confirm that the destination is still
# listening for an incoming migration.
addr, port = *ARGV
require 'flexnbd/fake_source'
include FlexNBD
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
# Now we do two things:
# - In the parent process, we sleep for CLIENT_MAX_WAIT_SECS+5, which
# will make the destination give up and close the connection.
# - In the child process, we sleep for CLIENT_MAX_WAIT_SECS+1, which
# should be able to reconnect despite the parent process not having
# closed its end yet.
kidpid = fork do
client.close
new_client = nil
sleep(FlexNBD::CLIENT_MAX_WAIT_SECS + 1)
new_client = FakeSource.new(addr, port, 'Timed out reconnecting.')
new_client.read_hello
exit 0
end
# Sleep for longer than the child, to give the flexnbd process a bit
# of slop
sleep(FlexNBD::CLIENT_MAX_WAIT_SECS + 3)
client.close
_, status = Process.waitpid2(kidpid)
exit status.exitstatus

View File

@@ -0,0 +1,20 @@
#!/usr/bin/env ruby
# Connect to the listener, wait for the hello, then sigterm the
# listener. We expect the listener to exit with a status of 6, which
# is enforced in the test.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, pid = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting.')
client.read_hello
Process.kill('TERM', pid.to_i)
sleep(0.2)
client.close
exit(0)

View File

@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# Successfully send a migration. This test just makes sure that the
# happy path is covered. We expect the destination to quit with a
# success status.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid, newaddr, newport = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
client.send_mirror
sleep(1)
exit(0)

View File

@@ -0,0 +1,30 @@
#!/usr/bin/env ruby
# Connect, read the hello then make a write request with an impossible
# (from,len) pair. We expect an error response, and not to be
# disconnected.
#
# We then expect to be able to issue a successful write: the destination
# has to flush the data in the socket.
require 'flexnbd/fake_source'
include FlexNBD
addr, port = *ARGV
client = FakeSource.new(addr, port, 'Timed out connecting')
hello = client.read_hello
client.write_write_request(hello[:size] + 1, 32, 'myhandle')
client.write_data('1' * 32)
response = client.read_response
raise 'Not an error' if response[:error] == 0
raise 'Wrong handle' unless response[:handle] == 'myhandle'
client.write_write_request(0, 32)
client.write_data('2' * 32)
success_response = client.read_response
raise 'Second write failed' unless success_response[:error] == 0
client.close
exit(0)

View File

@@ -0,0 +1,124 @@
# Noddy test class for writing files to disc in predictable patterns
# in order to test FlexNBD.
#
class FileWriter
def initialize(filename, blocksize)
@fh = File.open(filename, 'w+')
@blocksize = blocksize
@pattern = ''
end
def size
@blocksize * @pattern.split('').size
end
# We write in fixed block sizes, given by "blocksize"
# _ means skip a block
# 0 means write a block full of zeroes
# f means write a block with the file offset packed every 4 bytes
#
def write(data)
@pattern += data
data.split('').each do |code|
if code == '_'
@fh.seek(@blocksize, IO::SEEK_CUR)
else
@fh.write(data(code))
end
end
@fh.flush
self
end
# Returns what the data ought to be at the given offset and length
#
def read_original(off, len)
patterns = @pattern.split('')
patterns.zip((0...patterns.length).to_a)
.map do |blk, blk_off|
data(blk, blk_off)
end.join[off...(off + len)]
end
# Read what's actually in the file
#
def read(off, len)
@fh.seek(off, IO::SEEK_SET)
@fh.read(len)
end
def untouched?(offset, len)
read(offset, len) == read_original(offset, len)
end
def close
@fh.close
nil
end
protected
def data(code, at = @fh.tell)
case code
when '0', '_'
"\0" * @blocksize
when 'X'
'X' * @blocksize
when 'f'
r = ''
(@blocksize / 4).times do
r += [at].pack('I')
at += 4
end
r
else
raise "Unknown character '#{block}'"
end
end
end
if $PROGRAM_NAME == __FILE__
require 'tempfile'
require 'test/unit'
class FileWriterTest < Test::Unit::TestCase
def test_read_original_zeros
Tempfile.open('test_read_original_zeros') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4096)
file.write('0')
assert_equal file.read(0, 4096), file.read_original(0, 4096)
assert(file.untouched?(0, 4096), 'Untouched file was touched.')
end
end
def test_read_original_offsets
Tempfile.open('test_read_original_offsets') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4096)
file.write('f')
assert_equal file.read(0, 4096), file.read_original(0, 4096)
assert(file.untouched?(0, 4096), 'Untouched file was touched.')
end
end
def test_file_size
Tempfile.open('test_file_size') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4096)
file.write('f')
assert_equal 4096, File.stat(tempfile.path).size
end
end
def test_read_original_size
Tempfile.open('test_read_original_offsets') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4)
file.write('f' * 4)
assert_equal 4, file.read_original(0, 4).length
end
end
end
end

539
tests/acceptance/flexnbd.rb Normal file
View File

@@ -0,0 +1,539 @@
require 'socket'
require 'thread'
require 'open3'
require 'timeout'
require 'rexml/document'
require 'rexml/streamlistener'
require 'English'
Thread.abort_on_exception = true
class Executor
attr_reader :pid
def run(cmd)
@pid = fork { exec cmd }
end
end # class Executor
class ValgrindExecutor
attr_reader :pid
def run(cmd)
@pid = fork { exec "valgrind --track-origins=yes --suppressions=custom.supp #{cmd}" }
end
end # class ValgrindExecutor
class ValgrindKillingExecutor
attr_reader :pid
class Error
attr_accessor :what, :kind, :pid
attr_reader :backtrace
def initialize
@backtrace = []
@what = ''
@kind = ''
@pid = ''
end
def add_frame
@backtrace << {}
end
def add_fn(fn)
@backtrace.last[:fn] = fn
end
def add_file(file)
@backtrace.last[:file] = file
end
def add_line(line)
@backtrace.last[:line] = line
end
def to_s
([@what + " (#{@kind}) in #{@pid}"] + @backtrace.map { |h| "#{h[:file]}:#{h[:line]} #{h[:fn]}" }).join("\n")
end
end # class Error
class ErrorListener
include REXML::StreamListener
def initialize(killer)
@killer = killer
@error = Error.new
@found = false
end
def text(text)
@text = text
end
def tag_start(tag, _attrs)
case tag.to_s
when 'error'
@found = true
when 'frame'
@error.add_frame
end
end
def tag_end(tag)
case tag.to_s
when 'what'
@error.what = @text if @found
@text = ''
when 'kind'
@error.kind = @text if @found
when 'file'
@error.add_file(@text) if @found
when 'fn'
@error.add_fn(@text) if @found
when 'line'
@error.add_line(@text) if @found
when 'error', 'stack'
@killer.call(@error) if @found
when 'pid'
@error.pid = @text
end
end
end # class ErrorListener
class DebugErrorListener < ErrorListener
def text(txt)
print txt
super(txt)
end
def tag_start(tag, attrs)
print "<#{tag}>"
super(tag, attrs)
end
def tag_end(tag)
print "</#{tag}>"
super(tag)
end
end
def initialize
@pid = nil
end
def run(cmd)
@io_r, io_w = IO.pipe
@pid = fork { exec("valgrind --suppressions=custom.supp --xml=yes --xml-fd=#{io_w.fileno} " + cmd) }
launch_watch_thread(@pid, @io_r)
@pid
end
def call(err)
warn '*' * 72
warn '* Valgrind error spotted:'
warn err.to_s.split("\n").map { |s| " #{s}" }
warn '*' * 72
Process.kill('KILL', @pid)
exit(1)
end
private
def pick_listener
ENV['DEBUG'] ? DebugErrorListener : ErrorListener
end
def launch_watch_thread(_pid, io_r)
Thread.start do
io_source = REXML::IOSource.new(io_r)
listener = pick_listener.new(self)
REXML::Document.parse_stream(io_source, listener)
end
end
end # class ValgrindExecutor
module FlexNBD
# Noddy test class to exercise FlexNBD from the outside for testing.
#
class FlexNBD
attr_reader :bin, :ctrl, :pid, :ip, :port
class << self
def counter
Dir['tmp/*'].select { |f| File.file?(f) }.length + 1
end
end
def pick_executor
kls = if ENV['VALGRIND']
if ENV['VALGRIND'] =~ /kill/
ValgrindKillingExecutor
else
ValgrindExecutor
end
else
Executor
end
end
def build_debug_opt
if @do_debug
'--verbose'
else
'--quiet'
end
end
attr_accessor :prefetch_proxy
def initialize(bin, ip, port)
@bin = bin
@do_debug = ENV['DEBUG']
@debug = build_debug_opt
raise "#{bin} not executable" unless File.executable?(bin)
@executor = pick_executor.new
@ctrl = "/tmp/.flexnbd.ctrl.#{Time.now.to_i}.#{rand}"
@ip = ip
@port = port
@pid = @wait_thread = nil
@kill = []
@prefetch_proxy = false
end
def debug?
!!@do_debug
end
def debug(msg)
warn msg if debug?
end
def serve_cmd(file, acl)
"#{bin} serve "\
"--addr #{ip} "\
"--port #{port} "\
"--file #{file} "\
"--sock #{ctrl} "\
"#{@debug} "\
"#{acl.join(' ')}"
end
def listen_cmd(file, acl)
"#{bin} listen "\
"--addr #{ip} "\
"--port #{port} "\
"--file #{file} "\
"--sock #{ctrl} "\
"#{@debug} "\
"#{acl.join(' ')}"
end
def proxy_cmd(connect_ip, connect_port)
"#{bin}-proxy "\
"--addr #{ip} "\
"--port #{port} "\
"--conn-addr #{connect_ip} "\
"--conn-port #{connect_port} "\
"#{prefetch_proxy ? '--cache ' : ''}"\
"#{@debug}"
end
def read_cmd(offset, length)
"#{bin} read "\
"--addr #{ip} "\
"--port #{port} "\
"--from #{offset} "\
"#{@debug} "\
"--size #{length}"
end
def write_cmd(offset, data)
"#{bin} write "\
"--addr #{ip} "\
"--port #{port} "\
"--from #{offset} "\
"#{@debug} "\
"--size #{data.length}"
end
def base_mirror_opts(dest_ip, dest_port)
"--addr #{dest_ip} "\
"--port #{dest_port} "\
"--sock #{ctrl} "\
end
def unlink_mirror_opts(dest_ip, dest_port)
"#{base_mirror_opts(dest_ip, dest_port)} "\
'--unlink '
end
def base_mirror_cmd(opts)
"#{@bin} mirror "\
"#{opts} "\
"#{@debug}"
end
def mirror_cmd(dest_ip, dest_port)
base_mirror_cmd(base_mirror_opts(dest_ip, dest_port))
end
def mirror_unlink_cmd(dest_ip, dest_port)
base_mirror_cmd(unlink_mirror_opts(dest_ip, dest_port))
end
def break_cmd
"#{@bin} break "\
"--sock #{ctrl} "\
"#{@debug}"
end
def status_cmd
"#{@bin} status "\
"--sock #{ctrl} "\
"#{@debug}"
end
def acl_cmd(*acl)
"#{@bin} acl " \
"--sock #{ctrl} "\
"#{@debug} "\
"#{acl.join ' '}"
end
def run_serve_cmd(cmd)
File.unlink(ctrl) if File.exist?(ctrl)
debug(cmd)
@pid = @executor.run(cmd)
until File.socket?(ctrl)
pid, status = Process.wait2(@pid, Process::WNOHANG)
raise "server did not start (#{cmd})" if pid
sleep 0.1
end
start_wait_thread(@pid)
at_exit { kill }
end
private :run_serve_cmd
def serve(file, *acl)
cmd = serve_cmd(file, acl)
run_serve_cmd(cmd)
sleep(0.2) until File.exist?(ctrl)
end
def listen(file, *acl)
run_serve_cmd(listen_cmd(file, acl))
end
def tcp_server_open?
# raises if the other side doesn't accept()
sock = begin
TCPSocket.new(ip, port)
rescue StandardError
nil
end
success = !!sock
if sock
(begin
sock.close
rescue StandardError
nil
end)
end
success
end
def proxy(connect_ip, connect_port)
cmd = proxy_cmd(connect_ip, connect_port)
debug(cmd)
@pid = @executor.run(cmd)
until tcp_server_open?
pid, status = Process.wait2(@pid, Process::WNOHANG)
raise "server did not start (#{cmd})" if pid
sleep 0.1
end
start_wait_thread(@pid)
at_exit { kill }
end
def start_wait_thread(pid)
@wait_thread = Thread.start do
_, status = Process.waitpid2(pid)
if @kill
if status.signaled?
raise "flexnbd quit with a bad signal: #{status.inspect}" unless
@kill.include? status.termsig
else
raise "flexnbd quit with a bad status: #{status.inspect}" unless
@kill.include? status.exitstatus
end
else
warn "flexnbd #{self.pid} quit"
raise "flexnbd #{self.pid} quit early with status #{status.to_i}"
end
end
end
def can_die(*status)
status = [0] if status.empty?
@kill += status
end
def kill
# At this point, to a certain degree we don't care what the exit
# status is
can_die(1)
if @pid
begin
Process.kill('INT', @pid)
rescue Errno::ESRCH => e
# already dead. Presumably this means it went away after a
# can_die() call.
end
end
@wait_thread.join if @wait_thread
end
def read(offset, length)
cmd = read_cmd(offset, length)
debug(cmd)
IO.popen(cmd) do |fh|
return fh.read
end
raise IOError, 'NBD read failed' unless $CHILD_STATUS.success?
out
end
def write(offset, data)
cmd = write_cmd(offset, data)
debug(cmd)
IO.popen(cmd, 'w') do |fh|
fh.write(data)
end
raise IOError, 'NBD write failed' unless $CHILD_STATUS.success?
nil
end
def join
@wait_thread.join
end
def mirror_unchecked(dest_ip, dest_port, _bandwidth = nil, _action = nil, timeout = nil)
cmd = mirror_cmd(dest_ip, dest_port)
debug(cmd)
maybe_timeout(cmd, timeout)
end
def mirror_unlink(dest_ip, dest_port, timeout = nil)
cmd = mirror_unlink_cmd(dest_ip, dest_port)
debug(cmd)
maybe_timeout(cmd, timeout)
end
def maybe_timeout(cmd, timeout = nil)
stdout = ''
stderr = ''
stat = nil
run = proc do
# Ruby 1.9 changed the popen3 api. instead of 3 args, the block
# gets 4. Not only that, but it no longer sets $?, so we have to
# go elsewhere for the process' exit status.
Open3.popen3(cmd) do |io_in, io_out, io_err, maybe_thr|
io_in.close
stdout.replace io_out.read
stderr.replace io_err.read
stat = maybe_thr.value if maybe_thr
end
stat ||= $CHILD_STATUS
end
if timeout
Timeout.timeout(timeout, &run)
else
run.call
end
[stdout, stderr, stat]
end
def mirror(dest_ip, dest_port, bandwidth = nil, action = nil)
stdout, stderr, status = mirror_unchecked(dest_ip, dest_port, bandwidth, action)
raise IOError, "Migrate command failed\n" + stderr unless status.success?
stdout
end
def break(timeout = nil)
cmd = break_cmd
debug(cmd)
maybe_timeout(cmd, timeout)
end
def acl(*acl)
cmd = acl_cmd(*acl)
debug(cmd)
maybe_timeout(cmd, 2)
end
def status(timeout = nil)
cmd = status_cmd
debug(cmd)
o, e = maybe_timeout(cmd, timeout)
[parse_status(o), e]
end
def launched?
!!@pid
end
def paused
Process.kill('STOP', @pid)
yield
ensure
Process.kill('CONT', @pid)
end
protected
def control_command(*args)
raise 'Server not running' unless @pid
args = args.compact
UNIXSocket.open(@ctrl) do |u|
u.write(args.join("\n") + "\n")
code, message = u.readline.split(': ', 2)
return [code, message]
end
end
def parse_status(status)
hsh = {}
status.split(' ').each do |part|
next if part.strip.empty?
a, b = part.split('=')
b.strip!
b = true if b == 'true'
b = false if b == 'false'
hsh[a.strip] = b
end
hsh
end
end
end

View File

@@ -0,0 +1,42 @@
module FlexNBD
def self.binary(str)
if str.respond_to? :force_encoding
str.force_encoding 'ASCII-8BIT'
else
str
end
end
# eeevil is his one and only name...
def self.read_constants
parents = []
current = File.expand_path('.')
while current != '/'
parents << current
current = File.expand_path(File.join(current, '..'))
end
source_root = parents.find do |dirname|
File.directory?(File.join(dirname, 'src'))
end
raise 'No source root!' unless source_root
headers = Dir[File.join(source_root, 'src', '{common,proxy,server}', '*.h')]
headers.each do |header_filename|
txt_lines = File.readlines(header_filename)
txt_lines.each do |line|
if line =~ /^#\s*define\s+([A-Z0-9_]+)\s+(\d+)\s*$/
# Bodge until I can figure out what to do with #ifdefs
const_set(Regexp.last_match(1), Regexp.last_match(2).to_i) unless const_defined?(Regexp.last_match(1))
end
end
end
end
read_constants
REQUEST_MAGIC = binary("\x25\x60\x95\x13") unless defined?(REQUEST_MAGIC)
REPLY_MAGIC = binary("\x67\x44\x66\x98") unless defined?(REPLY_MAGIC)
end # module FlexNBD

View File

@@ -0,0 +1,146 @@
require 'socket'
require 'timeout'
require 'flexnbd/constants'
module FlexNBD
class FakeDest
class Client
def initialize(sock)
@sock = sock
end
def write_hello(opts = {})
@sock.write('NBDMAGIC')
if opts[:magic] == :wrong
write_rand(@sock, 8)
else
@sock.write("\x00\x00\x42\x02\x81\x86\x12\x53")
end
if opts[:size] == :wrong
write_rand(@sock, 8)
else
@sock.write("\x00\x00\x00\x00\x00\x00\x10\x00")
end
@sock.write("\x00" * 128)
end
def write_rand(sock, len)
len.times { sock.write(rand(256).chr) }
end
def read_request
req = @sock.read(28)
magic_s = req[0...4]
type_s = req[4...8]
handle_s = req[8...16]
from_s = req[16...24]
len_s = req[24...28]
{
magic: magic_s,
type: type_s.unpack('N').first,
handle: handle_s,
from: self.class.parse_be64(from_s),
len: len_s.unpack('N').first
}
end
def write_error(handle)
write_reply(handle, 1)
end
def disconnected?
Timeout.timeout(2) do
@sock.read(1).nil?
end
rescue Timeout::Error
return false
end
def write_reply(handle, err = 0, opts = {})
if opts[:magic] == :wrong
write_rand(@sock, 4)
else
@sock.write(::FlexNBD::REPLY_MAGIC)
end
@sock.write([err].pack('N'))
@sock.write(handle)
end
def close
@sock.close
end
def read_data(len)
@sock.read(len)
end
def write_data(len)
@sock.write(len)
end
def self.parse_be64(str)
raise "String is the wrong length: 8 bytes expected (#{str.length} received)" unless
str.length == 8
top, bottom = str.unpack('NN')
(top << 32) + bottom
end
def receive_mirror(opts = {})
write_hello
loop do
req = read_request
case req[:type]
when 1
read_data(req[:len])
write_reply(req[:handle])
when 65_536
write_reply(req[:handle], opts[:err] == :entrust ? 1 : 0)
break
else
raise "Unexpected request: #{req.inspect}"
end
end
disc = read_request
if disc[:type] == 2
close
else
raise "Not a disconnect: #{req.inspect}"
end
end
end # class Client
def initialize(addr, port)
@sock = TCPServer.new(addr, port)
end
def accept(err_msg = 'Timed out waiting for a connection', timeout = 5)
client_sock = nil
begin
Timeout.timeout(timeout) do
client_sock = @sock.accept
end
rescue Timeout::Error
raise Timeout::Error, err_msg
end
client_sock
Client.new(client_sock)
end
def close
@sock.close
end
end # module FakeDest
end # module FlexNBD

View File

@@ -0,0 +1,159 @@
require 'socket'
require 'timeout'
require 'flexnbd/constants'
module FlexNBD
class FakeSource
def initialize(addr, port, err_msg, source_addr = nil, source_port = 0)
timing_out(2, err_msg) do
begin
@sock = if source_addr
TCPSocket.new(addr, port, source_addr, source_port)
else
TCPSocket.new(addr, port)
end
rescue Errno::ECONNREFUSED
warn 'Connection refused, retrying'
sleep(0.2)
retry
end
end
end
def close
@sock.close
end
def read_hello
timing_out(::FlexNBD::MS_HELLO_TIME_SECS,
'Timed out waiting for hello.') do
raise 'No hello.' unless (hello = @sock.read(152)) &&
hello.length == 152
passwd_s = hello[0..7]
magic = hello[8..15].unpack('Q>').first
size = hello[16..23].unpack('Q>').first
flags = hello[24..27].unpack('L>').first
reserved = hello[28..-1]
return { passwd: passwd_s, magic: magic, size: size, flags: flags, reserved: reserved }
end
end
def send_request(type, handle = 'myhandle', from = 0, len = 0, magic = REQUEST_MAGIC, flags = 0)
raise 'Bad handle' unless handle.length == 8
@sock.write(magic)
@sock.write([flags].pack('n'))
@sock.write([type].pack('n'))
@sock.write(handle)
@sock.write([n64(from)].pack('q'))
@sock.write([len].pack('N'))
end
def write_write_request(from, len, handle = 'myhandle')
send_request(1, handle, from, len)
end
def write_write_request_with_fua(from, len, handle = 'myhandle')
send_request(1, handle, from, len, REQUEST_MAGIC, 1)
end
def write_flush_request(handle = 'myhandle')
send_request(3, handle, 0, 0)
end
def write_entrust_request(handle = 'myhandle')
send_request(65_536, handle)
end
def write_disconnect_request(handle = 'myhandle')
send_request(2, handle)
end
def write_read_request(from, len, _handle = 'myhandle')
send_request(0, 'myhandle', from, len)
end
def write_data(data)
@sock.write(data)
end
# Handy utility
def read(from, len)
timing_out(2, 'Timed out reading') do
send_request(0, 'myhandle', from, len)
read_raw(len)
end
end
def read_raw(len)
@sock.read(len)
end
def send_mirror
read_hello
write(0, '12345678')
read_response
write_disconnect_request
close
end
def write(from, data)
write_write_request(from, data.length)
write_data(data)
end
def write_with_fua(from, data)
write_write_request_with_fua(from, data.length)
write_data(data)
end
def flush
write_flush_request
end
def read_response
magic = @sock.read(4)
error_s = @sock.read(4)
handle = @sock.read(8)
{
magic: magic,
error: error_s.unpack('N').first,
handle: handle
}
end
def disconnected?
result = nil
Timeout.timeout(2) { result = @sock.read(1).nil? }
result
end
def timing_out(time, msg)
Timeout.timeout(time) do
yield
end
rescue Timeout::Error
warn msg
exit 1
end
private
# take a 64-bit number, turn it upside down (due to :
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/11920
# )
def n64(b)
((b & 0xff00000000000000) >> 56) |
((b & 0x00ff000000000000) >> 40) |
((b & 0x0000ff0000000000) >> 24) |
((b & 0x000000ff00000000) >> 8) |
((b & 0x00000000ff000000) << 8) |
((b & 0x0000000000ff0000) << 24) |
((b & 0x000000000000ff00) << 40) |
((b & 0x00000000000000ff) << 56)
end
end # class FakeSource
end # module FlexNBD

View File

@@ -0,0 +1,55 @@
require 'tempfile'
#
# LdPreload is a little wrapper for using LD_PRELOAD loggers to pick up system
# calls when testing flexnbd.
#
module LdPreload
#
# This takes an object name, sets up a temporary log file, whose name is
# recorded in the environment as OUTPUT_obj_name, where obj_name is the
# name of the preload module to build and load.
def with_ld_preload(obj_name)
@ld_preload_logs ||= {}
flunk 'Can only load a preload module once!' if @ld_preload_logs[obj_name]
system("make -C ld_preloads/ #{obj_name}.o > /dev/null") ||
flunk("Failed to build object #{obj_name}")
orig_env = ENV['LD_PRELOAD']
ENV['LD_PRELOAD'] = [orig_env, File.expand_path("./ld_preloads/#{obj_name}.o")].compact.join(' ')
# Open the log, and stick it in a hash
@ld_preload_logs[obj_name] = Tempfile.new(obj_name)
ENV['OUTPUT_' + obj_name] = @ld_preload_logs[obj_name].path
yield
ensure
if @ld_preload_logs[obj_name]
@ld_preload_logs[obj_name].close
@ld_preload_logs.delete(obj_name)
end
ENV['LD_PRELOAD'] = orig_env
end
def read_ld_preload_log(obj_name)
lines = []
lines << @ld_preload_logs[obj_name].readline.chomp until
@ld_preload_logs[obj_name].eof?
lines
end
#
# The next to methods assume the log file has one entry per line, and that
# each entry is a series of values separated by colons.
#
def parse_ld_preload_logs(obj_name)
read_ld_preload_log(obj_name).map do |l|
l.split(':').map { |i| i =~ /^\d+$/ ? i.to_i : i }
end
end
def assert_func_call(loglines, args, msg)
re = Regexp.new('^' + args.join(':'))
assert(loglines.any? { |l| l.match(re) }, msg)
end
end

View File

@@ -0,0 +1,13 @@
SRC := $(wildcard *.c)
OBJS := $(SRC:%.c=%.o)
all: $(OBJS)
clean:
$(RM) $(OBJS)
%.o: %.c
gcc -shared -fPIC -ldl -o $@ $<
.PHONY: all clean

View File

@@ -0,0 +1,33 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
typedef int (*real_msync_t)(void *addr, size_t length, int flags);
int real_msync(void *addr, size_t length, int flags) {
return ((real_msync_t)dlsym(RTLD_NEXT, "msync"))(addr, length, flags);
}
/*
* Noddy LD_PRELOAD wrapper to catch msync calls, and log them to a file.
*/
int msync(void *addr, size_t length, int flags) {
FILE *fd;
char *fn;
int retval;
retval = real_msync(addr, length, flags);
fn = getenv("OUTPUT_msync_logger");
if ( fn != NULL ) {
fd = fopen(fn,"a");
fprintf(fd,"msync:%d:%i:%i:%i\n", addr, length, flags, retval);
fclose(fd);
}
return retval;
}

View File

@@ -0,0 +1,38 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
/*
* Noddy LD_PRELOAD wrapper to catch setsockopt calls, and log them to a file.
*/
typedef int (*real_setsockopt_t)(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
int real_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
{
return ((real_setsockopt_t)dlsym(RTLD_NEXT, "setsockopt"))(sockfd, level, optname, optval, optlen);
}
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
{
FILE *fd;
char *fn;
int retval;
retval = real_setsockopt(sockfd, level, optname, optval, optlen);
fn = getenv("OUTPUT_setsockopt_logger");
/*
* Only interested in catching non-null 4-byte (integer) values
*/
if ( fn != NULL && optval != NULL && optlen == 4) {
fd = fopen(fn,"a");
fprintf(fd,"setsockopt:%i:%i:%i:%i:%i\n", sockfd, level, optname, *(int *)optval, retval);
fclose(fd);
}
return retval;
}

6
tests/acceptance/nbd_scenarios Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/ruby
test_files = Dir[File.dirname(__FILE__) + '/test*.rb']
for filename in test_files
require filename
end

View File

@@ -0,0 +1,240 @@
require 'flexnbd/fake_source'
require 'flexnbd/fake_dest'
require 'ld_preload'
module ProxyTests
include LdPreload
def b
"\xFF".b
end
def with_proxied_client(override_size = nil)
@env.serve1 unless @server_up
@env.proxy2 unless @proxy_up
@env.nbd2.can_die(0)
client = FlexNBD::FakeSource.new(@env.ip, @env.port2, "Couldn't connect to proxy")
begin
result = client.read_hello
assert_equal 'NBDMAGIC', result[:passwd]
assert_equal override_size || @env.file1.size, result[:size]
yield client
ensure
begin
client.close
rescue StandardError
nil
end
end
end
def test_exits_with_error_when_cannot_connect_to_upstream_on_start
assert_raises(RuntimeError) { @env.proxy1 }
end
def test_read_requests_successfully_proxied
with_proxied_client do |client|
(0..3).each do |n|
offset = n * 4096
client.write_read_request(offset, 4096, 'myhandle')
rsp = client.read_response
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 'myhandle', rsp[:handle]
assert_equal 0, rsp[:error]
orig_data = @env.file1.read(offset, 4096)
data = client.read_raw(4096)
assert_equal 4096, orig_data.size
assert_equal 4096, data.size
assert_equal(orig_data, data,
"Returned data does not match on request #{n + 1}")
end
end
end
def test_write_requests_successfully_proxied
with_proxied_client do |client|
(0..3).each do |n|
offset = n * 4096
client.write(offset, b * 4096)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 'myhandle', rsp[:handle]
assert_equal 0, rsp[:error]
data = @env.file1.read(offset, 4096)
assert_equal((b * 4096), data, "Data not written correctly (offset is #{n})")
end
end
end
def test_write_request_past_end_of_disc_returns_to_client
with_proxied_client do |client|
n = 1000
offset = n * 4096
client.write(offset, b * 4096)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 'myhandle', rsp[:handle]
# NBD protocol say ENOSPC (28) in this situation
assert_equal 28, rsp[:error]
end
end
def make_fake_server
server = FlexNBD::FakeDest.new(@env.ip, @env.port1)
@server_up = true
# We return a thread here because accept() and connect() both block for us
Thread.new do
sc = server.accept # just tell the supervisor we're up
sc.write_hello
[server, sc]
end
end
def test_read_request_retried_when_upstream_dies_partway
maker = make_fake_server
with_proxied_client(4096) do |client|
server, sc1 = maker.value
# Send the read request to the proxy
client.write_read_request(0, 4096)
# ensure we're given the read request
req1 = sc1.read_request
assert_equal ::FlexNBD::REQUEST_MAGIC, req1[:magic]
assert_equal ::FlexNBD::REQUEST_READ, req1[:type]
assert_equal 0, req1[:from]
assert_not_equal 0, req1[:len]
# Kill the server again, now we're sure the read request has been sent once
sc1.close
# We expect the proxy to reconnect without our client doing anything.
sc2 = server.accept
sc2.write_hello
# And once reconnected, it should resend an identical request.
req2 = sc2.read_request
assert_equal req1, req2
# The reply should be proxied back to the client.
sc2.write_reply(req2[:handle])
sc2.write_data(b * 4096)
# Check it to make sure it's correct
rsp = Timeout.timeout(15) { client.read_response }
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
assert_equal req1[:handle], rsp[:handle]
data = client.read_raw(4096)
assert_equal((b * 4096), data, 'Wrong data returned')
sc2.close
server.close
end
end
def test_write_request_retried_when_upstream_dies_partway
maker = make_fake_server
with_ld_preload('setsockopt_logger') do
with_proxied_client(4096) do |client|
server, sc1 = maker.value
# Send the read request to the proxy
client.write(0, (b * 4096))
# ensure we're given the read request
req1 = sc1.read_request
assert_equal ::FlexNBD::REQUEST_MAGIC, req1[:magic]
assert_equal ::FlexNBD::REQUEST_WRITE, req1[:type]
assert_equal 0, req1[:from]
assert_equal 4096, req1[:len]
data1 = sc1.read_data(4096)
assert_equal((b * 4096), data1, 'Data not proxied successfully')
# Read the setsockopt logs, so we can check that TCP_NODELAY is re-set
# later
read_ld_preload_log('setsockopt_logger')
# Kill the server again, now we're sure the read request has been sent once
sc1.close
# We expect the proxy to reconnect without our client doing anything.
sc2 = server.accept
sc2.write_hello
# And once reconnected, it should resend an identical request.
req2 = sc2.read_request
assert_equal req1, req2
data2 = sc2.read_data(4096)
assert_equal data1, data2
# The reply should be proxied back to the client.
sc2.write_reply(req2[:handle])
# Check it to make sure it's correct
rsp = Timeout.timeout(15) { client.read_response }
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
assert_equal req1[:handle], rsp[:handle]
sc2.close
server.close
# Check TCP_NODELAY was set on the upstream socket
log = read_ld_preload_log('setsockopt_logger')
assert_func_call(log,
['setsockopt', 3,
Socket::SOL_TCP, Socket::TCP_NODELAY, 1, 0],
'TCP_NODELAY not set on upstream fd 3')
end
end
end
def test_only_one_client_can_connect_to_proxy_at_a_time
with_proxied_client do |_client|
c2 = nil
assert_raises(Timeout::Error) do
Timeout.timeout(1) do
c2 = FlexNBD::FakeSource.new(@env.ip, @env.port2, "Couldn't connect to proxy (2)")
c2.read_hello
end
end
if c2
begin
c2.close
rescue StandardError
nil
end
end
end
end
def test_maximum_write_request_size
# Defined in src/common/nbdtypes.h
nbd_max_block_size = 32 * 1024 * 1024
@env.writefile1('0' * 40 * 1024)
with_proxied_client do |client|
# This will crash with EPIPE if the proxy dies.
client.write(0, b * nbd_max_block_size)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
end
end
end

View File

@@ -0,0 +1,88 @@
require 'test/unit'
require 'environment'
class TestDestErrorHandling < Test::Unit::TestCase
def setup
@env = Environment.new
@env.writefile1('0' * 4)
@env.listen1
end
def teardown
@env.cleanup
end
def test_hello_blocked_by_disconnect_causes_error_not_fatal
run_fake('source/close_after_connect')
assert_no_control
end
# # This is disabled while CLIENT_MAX_WAIT_SECS is removed
# def test_hello_goes_astray_causes_timeout_error
# run_fake( "source/hang_after_hello" )
# assert_no_control
# end
def test_sigterm_has_bad_exit_status
@env.nbd1.can_die(1)
run_fake('source/sigterm_after_hello')
end
def test_disconnect_after_hello_causes_error_not_fatal
run_fake('source/close_after_hello')
assert_no_control
end
def test_partial_read_causes_error
run_fake('source/close_mid_read')
end
def test_double_connect_during_hello
run_fake('source/connect_during_hello')
end
def test_acl_rejection
@env.acl1('127.0.0.1')
run_fake('source/connect_from_banned_ip')
end
def test_bad_write
run_fake('source/write_out_of_range')
end
def test_disconnect_before_write_data_causes_error
run_fake('source/close_after_write')
end
def test_disconnect_before_write_reply_causes_error
# Note that this is an odd case: writing the reply doesn't fail.
# The test passes because the next attempt by flexnbd to read a
# request returns EOF.
run_fake('source/close_after_write_data')
end
def test_straight_migration
@env.nbd1.can_die(0)
run_fake('source/successful_transfer')
end
private
def run_fake(name)
@env.run_fake(name, @env.ip, @env.port1)
assert @env.fake_reports_success, "#{name} failed."
end
def status
stat, = @env.status1
stat
end
def assert_no_control
assert !status['has_control'], 'Thought it had control'
end
def assert_control
assert status['has_control'], "Didn't think it had control"
end
end # class TestDestErrorHandling

View File

@@ -0,0 +1,155 @@
require 'test/unit'
require 'environment'
require 'flexnbd/constants'
class TestHappyPath < Test::Unit::TestCase
def setup
@env = Environment.new
end
def bin(str)
FlexNBD.binary str
end
def teardown
@env.nbd1.can_die(0)
@env.nbd2.can_die(0)
@env.cleanup
end
def test_read1
@env.writefile1('f' * 64)
@env.serve1
[0, 12, 63].each do |num|
assert_equal(
bin(@env.nbd1.read(num * @env.blocksize, @env.blocksize)),
bin(@env.file1.read(num * @env.blocksize, @env.blocksize))
)
end
[124, 1200, 10_028, 25_488].each do |num|
assert_equal(bin(@env.nbd1.read(num, 4)), bin(@env.file1.read(num, 4)))
end
end
# Check that we're not
#
def test_writeread1
@env.writefile1('0' * 64)
@env.serve1
[0, 12, 63].each do |num|
data = 'X' * @env.blocksize
@env.nbd1.write(num * @env.blocksize, data)
assert_equal(data, @env.file1.read(num * @env.blocksize, data.size))
assert_equal(data, @env.nbd1.read(num * @env.blocksize, data.size))
end
end
# Check that we're not overstepping or understepping where our writes end
# up.
#
def test_writeread2
@env.writefile1('0' * 1024)
@env.serve1
d0 = "\0" * @env.blocksize
d1 = 'X' * @env.blocksize
(0..63).each do |num|
@env.nbd1.write(num * @env.blocksize * 2, d1)
end
(0..63).each do |num|
assert_equal(d0, @env.nbd1.read(((2 * num) + 1) * @env.blocksize, d0.size))
end
end
def setup_to_mirror
@env.writefile1('f' * 4)
@env.serve1
@env.writefile2('0' * 4)
@env.listen2
end
def test_mirror
@env.nbd1.can_die
@env.nbd2.can_die(0)
setup_to_mirror
stdout, stderr = @env.mirror12
@env.nbd1.join
@env.nbd2.join
assert(File.file?(@env.filename1),
'The source file was incorrectly deleted')
assert_equal(@env.file1.read_original(0, @env.blocksize),
@env.file2.read(0, @env.blocksize))
end
def test_mirror_unlink
@env.nbd1.can_die(0)
@env.nbd2.can_die(0)
setup_to_mirror
assert File.file?(@env.filename1)
stdout, stderr = @env.mirror12_unlink
assert_no_match(/unrecognized/, stderr)
Timeout.timeout(10) { @env.nbd1.join }
assert !File.file?(@env.filename1)
end
def test_write_to_high_block
#
# This test does not work on 32 bit platforms.
#
skip('Not relevant on 32-bit platforms') if ['a'].pack('p').size < 8
# Create a large file, then try to write to somewhere after the 2G boundary
@env.truncate1 '4G'
@env.serve1
@env.nbd1.write(2**31 + 2**29, '12345678')
sleep(1)
assert_equal '12345678', @env.nbd1.read(2**31 + 2**29, 8)
end
def test_set_acl
# Just check that we get sane feedback here
@env.writefile1('f' * 4)
@env.serve1
_, stderr = @env.acl1('127.0.0.1')
assert_no_match(/^(F|E):/, stderr)
end
def test_write_more_than_one_run
one_mb = 2**20
data = "\0" * 256 * one_mb
File.open(@env.filename1, 'wb') { |f| f.write('1' * 256 * one_mb) }
@env.serve1
sleep 5
@env.write1(data)
@env.nbd1.can_die(0)
@env.nbd1.kill
i = 0
File.open(@env.filename1, 'rb') do |f|
while mb = f.read(one_mb)
unless "\0" * one_mb == mb
msg = format("Read non-zeros after offset %x:\n", (i * one_mb))
msg += `hexdump #{@env.filename1} | head -n5`
raise msg
end
i += 1
end
end
end
end

View File

@@ -0,0 +1,19 @@
require 'test/unit'
require 'environment'
require 'proxy_tests'
class TestPrefetchProxyMode < Test::Unit::TestCase
include ProxyTests
def setup
super
@env = Environment.new
@env.prefetch_proxy!
@env.writefile1('f' * 16)
end
def teardown
@env.cleanup
super
end
end

View File

@@ -0,0 +1,18 @@
require 'test/unit'
require 'environment'
require 'proxy_tests'
class TestProxyMode < Test::Unit::TestCase
include ProxyTests
def setup
super
@env = Environment.new
@env.writefile1('f' * 16)
end
def teardown
@env.cleanup
super
end
end

Some files were not shown because too many files have changed in this diff Show More