192 Commits

Author SHA1 Message Date
Patrick J Cherry
b3cea813e4 Updated changelog for release 2018-04-24 12:06:06 +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
117 changed files with 11998 additions and 8790 deletions

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.

View File

@@ -11,28 +11,9 @@ ifdef DEBUG
else
CFLAGS_EXTRA=-O2
endif
CFLAGS_EXTRA += -fPIC --std=gnu99
LDFLAGS_EXTRA += -Wl,--relax,--gc-sections
TOOLCHAIN := $(shell $(CC) --version|awk '/Debian/ {print "debian";exit;}')
#
# This bit adds extra flags depending of the distro, and the
# architecture. To make sure debian packages have the right
# set of 'native' flags on them
#
ifeq ($(TOOLCHAIN),debian)
DEBARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq ($(DEBARCH),$(filter $(DEBARCH),amd64 i386))
CFLAGS_EXTRA += -march=native
endif
ifeq ($(DEBARCH),armhf)
CFLAGS_EXTRA += -march=armv7-a -mtune=cortex-a8 -mfpu=neon
endif
LDFLAGS_EXTRA += -L$(LIB) -Wl,-rpath,${shell readlink -f ${LIB}}
else
LDFLAGS_EXTRA += -L$(LIB) -Wl,-rpath-link,$(LIB)
endif
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.
@@ -42,23 +23,19 @@ WARNINGS=-Wall \
-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) $(INC) -c $(CCFLAGS)
SAVEDEP=$(CC) $(INC) -MM $(CCFLAGS)
COMPILE=$(CC) -MMD $(INC) -c $(CCFLAGS)
LINK=$(CC) $(LLDFLAGS) -Isrc $(LIBS)
LIB=build/
EXISTING_OBJS := $(wildcard build/*.o)
-include $(EXISTING_OBJS:.o=.d)
COMMON_SRC := $(wildcard src/common/*.c)
SERVER_SRC := $(wildcard src/server/*.c)
PROXY_SRC := $(wildcard src/proxy/*.c)
@@ -71,12 +48,13 @@ SRCS := $(COMMON_SRC) $(SERVER_SRC) $(PROXY_SRC)
OBJS := $(COMMON_OBJ) $(SERVER_OBJ) $(PROXY_OBJ)
all: build/flexnbd build/flexnbd-proxy doc
all: build doc
build: server proxy
build/%.o: %.c
mkdir -p $(dir $@)
$(COMPILE) $< -o $@
$(SAVEDEP) $< > build/$*.d
objs: $(OBJS)
@@ -87,41 +65,42 @@ 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/tests/%.o)
CHECK_OBJ := $(CHECK_SRC:tests/unit/%.c=build/%.o)
# Why can't we reuse the build/%.o rule above? Not sure.
build/tests/%.o: tests/unit/%.c
mkdir -p $(dir $@)
$(COMPILE) $< -o $@
$(SAVEDEP) $< > build/tests/$*.d
CHECK_BINS := $(CHECK_OBJ:build/tests/%.o=build/tests/%)
build/tests/%: build/tests/%.o $(OBJS)
$(LINK) $^ -o $@ -lcheck
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: $(CHECK_BINS)
for bin in $^; do $$bin; done
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
a2x --destination-dir build --format manpage $<
txt2man -t flexnbd -s 1 $< > $@
build/flexnbd-proxy.1: README.proxy.txt
a2x --destination-dir build --format manpage $<
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 $< > $@
server-man: build/flexnbd.1.gz
proxy-man: build/flexnbd-proxy.1.gz
doc: server-man proxy-man
doc: build/flexnbd.1.gz build/flexnbd-proxy.1.gz
install:
mkdir -p $(INSTALLDIR)
@@ -130,5 +109,7 @@ install:
clean:
rm -rf build/*
.PHONY: clean objs check_objs all server proxy check_bins check doc build test acceptance
.PHONY: clean objs check_objs all server proxy check_bins check server-man proxy-man doc
# Include extra dependencies at the end, NOT before 'all'
-include $(wildcard build/*.d)

View File

@@ -1,19 +1,14 @@
FLEXNBD-PROXY(1)
================
:doctype: manpage
NAME
----
flexnbd-proxy - A simple NBD proxy
SYNOPSIS
--------
*flexnbd-proxy* ['OPTIONS']
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
@@ -25,11 +20,6 @@ of view of the client) reconnects and retransmits the request, before
returning the response to the client.
USAGE
-----
$ flexnbd-proxy --addr <ADDR> [ --port <PORT> ]
--conn-addr <ADDR> --conn-port <PORT>
[--bind <ADDR>] [--cache[=<CACHE_BYTES>]] [option]*
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
@@ -58,40 +48,39 @@ 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
~~~~~~~
OPTIONS
*--addr, -l ADDR*:
--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*:
--port, -p PORT
The port to listen on, if --addr is not a UNIX socket.
*--conn-addr, -C ADDR*:
--conn-addr, -C ADDR
The address of the NBD server to connect to. Required.
*--conn-port, -P PORT*:
--conn-port, -P PORT
The port of the NBD server to connect to. Required.
*--cache, -c=CACHE_BYTES*:
--cache, -c=CACHE_BYTES
If given, the size in bytes of read cache to use. CACHE_BYTES
defaults to 4096.
*--help, -h* :
--help, -h
Show command or global help.
*--verbose, -v* :
--verbose, -v
Output all available log information to STDERR.
*--quiet, -q* :
--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
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
@@ -101,32 +90,31 @@ The log line format is:
<TIMESTAMP>:<LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
*TIMESTAMP*:
<TIMESTAMP>
Time the log entry was made. This is expressed in terms of monotonic ms
*LEVEL*:
<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.
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*:
<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.
<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*:
<SOURCEFILE:SOURCELINE>
Identifies where in the source code this log line can be found.
*MSG*:
<MSG>
A short message describing what's happening, how it's being done, or
if you're very lucky *why* it's going on.
if you're very lucky why it's going on.
Proxying
~~~~~~~~
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
@@ -160,7 +148,6 @@ 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
@@ -173,7 +160,7 @@ 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:
Note: If specifying a cache size, you must use this form:
nbd-client$ flexnbd-proxy --cache=XXXX
@@ -183,30 +170,37 @@ 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 to nick@bytemark.co.uk.
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
* 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
------
Written by Alex Young <alex@bytemark.co.uk>.
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 <nick@bytemark.co.uk>
Proxy mode written by Nick Thomas <me@ur.gs>.
COPYING
-------
The full commit history is available on GitHub.
Copyright (c) 2012 Bytemark Hosting Ltd. Free use of this software is
granted under the terms of the GNU General Public License version 3 or
later.
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.

View File

@@ -1,17 +1,36 @@
FLEXNBD(1)
==========
:doctype: manpage
NAME
----
flexnbd - A fast NBD server
SYNOPSIS
--------
*flexnbd* 'COMMAND' ['OPTIONS']
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.
@@ -19,57 +38,56 @@ migration will be invisible to any connected clients.
Flexnbd tries quite hard to preserve sparsity of files it is serving,
even across migrations.
COMMANDS
--------
SERVE MODE
Serve a file.
serve
~~~~~
$ flexnbd serve --addr <ADDR> --port <PORT> --file <FILE>
[--sock <SOCK>] [--default-deny] [-k] [global option]* [acl entry]*
[--sock <SOCK>] [--default-deny] [-k] [global_option]*
[acl_entry]*
Serve a file. If any ACL entries are given (which should be IP
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
^^^^^^^
OPTIONS
*--addr, -l ADDR*:
--addr, -l ADDR
The address to listen on. Required.
*--port, -p PORT*:
--port, -p PORT
The port to listen on. Required.
*--file, -f FILE*:
--file, -f FILE
The file to serve. Must already exist. Required.
*--sock, -s SOCK*:
--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*:
--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*:
--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
~~~~~~
$ flexnbd listen --addr <ADDR> --port <PORT> --file <FILE>
[--sock <SOCK>] [--default-deny] [global option]* [acl entry]*
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'.
@@ -81,25 +99,24 @@ 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
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`
until it succeeds, and you will need to restart the 'flexnbd listen'
process to allow that to happen.
Options
^^^^^^^
As for 'serve'.
OPTIONS
mirror
~~~~~~
As for serve.
$ flexnbd mirror --addr <ADDR> --port <PORT> --sock SOCK
[--unlink] [--bind <BIND-ADDR>] [global option]*
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
@@ -112,7 +129,7 @@ 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
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
@@ -122,161 +139,150 @@ the data, there can be no ambiguity.
Note: files smaller than 4096 bytes cannot be mirrored.
Options
^^^^^^^
OPTIONS
*--addr, -l ADDR*:
--addr, -l ADDR
The address of the remote server to migrate to. Required.
*--port, -p PORT*:
--port, -p PORT
The port of the remote server to migrate to. Required.
*--sock, -s SOCK*:
--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.
--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.
--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
~~~~~
$ flexnbd mirror --sock SOCK [global option]*
BREAK MODE
Stop a running migration.
Options
^^^^^^^
$ flexnbd break --sock SOCK [global_option]*
*--sock, -s SOCK*:
The control socket of the local server whose emigration to stop.
OPTIONS
--sock, -s SOCK
The control socket of the local server whose migration to stop.
Required.
acl
~~~
$ flexnbd acl --sock <SOCK> [acl entry]+ [global option]*
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
^^^^^^^
OPTIONS
*--sock, -s SOCK*:
The control socket of the server whose ACL to replace.
--sock, -s SOCK
The control socket of the server whose ACL to replace. Required
status
~~~~~~
$ flexnbd status --sock <SOCK> [global option]*
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*:
pid
The process id of the server listening on SOCK.
*is_mirroring*:
is_mirroring
'true' if this server is sending migration data, 'false' otherwise.
*has_control*:
has_control
'false' if this server was started in 'listen' mode. 'true' otherwise.
read
~~~~
OPTIONS
$ flexnbd read --addr <ADDR> --port <PORT> --from <OFFSET>
--size <SIZE> [--bind BIND-ADDR] [global option]*
--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. The returned data will be echoed to
STDOUT. In case of a remote ACL, set the local source address to
BIND-ADDR.
OFFSET in a single NBD query.
Options
^^^^^^^
$ flexnbd read --addr ADDR --port PORT --from OFFSET --size SIZE
[--bind BIND_ADDR] [global_option]*
*--addr, -l ADDR*:
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*:
--port, -p PORT
The port of the remote server. Required.
*--from, -F OFFSET*:
--from, -F OFFSET
The byte offset to start reading from. Required. Maximum 2^62.
*--size, -S SIZE*:
--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.
--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
~~~~~
$ cat ... | flexnbd write --addr <ADDR> --port <PORT> --from <OFFSET>
--size <SIZE> [--bind BIND-ADDR] [global option]*
WRITE MODE
Connect to the server at ADDR:PORT, and write SIZE bytes from STDIN
starting at OFFSET in a single NBD query. In case of a remote ACL, set
the local source address to BIND-ADDR.
starting at OFFSET in a single NBD query.
Options
^^^^^^^
$ cat ... | flexnbd write --addr ADDR --port PORT --from OFFSET
--size SIZE [--bind BIND_ADDR] [global_option]*
*--addr, -l ADDR*:
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*:
--port, -p PORT
The port of the remote server. Required.
*--from, -F OFFSET*:
--from, -F OFFSET
The byte offset to start writing from. Required. Maximum 2^62.
*--size, -S SIZE*:
--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.
--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
~~~~
HELP MODE
$ flexnbd help [command] [global option]*
$ flexnbd help [mode] [global_option]*
Without 'command', show the list of available commands. With 'command',
show help for that command.
Without mode, show the list of available modes. With mode, show help for that mode.
GLOBAL OPTIONS
--------------
*--help, -h* :
Show command or global help.
--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.
--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
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
@@ -284,39 +290,38 @@ error to set both --verbose and --quiet. The last one wins.
The log line format is:
<TIMESTAMP>:<LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
<TIMESTAMP>:<LEVEL>:<PID> <THREAD> <SOURCEFILE:SOURCELINE>: <MSG>
*TIMESTAMP*:
Time the log entry was made. This is expressed in terms of monotonic ms.
<TIMESTAMP>
Time the log entry was made. This is expressed in terms of monotonic
ms.
*LEVEL*:
<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.
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*:
<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.
<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*:
<SOURCEFILE:SOURCELINE>
Identifies where in the source code this log line can be found.
*MSG*:
<MSG>
A short message describing what's happening, how it's being done, or
if you're very lucky *why* it's going on.
if you're very lucky why it's going on.
EXAMPLES
--------
Serving a file
~~~~~~~~~~~~~~
SERVING A FILE
The simplest case is serving a file on the default nbd port:
@@ -326,8 +331,7 @@ The simplest case is serving a file on the default nbd port:
root:x:
$
Reading server status
~~~~~~~~~~~~~~~~~~~~~
READING SERVER STATUS
In order to read a server's status, we need it to open a control socket.
@@ -340,8 +344,7 @@ In order to read a server's status, we need it to open a control socket.
Note that the status output is newline-terminated.
Migrating
~~~~~~~~~
MIGRATING
To migrate, we need to provide a destination file of the right size.
@@ -367,8 +370,8 @@ 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 build/flexnbd serve --addr 0.0.0.0 --port 4778
[2] + 9651 done build/flexnbd listen --addr 0.0.0.0 --port 4779
[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
@@ -376,21 +379,25 @@ server quit soon after we start the migration, and the destination
exited at roughly the same time.
BUGS
----
Should be reported to alex@bytemark.co.uk.
Should be reported on GitHub at
* https://github.com/BytemarkHosting/flexnbd-c/issues
AUTHOR
------
Written by Alex Young <alex@bytemark.co.uk>.
Originally written by Alex Young <alex@blackkettle.org>.
Original concept and core code by Matthew Bloch <matthew@bytemark.co.uk>.
Some additions by Nick Thomas <nick@bytemark.co.uk>
Proxy mode written by Nick Thomas <me@ur.gs>.
COPYING
-------
The full commit history is available on GitHub.
Copyright (c) 2012 Bytemark Hosting Ltd. Free use of this software is
granted under the terms of the GNU General Public License version 3 or
later.
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.

View File

@@ -1,60 +0,0 @@
# encoding: utf-8
def make(*targets)
sh "make #{targets.map{|t| t.to_s}.join(" ")}"
end
def maketask( opts )
case opts
when Symbol
maketask opts => opts
else
opts.each do |name, targets|
task( name ){make *[*targets]}
end
end
end
desc "Build the binary and man page"
maketask :build => [:all, :doc]
desc "Build just the flexnbd binary"
maketask :flexnbd => [:server]
file "build/flexnbd" => :flexnbd
desc "Build just the flexnbd-proxy binary"
maketask :flexnbd_proxy => [:proxy]
file "build/flexnbd-proxy" => :flexnbd_proxy
desc "Build just the man page"
maketask :man => :doc
namespace "test" do
desc "Run all tests"
task 'run' => ["unit", "scenarios"]
desc "Build C tests"
maketask :build => :check_bins
desc "Run C tests"
maketask :unit => :check
desc "Run NBD test scenarios"
task 'scenarios' => ["build/flexnbd", "build/flexnbd-proxy"] do
sh "cd tests/acceptance && RUBYOPT='-I.' ruby nbd_scenarios -v"
end
end
desc "Remove all build targets, binaries and temporary files"
maketask :clean
file "debian/changelog" do
FileUtils.mkdir_p "debian"
sh "hg log --style=changelog.template > debian/changelog"
end
desc "Generate the changelog"
task :changelog => "debian/changelog"

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)

View File

@@ -45,8 +45,7 @@ int build_allocation_map(struct bitset * allocation_map, int fd)
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 {
} else {
for (unsigned int i = 0; i < fiemap->fm_mapped_extents; i++) {
bitset_set_range(allocation_map,
fiemap->fm_extents[i].fe_logical,
@@ -58,12 +57,10 @@ int build_allocation_map(struct bitset * allocation_map, int fd)
* 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
];
struct fiemap_extent *last =
&fiemap->fm_extents[fiemap->fm_mapped_extents - 1];
offset = last->fe_logical + last->fe_length;
}
else {
} else {
offset += fiemap->fm_length;
}
}
@@ -74,7 +71,8 @@ int build_allocation_map(struct bitset * allocation_map, int fd)
}
int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **out_map)
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.
@@ -85,7 +83,7 @@ int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **
off64_t size;
/* O_DIRECT should not be used with mmap() */
*out_fd = open(filename, O_RDWR | O_SYNC );
*out_fd = open(filename, O_RDWR | O_NOATIME);
if (*out_fd < 1) {
warn("open(%s) failed: does it exist?", filename);
@@ -97,6 +95,15 @@ int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **
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;
}
@@ -108,9 +115,9 @@ int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **
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 @ %p", filename, size, *out_fd,
*out_map);
} else {
debug("opened %s size %ld on fd %d", filename, size, *out_fd);
}
@@ -141,7 +148,8 @@ int readloop(int filedes, void *buffer, size_t size)
ssize_t result = read(filedes, buffer + readden, size - readden);
if (result == 0 /* EOF */ ) {
warn( "end-of-file detected while reading after %i bytes", readden );
warn("end-of-file detected while reading after %i bytes",
readden);
return -1;
}
@@ -161,7 +169,9 @@ 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);
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);
@@ -175,7 +185,8 @@ int sendfileloop(int out_fd, int in_fd, off64_t *offset, size_t count)
}
#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)
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;
@@ -183,13 +194,13 @@ ssize_t spliceloop(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_
//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);
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 {
} else {
return -1;
}
} else {
@@ -213,13 +224,19 @@ int splice_via_pipe_loop(int fd_in, int fd_out, size_t len)
while (spliced < len) {
ssize_t run = len - spliced;
ssize_t s2, s1 = spliceloop(fd_in, NULL, pipefd[1], NULL, run, SPLICE_F_NONBLOCK);
ssize_t s2, s1 =
spliceloop(fd_in, NULL, pipefd[1], NULL, run,
SPLICE_F_NONBLOCK);
/*if (run > 65535)
run = 65535; */
if (s1 < 0) { break; }
if (s1 < 0) {
break;
}
s2 = spliceloop(pipefd[0], NULL, fd_out, NULL, s1, 0);
if (s2 < 0) { break; }
if (s2 < 0) {
break;
}
spliced += s2;
}
close(pipefd[0]);
@@ -241,7 +258,9 @@ int read_until_newline(int fd, char* buf, int bufsize)
for (cur = 0; cur < bufsize; cur++) {
int result = read(fd, buf + cur, 1);
if (result <= 0) { return -1; }
if (result <= 0) {
return -1;
}
if (buf[cur] == 10) {
buf[cur] = '\0';
break;
@@ -266,7 +285,9 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines)
* -1 for an eof
* -1 for a read error
*/
if (readden <= 1) { return lines_count; }
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) {
@@ -304,7 +325,8 @@ ssize_t iobuf_read(int fd, struct iobuf *iobuf, size_t default_size )
}
left = iobuf->size - iobuf->needle;
debug( "Reading %"PRIu32" of %"PRIu32" bytes from fd %i", left, iobuf->size, fd );
debug("Reading %" PRIu32 " of %" PRIu32 " bytes from fd %i", left,
iobuf->size, fd);
count = read(fd, iobuf->buf + iobuf->needle, left);
@@ -332,7 +354,8 @@ 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 );
debug("Writing %" PRIu32 " of %" PRIu32 " bytes to fd %i", left,
iobuf->size, fd);
count = write(fd, iobuf->buf + iobuf->needle, left);
if (count >= 0) {

View File

@@ -38,7 +38,8 @@ int readloop(int filedes, void *buffer, size_t size);
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);
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
@@ -65,7 +66,8 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines);
* ''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);
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.
@@ -73,4 +75,3 @@ int open_and_mmap( const char* filename, int* out_fd, uint64_t* out_size, void *
int fd_is_closed(int fd_in);
#endif

View File

@@ -94,4 +94,3 @@ void mode(char* mode, int argc, char **argv);
char *help_help_text;
#endif

View File

@@ -13,6 +13,7 @@ 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)
@@ -20,39 +21,41 @@ 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 = htobe32( from->magic );
to->type = htobe32( from->type );
memcpy( to->handle, from->handle, 8 );
to->from = htobe64( from->from );
to->len = htobe32( from->len );
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 = be32toh( from->magic );
to->type = be32toh( from->type );
memcpy( to->handle, from->handle, 8 );
to->from = be64toh( from->from );
to->len = be32toh( from->len );
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 = htobe32( from->magic );
to->error = htobe32( from->error );
memcpy( to->handle, from->handle, 8 );
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 = be32toh( from->magic );
to->error = be32toh( from->error );
memcpy( to->handle, from->handle, 8 );
to->magic = htobe32(from->magic);
to->error = htobe32(from->error);
to->handle.w = from->handle.w;
}

View File

@@ -7,16 +7,39 @@
#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
/* The top 2 bytes of the type field are overloaded and can contain flags */
#define REQUEST_MASK 0x0000ffff
/* 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
/* 1MiB is the de-facto standard for maximum size of header + data */
#define NBD_MAX_SIZE ( 1024 * 1024 )
/* 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 ) )
@@ -24,6 +47,11 @@
#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_
@@ -33,13 +61,15 @@ struct nbd_init_raw {
char passwd[8];
__be64 magic;
__be64 size;
char reserved[128];
__be32 flags;
char reserved[124];
};
struct nbd_request_raw {
__be32 magic;
__be32 type; /* == READ || == WRITE */
char handle[8];
__be16 flags;
__be16 type; /* == READ || == WRITE || == FLUSH */
nbd_handle_t handle;
__be64 from;
__be32 len;
} __attribute__ ((packed));
@@ -47,22 +77,22 @@ struct nbd_request_raw {
struct nbd_reply_raw {
__be32 magic;
__be32 error; /* 0 = ok, else error */
char handle[8]; /* handle you got from request */
nbd_handle_t handle; /* handle you got from request */
};
struct nbd_init {
char passwd[8];
uint64_t magic;
uint64_t size;
char reserved[128];
uint32_t flags;
char reserved[124];
};
struct nbd_request {
uint32_t magic;
uint32_t type; /* == READ || == WRITE || == DISCONNECT */
char handle[8];
uint16_t flags;
uint16_t type; /* == READ || == WRITE || == DISCONNECT || == FLUSH */
nbd_handle_t handle;
uint64_t from;
uint32_t len;
} __attribute__ ((packed));
@@ -70,7 +100,7 @@ struct nbd_request {
struct nbd_reply {
uint32_t magic;
uint32_t error; /* 0 = ok, else error */
char handle[8]; /* handle you got from request */
nbd_handle_t handle; /* handle you got from request */
};
void nbd_r2h_init(struct nbd_init_raw *from, struct nbd_init *to);
@@ -82,4 +112,3 @@ 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

View File

@@ -22,7 +22,9 @@ int parse_ip_to_sockaddr(struct sockaddr* out, char* src)
/* allow user to start with [ and end with any other invalid char */
{
int i = 0, j = 0;
if (src[i] == '[') { i++; }
if (src[i] == '[') {
i++;
}
for (; i < 64 && IS_IP_VALID_CHAR(src[i]); i++) {
temp[j++] = src[i];
}
@@ -72,8 +74,7 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
if (max == 0) {
*out = NULL;
return 0;
}
else {
} else {
list = xmalloc(max * sizeof(struct ip_and_mask));
*out = (struct ip_and_mask(*)[]) list;
debug("acl alloc: %p", *out);
@@ -88,16 +89,14 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
return i;
}
for (j=0; entries[i][j] && entries[i][j] != '/'; j++)
; // increment j!
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 {
} else {
outentry->mask = MAX_MASK_BITS;
}
# undef MAX_MASK_BITS
@@ -124,4 +123,3 @@ void parse_port( char *s_port, struct sockaddr_in *out )
}
out->sin_port = htobe16(raw_port);
}

View File

@@ -26,4 +26,3 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries);
void parse_port(char *s_port, struct sockaddr_in *out);
#endif

View File

@@ -10,7 +10,9 @@
int socket_connect(struct sockaddr *to, struct sockaddr *from)
{
int fd = socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
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;
@@ -41,7 +43,8 @@ int socket_connect(struct sockaddr* to, struct sockaddr* from)
return fd;
}
int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* out_size )
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");
@@ -56,13 +59,18 @@ int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* 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 )
int socket_nbd_read_hello(int fd, uint64_t * out_size,
uint32_t * out_flags)
{
struct nbd_init_raw init_raw;
@@ -72,16 +80,18 @@ int socket_nbd_read_hello( int fd, uint64_t* out_size )
return 0;
}
return nbd_check_hello( &init_raw, out_size );
return nbd_check_hello(&init_raw, out_size, out_flags);
}
void nbd_hello_to_buf( struct nbd_init_raw *buf, off64_t out_size )
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);
@@ -89,10 +99,10 @@ void nbd_hello_to_buf( struct nbd_init_raw *buf, off64_t out_size )
return;
}
int socket_nbd_write_hello(int fd, off64_t out_size)
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 );
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"));
@@ -101,22 +111,25 @@ int socket_nbd_write_hello(int fd, off64_t out_size)
return 1;
}
void fill_request(struct nbd_request *request, int type, uint64_t from, uint32_t len)
void fill_request(struct nbd_request_raw *request_raw, uint16_t type,
uint16_t flags, uint64_t from, uint32_t len)
{
request->magic = htobe32(REQUEST_MAGIC);
request->type = htobe32(type);
uint32_t * randa = (uint32_t*)request->handle;
randa[0] = rand();
randa[1] = rand();
request->from = htobe64(from);
request->len = htobe32(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, struct nbd_request *request, struct nbd_reply *reply)
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)),
ERROR_IF_NEGATIVE(readloop
(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
"Couldn't read reply");
nbd_r2h_reply(&reply_raw, reply);
@@ -127,7 +140,7 @@ void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply)
if (reply->error != 0) {
error("Server replied with error %d", reply->error);
}
if (strncmp(request->handle, reply->handle, 8) != 0) {
if (request_raw_handle != reply_raw.handle.w) {
error("Did not reply with correct handle");
}
}
@@ -141,81 +154,77 @@ void wait_for_data( int fd, int timeout_secs )
FD_ZERO(&fds);
FD_SET(fd, &fds);
selected = sock_try_select(
FD_SETSIZE, &fds, NULL, NULL, timeout_secs >=0 ? &tv : NULL
);
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)
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs)
{
struct nbd_request request;
struct nbd_request_raw request_raw;
struct nbd_reply reply;
fill_request(&request, REQUEST_READ, from, len);
FATAL_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)),
fill_request(&request_raw, REQUEST_READ, 0, from, len);
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
"Couldn't write request");
wait_for_data(fd, timeout_secs);
read_reply(fd, &request, &reply);
read_reply(fd, request_raw.handle.w, &reply);
if (out_buf) {
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len),
"Read failed");
}
else {
FATAL_IF_NEGATIVE(
splice_via_pipe_loop(fd, out_fd, len),
"Splice failed"
);
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len), "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)
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd,
void *in_buf, int timeout_secs)
{
struct nbd_request request;
struct nbd_request_raw request_raw;
struct nbd_reply reply;
fill_request(&request, REQUEST_WRITE, from, len);
ERROR_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)),
fill_request(&request_raw, REQUEST_WRITE, 0, from, len);
ERROR_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
"Couldn't write request");
if (in_buf) {
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len),
"Write failed");
}
else {
ERROR_IF_NEGATIVE(
splice_via_pipe_loop(in_fd, fd, len),
"Splice failed"
);
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len), "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, &reply);
read_reply(fd, request_raw.handle.w, &reply);
}
int socket_nbd_disconnect(int fd)
{
int success = 1;
struct nbd_request request;
struct nbd_request_raw request_raw;
fill_request( &request, REQUEST_DISCONNECT, 0, 0 );
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, sizeof( request ) ),
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
"Failed to write the disconnect request.");
return success;
}
#define CHECK_RANGE(error_type) { \
uint64_t size;\
int success = socket_nbd_read_hello(params->client, &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 || \
@@ -233,7 +242,9 @@ int socket_nbd_disconnect( int fd )
void do_read(struct mode_readwrite_params *params)
{
params->client = socket_connect(&params->connect_to.generic, &params->connect_from.generic);
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,
@@ -243,11 +254,12 @@ void do_read(struct mode_readwrite_params* params)
void do_write(struct mode_readwrite_params *params)
{
params->client = socket_connect(&params->connect_to.generic, &params->connect_from.generic);
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);
}

View File

@@ -7,17 +7,20 @@
#include "nbdtypes.h"
int socket_connect(struct sockaddr *to, struct sockaddr *from);
int socket_nbd_read_hello(int fd, uint64_t* size);
int socket_nbd_write_hello(int fd, uint64_t size);
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_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 );
int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* out_size );
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

View File

@@ -24,7 +24,8 @@ void print_response( const char * response )
fprintf(out, "%s\n", response_text + 2);
}
void do_remote_command(char* command, char* socket_name, int argc, char** argv)
void do_remote_command(char *command, char *socket_name, int argc,
char **argv)
{
char newline = 10;
int i;
@@ -40,10 +41,10 @@ void do_remote_command(char* command, char* socket_name, int argc, char** argv)
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
);
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);
@@ -55,13 +56,10 @@ void do_remote_command(char* command, char* socket_name, int argc, char** argv)
}
write(remote, &newline, 1);
FATAL_IF_NEGATIVE(
read_until_newline(remote, response, max_response),
"Couldn't read response from %s", socket_name
);
FATAL_IF_NEGATIVE(read_until_newline(remote, response, max_response),
"Couldn't read response from %s", socket_name);
print_response(response);
exit(atoi(response));
}

View File

@@ -52,7 +52,9 @@ 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 (NULL == sig) {
return NULL;
}
if (pipe(fds)) {
free(sig);
@@ -60,7 +62,8 @@ struct self_pipe * self_pipe_create(void)
return NULL;
}
if ( fcntl( fds[0], F_SETFL, O_NONBLOCK ) || fcntl( fds[1], F_SETFL, O_NONBLOCK ) ) {
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);

View File

@@ -29,7 +29,8 @@ size_t sockaddr_size( const struct sockaddr* sa )
return ret;
}
const char* sockaddr_address_string( const struct sockaddr* sa, char* dest, size_t len )
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
size_t len)
{
NULLCHECK(sa);
NULLCHECK(dest);
@@ -65,13 +66,50 @@ const char* sockaddr_address_string( const struct sockaddr* sa, char* dest, size
int sock_set_reuseaddr(int fd, int optval)
{
return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(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) );
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval,
sizeof(optval));
}
int sock_set_tcp_cork(int fd, int optval)
@@ -100,7 +138,7 @@ int sock_try_bind( int fd, const struct sockaddr* sa )
{
int bind_result;
char s_address[256];
int retry = 1;
int retry = 10;
sockaddr_address_string(sa, &s_address[0], 256);
@@ -109,8 +147,7 @@ int sock_try_bind( int fd, const struct sockaddr* sa )
if (0 == bind_result) {
info("Bound to %s", s_address);
break;
}
else {
} else {
warn(SHOW_ERRNO("Couldn't bind to %s", s_address));
switch (errno) {
@@ -126,8 +163,11 @@ int sock_try_bind( int fd, const struct sockaddr* sa )
* will cope with it.
*/
case EADDRNOTAVAIL:
retry--;
if (retry) {
debug("retrying");
sleep(1);
}
continue;
case EADDRINUSE:
warn("%s in use, giving up.", s_address);
@@ -143,7 +183,8 @@ int sock_try_bind( int fd, const struct sockaddr* sa )
return bind_result;
}
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
fd_set * exceptfds, struct timeval *timeout)
{
int result;
@@ -158,14 +199,16 @@ int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptf
return result;
}
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait )
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()" ) );
warn(SHOW_ERRNO
("Failed to set socket non-blocking for connect()"));
return connect(fd, to, addrlen);
}
@@ -250,4 +293,3 @@ int sock_try_close( int fd )
return result;
}

View File

@@ -12,11 +12,27 @@ 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);
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);
@@ -29,13 +45,14 @@ int sock_set_nonblock(int fd, int optval);
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);
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 );
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

View File

@@ -13,6 +13,7 @@
pthread_key_t cleanup_handler_key;
int log_level = 2;
char *log_context = "";
void error_init(void)
{
@@ -25,10 +26,12 @@ void error_handler(int fatal)
if (context) {
longjmp(context->jmp, fatal ? 1 : 2);
} else {
if (fatal) {
abort();
} else {
pthread_exit((void *) 1);
}
else {
if ( fatal ) { abort(); }
else { pthread_exit((void*) 1); }
}
}
@@ -42,7 +45,9 @@ void exit_err( const char *msg )
void mylog(int line_level, const char *format, ...)
{
if (line_level < log_level) { return; }
if (line_level < log_level) {
return;
}
va_list argptr;
@@ -56,8 +61,7 @@ uint64_t monotonic_time_ms()
struct timespec ts;
uint64_t seconds_ms, nanoseconds_ms;
FATAL_IF_NEGATIVE(
clock_gettime(CLOCK_MONOTONIC, &ts),
FATAL_IF_NEGATIVE(clock_gettime(CLOCK_MONOTONIC, &ts),
SHOW_ERRNO("clock_gettime failed")
);
@@ -74,7 +78,8 @@ uint64_t monotonic_time_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);
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes",
ptr ? "realloc" : "malloc", size);
return p;
}
@@ -84,4 +89,3 @@ void* xmalloc(size_t size)
memset(p, 0, size);
return p;
}

View File

@@ -13,7 +13,8 @@
void *xrealloc(void *ptr, size_t size);
void *xmalloc(size_t size);
typedef void (cleanup_handler)(void* /* context */, int /* is fatal? */);
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;
@@ -21,6 +22,9 @@ 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 *);
@@ -92,7 +96,7 @@ 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:%d: "msg"\n", monotonic_time_ms(), levstr(level), getpid(),pthread_self(), __FILE__, __LINE__, ##__VA_ARGS__ )
#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__)
@@ -159,4 +163,3 @@ uint64_t monotonic_time_ms(void);
#define WARN_IF_NEGATIVE( value, msg, ... ) if ( value < 0 ) { warn( msg, ##__VA_ARGS__ ); }
#endif

View File

@@ -19,4 +19,3 @@ int main(int argc, char** argv)
return 0;
}

View File

@@ -19,6 +19,7 @@ static struct option proxy_options[] = {
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"
@@ -26,24 +27,24 @@ static char proxy_help_text[] =
"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_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;
"\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,
void read_proxy_param(int c,
char **downstream_addr,
char **downstream_port,
char **upstream_addr,
char **upstream_port,
char **bind_addr,
char **cache_bytes)
char **bind_addr, char **cache_bytes)
{
switch (c) {
case 'h':
@@ -116,34 +117,31 @@ int main( int argc, char *argv[] )
srand(time(NULL));
while (1) {
c = getopt_long( argc, argv, proxy_short_options, proxy_options, NULL );
if ( -1 == c ) { break; }
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
);
&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" );
fprintf(stderr,
"both --conn-addr and --conn-port are required.\n");
exit_err(proxy_help_text);
}
proxy = proxy_create(
downstream_addr,
proxy = proxy_create(downstream_addr,
downstream_port,
upstream_addr,
upstream_port,
bind_addr,
cache_bytes
);
upstream_port, bind_addr, cache_bytes);
/* Set these *after* proxy has been assigned to */
sigaction(SIGTERM, &exit_action, NULL);
@@ -152,15 +150,12 @@ int main( int argc, char *argv[] )
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
);
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
);
info("Proxying between %s (downstream) and %s %s (upstream)",
downstream_addr, upstream_addr, upstream_port);
}
success = do_proxy(proxy);
@@ -168,4 +163,3 @@ int main( int argc, char *argv[] )
return success ? 0 : 1;
}

View File

@@ -2,7 +2,8 @@
#include "util.h"
struct prefetch* prefetch_create( size_t size_bytes ){
struct prefetch *prefetch_create(size_t size_bytes)
{
struct prefetch *out = xmalloc(sizeof(struct prefetch));
NULLCHECK(out);
@@ -19,14 +20,16 @@ struct prefetch* prefetch_create( size_t size_bytes ){
}
void prefetch_destroy( struct prefetch *prefetch ) {
void prefetch_destroy(struct prefetch *prefetch)
{
if (prefetch) {
free(prefetch->buffer);
free(prefetch);
}
}
size_t prefetch_size( struct prefetch *prefetch){
size_t prefetch_size(struct prefetch *prefetch)
{
if (prefetch) {
return prefetch->size;
} else {
@@ -34,21 +37,25 @@ size_t prefetch_size( struct prefetch *prefetch){
}
}
void prefetch_set_is_empty( struct prefetch *prefetch ){
void prefetch_set_is_empty(struct prefetch *prefetch)
{
prefetch_set_full(prefetch, 0);
}
void prefetch_set_is_full( struct prefetch *prefetch ){
void prefetch_set_is_full(struct prefetch *prefetch)
{
prefetch_set_full(prefetch, 1);
}
void prefetch_set_full( struct prefetch *prefetch, int val ){
void prefetch_set_full(struct prefetch *prefetch, int val)
{
if (prefetch) {
prefetch->is_full = val;
}
}
int prefetch_is_full( struct prefetch *prefetch ){
int prefetch_is_full(struct prefetch *prefetch)
{
if (prefetch) {
return prefetch->is_full;
} else {
@@ -56,13 +63,16 @@ int prefetch_is_full( struct prefetch *prefetch ){
}
}
int prefetch_contains( struct prefetch *prefetch, uint64_t from, uint32_t len ){
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 ){
char *prefetch_offset(struct prefetch *prefetch, uint64_t from)
{
NULLCHECK(prefetch);
return prefetch->buffer + (from - prefetch->from);
}

View File

@@ -27,7 +27,8 @@ 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 );
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
uint32_t len);
char *prefetch_offset(struct prefetch *prefetch, uint64_t from);
#endif

View File

@@ -13,13 +13,11 @@
#include <sys/socket.h>
#include <netinet/tcp.h>
struct proxier* proxy_create(
char* s_downstream_address,
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 )
char *s_upstream_bind, char *s_cache_bytes)
{
struct proxier *out;
out = xmalloc(sizeof(struct proxier));
@@ -27,10 +25,9 @@ struct proxier* proxy_create(
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"
);
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");
@@ -41,22 +38,19 @@ struct proxier* proxy_create(
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 ),
FATAL_UNLESS(parse_ip_to_sockaddr
(&out->connect_to.generic, s_upstream_address),
"Couldn't parse upstream address '%s'",
s_upstream_address
);
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
);
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;
}
@@ -76,18 +70,28 @@ struct proxier* proxy_create(
}
out->init.buf = xmalloc(sizeof(struct nbd_init_raw));
out->req.buf = xmalloc( NBD_MAX_SIZE );
out->rsp.buf = xmalloc( NBD_MAX_SIZE );
/* 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 ) {
int proxy_prefetches(struct proxier *proxy)
{
NULLCHECK(proxy);
return proxy->prefetch != NULL;
}
int proxy_prefetch_bufsize( struct proxier* proxy ){
int proxy_prefetch_bufsize(struct proxier *proxy)
{
NULLCHECK(proxy);
return prefetch_size(proxy->prefetch);
}
@@ -103,7 +107,8 @@ void proxy_destroy( struct proxier* proxy )
}
/* Shared between our two different connect_to_upstream paths */
void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size );
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.
@@ -117,22 +122,22 @@ int proxy_connect_to_upstream( struct proxier* proxy )
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 ) ) {
WARN_IF_NEGATIVE(
sock_try_close( fd ),
"Couldn't close() after failed read of NBD hello on fd %i", fd
);
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 );
proxy_finish_connect_to_upstream(proxy, size, flags);
return 1;
}
@@ -153,7 +158,8 @@ void proxy_start_connect_to_upstream( struct proxier* proxy )
fd = socket(to->sa_family, SOCK_STREAM, 0);
if (fd < 0) {
warn( SHOW_ERRNO( "Couldn't create socket to reconnect to upstream" ) );
warn(SHOW_ERRNO
("Couldn't create socket to reconnect to upstream"));
return;
}
@@ -188,19 +194,28 @@ error:
return;
}
void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size ) {
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
);
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"));
@@ -218,11 +233,9 @@ void proxy_disconnect_from_upstream( struct proxier* proxy )
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 ),
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
"Failed to close() fd %i when disconnecting from upstream",
proxy->upstream_fd
);
proxy->upstream_fd);
proxy->upstream_fd = -1;
}
}
@@ -234,31 +247,28 @@ 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" )
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),
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),
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 ),
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),
FATAL_IF_NEGATIVE(listen(params->listen_fd, 1),
SHOW_ERRNO("Failed to listen on listening socket")
);
@@ -289,8 +299,8 @@ static char* proxy_session_state_names[] = {
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;
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)
@@ -299,10 +309,12 @@ int proxy_prefetch_for_request( struct proxier* proxy, int state )
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;
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_MASK ) == REQUEST_READ;
int is_read = req->type == REQUEST_READ;
if (is_read) {
/* See if we can respond with what's in our prefetch
@@ -321,11 +333,8 @@ int proxy_prefetch_for_request( struct proxier* proxy, int state )
nbd_h2r_reply(rsp, rsp_raw);
/* and the data */
memcpy(
proxy->rsp.buf + NBD_REPLY_SIZE,
prefetch_offset( proxy->prefetch, req->from ),
req->len
);
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;
@@ -333,14 +342,14 @@ int proxy_prefetch_for_request( struct proxier* proxy, int state )
/* return early, our work here is done */
return WRITE_TO_DOWNSTREAM;
}
}
else {
} 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 );
debug("Blowing away prefetch cache on type %d request.",
req->type);
prefetch_set_is_empty(proxy->prefetch);
}
@@ -388,13 +397,12 @@ int proxy_prefetch_for_reply( struct proxier* proxy, int state )
prefetched_bytes = proxy->req_hdr.len - proxy->prefetch_req_orig_len;
debug("Prefetched additional %d bytes", prefetched_bytes);
memcpy(
proxy->prefetch->buffer,
memcpy(proxy->prefetch->buffer,
proxy->rsp.buf + proxy->prefetch_req_orig_len + NBD_REPLY_SIZE,
prefetched_bytes
);
prefetched_bytes);
proxy->prefetch->from = proxy->req_hdr.from + proxy->prefetch_req_orig_len;
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
@@ -417,12 +425,14 @@ 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_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 );
count =
iobuf_read(proxy->downstream_fd, &proxy->req, NBD_REQUEST_SIZE);
if (count == -1) {
warn(SHOW_ERRNO("Couldn't read request from downstream"));
@@ -432,21 +442,26 @@ int proxy_read_from_downstream( struct proxier *proxy, int state )
if (proxy->req.needle == NBD_REQUEST_SIZE) {
nbd_r2h_request(request_raw, request);
if ( ( request->type & REQUEST_MASK ) == REQUEST_DISCONNECT ) {
if (request->type == REQUEST_DISCONNECT) {
info("Received disconnect request from client");
return EXIT;
}
/* Simple validations */
if ( ( request->type & REQUEST_MASK ) == REQUEST_READ ) {
if (request->len > ( NBD_MAX_SIZE - NBD_REPLY_SIZE ) ) {
warn( "NBD read request size %"PRIu32" too large", request->len );
/* 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_MASK ) == REQUEST_WRITE ) {
if (request->len > ( NBD_MAX_SIZE - NBD_REQUEST_SIZE ) ) {
warn( "NBD write request size %"PRIu32" too large", request->len );
if (request->type == REQUEST_WRITE) {
if (request->len > NBD_MAX_SIZE) {
warn("NBD write request size %" PRIu32 " too large",
request->len);
return EXIT;
}
@@ -455,10 +470,9 @@ int proxy_read_from_downstream( struct proxier *proxy, int state )
}
if (proxy->req.needle == proxy->req.size) {
debug(
"Received NBD request from downstream. type=%"PRIu32" from=%"PRIu64" len=%"PRIu32,
request->type, request->from, request->len
);
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 */
@@ -476,9 +490,8 @@ int proxy_continue_connecting_to_upstream( struct proxier* proxy, int state )
// assert( state == CONNECT_TO_UPSTREAM );
result = getsockopt(
proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len
);
result =
getsockopt(proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (result == -1) {
warn(SHOW_ERRNO("Failed to tell if connected to upstream"));
@@ -504,7 +517,9 @@ int proxy_read_init_from_upstream( struct proxier* proxy, int state )
// assert( state == READ_INIT_FROM_UPSTREAM );
count = iobuf_read( proxy->upstream_fd, &proxy->init, sizeof( struct nbd_init_raw ) );
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"));
@@ -513,11 +528,18 @@ int proxy_read_init_from_upstream( struct proxier* proxy, int state )
if (proxy->init.needle == proxy->init.size) {
uint64_t upstream_size;
if ( !nbd_check_hello( (struct nbd_init_raw*) proxy->init.buf, &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 */
@@ -581,7 +603,8 @@ 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;
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);
@@ -599,12 +622,7 @@ int proxy_read_from_upstream( struct proxier* proxy, int state )
goto disconnect;
}
if ( reply->error != 0 ) {
warn( "NBD error returned from upstream: %"PRIu32, reply->error );
goto disconnect;
}
if ( ( proxy->req_hdr.type & REQUEST_MASK ) == REQUEST_READ ) {
if (proxy->req_hdr.type == REQUEST_READ) {
/* Get the read reply data too. */
proxy->rsp.size += proxy->req_hdr.len;
}
@@ -680,7 +698,8 @@ void proxy_session( struct proxier* proxy )
/* First action: Write hello to downstream */
nbd_hello_to_buf( (struct nbd_init_raw *) proxy->rsp.buf, proxy->upstream_size );
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;
@@ -708,13 +727,13 @@ void proxy_session( struct proxier* proxy )
if (state != old_state) {
state_started = monotonic_time_ms();
debug(
"State transitition from %s to %s",
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 );
debug("Proxy is in state %s", proxy_session_state_names[state],
state);
}
old_state = state;
@@ -759,7 +778,9 @@ void proxy_session( struct proxier* proxy )
select_timeout_ptr = &select_timeout;
}
result = sock_try_select( FD_SETSIZE, &rfds, &wfds, NULL, select_timeout_ptr );
result =
sock_try_select(FD_SETSIZE, &rfds, &wfds, NULL,
select_timeout_ptr);
if (result == -1) {
warn(SHOW_ERRNO("select() failed: "));
@@ -785,7 +806,8 @@ void proxy_session( struct proxier* proxy )
break;
case CONNECT_TO_UPSTREAM:
if (FD_ISSET(proxy->upstream_fd, &wfds)) {
state = proxy_continue_connecting_to_upstream( proxy, state );
state =
proxy_continue_connecting_to_upstream(proxy, state);
}
/* Leaving state untouched will retry connecting to upstream -
* so introduce a bit of sleep */
@@ -827,9 +849,7 @@ void proxy_session( struct proxier* proxy )
*/
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]
warn("Timed out in state %s while communicating with upstream", proxy_session_state_names[state]
);
state = CONNECT_TO_UPSTREAM;
@@ -843,10 +863,8 @@ void proxy_session( struct proxier* proxy )
}
}
info(
"Finished proxy session on fd %i after %"PRIu64" successful request(s)",
proxy->downstream_fd, proxy->req_count
);
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;
@@ -871,13 +889,13 @@ int proxy_accept( struct proxier* params )
FD_ZERO(&fds);
FD_SET(params->listen_fd, &fds);
FATAL_IF_NEGATIVE(
sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
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 );
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) {
@@ -890,11 +908,9 @@ int proxy_accept( struct proxier* params )
params->downstream_fd = client_fd;
proxy_session(params);
WARN_IF_NEGATIVE(
sock_try_close( params->downstream_fd ),
WARN_IF_NEGATIVE(sock_try_close(params->downstream_fd),
"Couldn't close() downstram fd %i after proxy session",
params->downstream_fd
);
params->downstream_fd);
params->downstream_fd = -1;
}
@@ -919,33 +935,31 @@ void proxy_cleanup( struct proxier* proxy )
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(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 )
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
)
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
)
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
SHOW_ERRNO("Failed to close() upstream fd %i",
proxy->upstream_fd)
);
proxy->upstream_fd = -1;
}
@@ -972,4 +986,3 @@ int do_proxy( struct proxier* params )
return 0;
}

View File

@@ -46,6 +46,9 @@ struct proxier {
/* 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;
@@ -82,16 +85,13 @@ struct proxier {
/** */
};
struct proxier* proxy_create(
char* s_downstream_address,
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);
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

View File

@@ -22,7 +22,9 @@ 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)
static int is_included_in_acl(int list_length,
struct ip_and_mask (*list)[],
union mysockaddr *test)
{
NULLCHECK(test);
@@ -33,7 +35,8 @@ static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], uni
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);
debug("checking acl entry %d (%d/%d)", i, test->generic.sa_family,
entry->ip.family);
if (test->generic.sa_family != entry->ip.family) {
continue;
@@ -43,24 +46,24 @@ static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], uni
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) {
} 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 {
} 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]);
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; }
if (raw_address1[0] != raw_address2[0]) {
goto no_match;
}
else {
} else {
if ((raw_address1[0] & testmasks[testbits % 8]) !=
(raw_address2[0] & testmasks[testbits % 8])) {
goto no_match;
@@ -86,8 +89,7 @@ int acl_includes( struct acl * acl, union mysockaddr * addr )
if (0 == acl->len) {
return !(acl->default_deny);
}
else {
} else {
return is_included_in_acl(acl->len, acl->entries, addr);
}
}
@@ -105,4 +107,3 @@ void acl_destroy( struct acl * acl )
acl->entries = NULL;
free(acl);
}

View File

@@ -30,30 +30,41 @@ typedef bitfield_word_t * bitfield_p;
((_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) {
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) {
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) {
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) {
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) {
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) {
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)
{
@@ -72,8 +83,10 @@ static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t 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)
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++);
@@ -96,7 +109,9 @@ static inline void bit_clear_range(bitfield_p b, uint64_t from, uint64_t len)
* 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) {
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;
@@ -185,8 +200,10 @@ 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 ) );
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;
@@ -224,12 +241,9 @@ static inline void bitset_free( struct bitset * set )
FATAL_IF_NEGATIVE(pthread_mutex_unlock(&set->lock), "Error unlocking bitset")
static inline void bitset_stream_enqueue(
struct bitset * set,
static inline void bitset_stream_enqueue(struct bitset *set,
enum bitset_stream_events event,
uint64_t from,
uint64_t len
)
uint64_t from, uint64_t len)
{
struct bitset_stream *stream = set->stream;
@@ -254,10 +268,8 @@ static inline void bitset_stream_enqueue(
return;
}
static inline void bitset_stream_dequeue(
struct bitset * set,
struct bitset_stream_entry * out
)
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;
@@ -298,10 +310,9 @@ static inline size_t bitset_stream_size( struct bitset * set )
return size;
}
static inline uint64_t bitset_stream_queued_bytes(
struct bitset * set,
enum bitset_stream_events event
)
static inline uint64_t bitset_stream_queued_bytes(struct bitset *set,
enum bitset_stream_events
event)
{
uint64_t total;
@@ -331,10 +342,8 @@ static inline void bitset_disable_stream( struct bitset * set )
/** 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)
static inline void bitset_set_range(struct bitset *set,
uint64_t from, uint64_t len)
{
INT_FIRST_AND_LAST;
BITSET_LOCK;
@@ -357,10 +366,8 @@ static inline void bitset_set( struct bitset * set )
/** 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)
static inline void bitset_clear_range(struct bitset *set,
uint64_t from, uint64_t len)
{
INT_FIRST_AND_LAST;
BITSET_LOCK;
@@ -383,12 +390,9 @@ static inline void bitset_clear( struct bitset * set )
/** 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,
static inline uint64_t bitset_run_count_ex(struct bitset *set,
uint64_t from,
uint64_t len,
int* run_is_set
)
uint64_t len, int *run_is_set)
{
uint64_t run;
@@ -401,7 +405,9 @@ static inline uint64_t bitset_run_count_ex(
INT_FIRST_AND_LAST;
BITSET_LOCK;
run = bit_run_count(set->bits, first, bitlen, run_is_set) * set->resolution;
run =
bit_run_count(set->bits, first, bitlen,
run_is_set) * set->resolution;
run -= (from % set->resolution);
BITSET_UNLOCK;
@@ -411,10 +417,8 @@ static inline uint64_t bitset_run_count_ex(
/** 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)
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);
}
@@ -435,4 +439,3 @@ static inline int bitset_is_set_at( struct bitset * set, uint64_t at )
#endif

View File

@@ -18,14 +18,16 @@
// 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)))
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" )
FATAL_IF(-1 == shutdown(fd, SHUT_RDWR),
SHOW_ERRNO
("Failed to shutdown() the socket, killing the server")
);
}
@@ -53,8 +55,8 @@ struct client *client_create( struct server *serve, int socket )
c->stop_signal = self_pipe_create();
FATAL_IF_NEGATIVE(
timer_create( CLOCK_MONOTONIC, &evp, &(c->killswitch) ),
FATAL_IF_NEGATIVE(timer_create
(CLOCK_MONOTONIC, &evp, &(c->killswitch)),
SHOW_ERRNO("Failed to create killswitch timer")
);
@@ -67,7 +69,8 @@ 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 );
debug("client %p: signal stop (%d, %d)", c, c->stop_signal->read_fd,
c->stop_signal->write_fd);
self_pipe_signal(c->stop_signal);
}
@@ -75,8 +78,7 @@ void client_destroy( struct client *client )
{
NULLCHECK(client);
FATAL_IF_NEGATIVE(
timer_delete( client->killswitch ),
FATAL_IF_NEGATIVE(timer_delete(client->killswitch),
SHOW_ERRNO("Couldn't delete killswitch")
);
@@ -119,7 +121,8 @@ void write_not_zeroes(struct client* client, uint64_t from, uint64_t len)
uint64_t run = bitset_run_count(map, from, len);
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len, run);
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len,
run);
if (run > len) {
run = len;
@@ -164,8 +167,7 @@ void write_not_zeroes(struct client* client, uint64_t from, uint64_t len)
bitset_set_range(map, from, run);
len -= run;
from += run;
}
else {
} else {
char zerobuffer[block_allocation_resolution];
/* not allocated, read in block_allocation_resoution */
while (run > 0) {
@@ -183,7 +185,8 @@ void write_not_zeroes(struct client* client, uint64_t from, uint64_t len)
*/
int all_zeros = (zerobuffer[0] == 0) &&
(0 == memcmp( zerobuffer, zerobuffer+1, blockrun-1 ));
(0 ==
memcmp(zerobuffer, zerobuffer + 1, blockrun - 1));
if (!all_zeros) {
memcpy(client->mapped + from, zerobuffer, blockrun);
@@ -217,7 +220,8 @@ int fd_read_request( int fd, struct nbd_request_raw *out_request)
/* 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 )
int client_read_request(struct client *client,
struct nbd_request *out_request, int *disconnected)
{
NULLCHECK(client);
NULLCHECK(out_request);
@@ -228,11 +232,13 @@ int client_read_request( struct client * client , struct nbd_request *out_reques
*disconnected = 1;
switch (errno) {
case 0:
debug( "EOF while reading request" );
warn("EOF while reading request");
return 0;
case ECONNRESET:
debug( "Connection reset while"
" reading request" );
warn("Connection reset while" " reading request");
return 0;
case ETIMEDOUT:
warn("Connection timed out while" " reading request");
return 0;
default:
/* FIXME: I've seen this happen, but I
@@ -242,9 +248,7 @@ int client_read_request( struct client * client , struct nbd_request *out_reques
* again. It should *probably* be an
* error() call, but I want to be sure.
* */
fatal("Error reading request: %d, %s",
errno,
strerror( errno ));
fatal("Error reading request: %d, %s", errno, strerror(errno));
}
}
@@ -252,14 +256,14 @@ int client_read_request( struct client * client , struct nbd_request *out_reques
return 1;
}
int fd_write_reply( int fd, char *handle, int error )
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;
memcpy( reply.handle, handle, 8 );
reply.handle.w = handle;
nbd_h2r_reply(&reply, &reply_raw);
debug("Replying with handle=0x%08X, error=%" PRIu32, handle, error);
@@ -289,9 +293,10 @@ int fd_write_reply( int fd, char *handle, int error )
* 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 )
int client_write_reply(struct client *client, struct nbd_request *request,
int error)
{
return fd_write_reply( client->socket, request->handle, error);
return fd_write_reply(client->socket, request->handle.w, error);
}
@@ -303,14 +308,17 @@ void client_write_init( struct client * client, uint64_t size )
memcpy(init.passwd, INIT_PASSWD, sizeof(init.passwd));
init.magic = INIT_MAGIC;
init.size = size;
memset( init.reserved, 0, 128 );
/* 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)),
"Couldn't send hello"
);
ERROR_IF_NEGATIVE(writeloop
(client->socket, &init_raw, sizeof(init_raw)),
"Couldn't send hello");
}
@@ -330,23 +338,15 @@ void client_flush( struct client * client, size_t len )
size_t spliced = 0;
while (spliced < len) {
ssize_t received = splice(
client->socket, NULL,
ssize_t received = splice(client->socket, NULL,
pipes[1], NULL,
len - spliced, flags);
FATAL_IF_NEGATIVE( received,
"splice error: %s",
strerror(errno));
FATAL_IF_NEGATIVE(received, "splice error: %s", strerror(errno));
ssize_t junked = 0;
while (junked < received) {
ssize_t junk;
junk = splice(
pipes[0], NULL,
devnull, NULL,
received, flags );
FATAL_IF_NEGATIVE( junk,
"splice error: %s",
strerror(errno));
junk = splice(pipes[0], NULL, devnull, NULL, received, flags);
FATAL_IF_NEGATIVE(junk, "splice error: %s", strerror(errno));
junked += junk;
}
spliced += received;
@@ -378,27 +378,26 @@ int client_request_needs_reply( struct client * client,
return 0;
}
debug(
"request type=%"PRIu32", from=%"PRIu64", len=%"PRIu32", handle=0x%08X",
request.type, request.from, request.len, request.handle
);
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 */
/* 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
);
request.from, request.len);
if (request.type == REQUEST_WRITE) {
client_flush(client, request.len);
}
client_write_reply( client, &request, EPERM ); /* TODO: Change to ERANGE ? */
client_write_reply(client, &request, ENOSPC);
client->disconnect = 0;
return 0;
}
switch (request.type)
{
switch (request.type) {
case REQUEST_READ:
break;
case REQUEST_WRITE:
@@ -407,15 +406,22 @@ int client_request_needs_reply( struct client * client,
debug("request disconnect");
client->disconnect = 1;
return 0;
case REQUEST_FLUSH:
break;
default:
fatal("Unknown request 0x%08X", request.type);
/* 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 )
void client_reply_to_read(struct client *client,
struct nbd_request request)
{
off64_t offset;
@@ -428,62 +434,74 @@ void client_reply_to_read( struct client* client, struct nbd_request request )
/* 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,
ERROR_IF_NEGATIVE(sendfileloop(client->socket,
client->fileno,
&offset,
request.len),
"sendfile failed from=%ld, len=%d",
offset,
request.len);
offset, request.len);
sock_set_tcp_cork(client->socket, 0);
}
void client_reply_to_write( struct client* client, struct nbd_request request )
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);
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 {
} else {
debug("No allocation map, writing directly.");
/* If we get cut off partway through reading this data:
* */
ERROR_IF_NEGATIVE(
readloop( client->socket,
ERROR_IF_NEGATIVE(readloop(client->socket,
client->mapped + request.from,
request.len),
"reading write data failed from=%ld, len=%d",
request.from,
request.len
);
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);
bitset_set_range(client->serve->allocation_map, request.from,
request.len);
}
if (1) /* not sure whether this is necessary... */
{
/* multiple of 4K page size */
uint64_t from_rounded = request.from & (!0xfff);
// 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,
FATAL_IF_NEGATIVE(msync(client->mapped + from_rounded,
len_rounded,
MS_SYNC | MS_INVALIDATE),
"msync failed %ld %ld", request.from, request.len
);
"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)
{
@@ -494,6 +512,9 @@ void client_reply( struct client* client, struct nbd_request request )
case REQUEST_WRITE:
client_reply_to_write(client, request);
break;
case REQUEST_FLUSH:
client_reply_to_flush(client, request);
break;
}
}
@@ -514,8 +535,7 @@ void client_arm_killswitch( struct client* client )
debug("Arming killswitch");
FATAL_IF_NEGATIVE(
timer_settime( client->killswitch, 0, &its, NULL ),
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
SHOW_ERRNO("Failed to arm killswitch")
);
@@ -535,8 +555,7 @@ void client_disarm_killswitch( struct client* client )
debug("Disarming killswitch");
FATAL_IF_NEGATIVE(
timer_settime( client->killswitch, 0, &its, NULL ),
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
SHOW_ERRNO("Failed to disarm killswitch")
);
@@ -572,8 +591,9 @@ int client_serve_request(struct client* client)
if (fd_count == 0) {
/* This "can't ever happen" */
fatal("No FDs selected, and no timeout!");
} else if (fd_count < 0) {
fatal("Select failed");
}
else if ( fd_count < 0 ) { fatal( "Select failed" ); }
if (self_pipe_fd_isset(client->stop_signal, &rfds)) {
debug("Client received stop signal.");
@@ -651,13 +671,14 @@ void client_cleanup(struct client* client,
}
if (client->fileno) {
FATAL_IF_NEGATIVE(close(client->fileno),
"Error closing file %d",
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 ); }
if (server_acl_locked(client->serve)) {
server_unlock_acl(client->serve);
}
}
@@ -668,19 +689,18 @@ void* client_serve(void* client_uncast)
error_set_handler((cleanup_handler *) client_cleanup, client);
info("client: mmaping file");
FATAL_IF_NEGATIVE(
open_and_mmap(
client->serve->filename,
FATAL_IF_NEGATIVE(open_and_mmap(client->serve->filename,
&client->fileno,
NULL,
(void**) &client->mapped
),
"Couldn't open/mmap file %s: %s", client->serve->filename, strerror( errno )
&client->mapped_size,
(void **) &client->mapped),
"Couldn't open/mmap file %s: %s",
client->serve->filename, strerror(errno)
);
FATAL_IF_NEGATIVE(
madvise( client->mapped, client->serve->size, MADV_RANDOM ),
SHOW_ERRNO( "Failed to madvise() %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);
@@ -688,8 +708,7 @@ void* client_serve(void* client_uncast)
client_send_hello(client);
debug("client: serving requests");
while (client_serve_request(client) == 0)
;
while (client_serve_request(client) == 0);
debug("client: stopped serving requests");
client->stopped = 1;
@@ -698,10 +717,10 @@ void* client_serve(void* client_uncast)
server_control_arrived(client->serve);
}
debug("Cleaning client %p up normally in thread %p", client, pthread_self());
debug("Cleaning client %p up normally in thread %p", client,
pthread_self());
client_cleanup(client, 0);
debug("Client thread done");
return NULL;
}

View File

@@ -3,6 +3,7 @@
#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.
@@ -31,6 +32,8 @@ struct client {
int fileno;
char *mapped;
uint64_t mapped_size;
struct self_pipe *stop_signal;
struct server *serve; /* FIXME: remove above duplication */
@@ -53,4 +56,3 @@ void client_destroy( struct client * client );
void client_signal_stop(struct client *client);
#endif

View File

@@ -44,9 +44,7 @@
#include <unistd.h>
struct control * control_create(
struct flexnbd * flexnbd,
const char * csn)
struct control *control_create(struct flexnbd *flexnbd, const char *csn)
{
struct control *control = xmalloc(sizeof(struct control));
@@ -80,8 +78,7 @@ void control_destroy( struct control * control )
free(control);
}
struct control_client * control_client_create(
struct flexnbd * flexnbd,
struct control_client *control_client_create(struct flexnbd *flexnbd,
int client_fd,
struct mbox *state_mbox)
{
@@ -112,8 +109,7 @@ 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,
control_client_create(control->flexnbd,
client_fd,
control->mirror_state_mbox);
@@ -132,7 +128,8 @@ void control_accept_client( struct control * control )
union mysockaddr client_address;
socklen_t addrlen = sizeof(union mysockaddr);
client_fd = accept( control->control_fd, &client_address.generic, &addrlen );
client_fd =
accept(control->control_fd, &client_address.generic, &addrlen);
FATAL_IF(-1 == client_fd, "control accept failed");
control_handle_client(control, client_fd);
@@ -178,25 +175,23 @@ int open_control_socket( const char * socket_name )
}
control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(control_fd ,
"Couldn't create control socket");
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);
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)),
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"
);
FATAL_IF_NEGATIVE(listen(control_fd, 5),
"Couldn't listen on control socket");
return control_fd;
}
@@ -229,8 +224,7 @@ void control_serve( struct control * control )
}
void control_cleanup(
struct control * control,
void control_cleanup(struct control *control,
int fatal __attribute__ ((unused)))
{
NULLCHECK(control);
@@ -256,7 +250,8 @@ void * control_runner( void * control_uncast )
#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 )
void control_write_mirror_response(enum mirror_state mirror_state,
int client_fd)
{
switch (mirror_state) {
case MS_INIT:
@@ -286,12 +281,12 @@ void control_write_mirror_response( enum mirror_state mirror_state, int client_f
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)
enum mirror_state control_client_mirror_wait(struct control_client *client)
{
NULLCHECK(client);
NULLCHECK(client->mirror_state_mbox);
@@ -345,14 +340,11 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
if (linesc > 2) {
if (strcmp("exit", lines[2]) == 0) {
action_at_finish = ACTION_EXIT;
}
else if (strcmp( "unlink", lines[2]) == 0 ) {
} else if (strcmp("unlink", lines[2]) == 0) {
action_at_finish = ACTION_UNLINK;
}
else if (strcmp("nothing", lines[2]) == 0) {
} else if (strcmp("nothing", lines[2]) == 0) {
action_at_finish = ACTION_NOTHING;
}
else {
} else {
write_socket("1: action must be 'exit' or 'nothing'");
return -1;
}
@@ -389,13 +381,13 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
server_lock_start_mirror(serve);
{
if (server_mirror_can_start(serve)) {
serve->mirror_super = mirror_super_create(
serve->filename,
serve->mirror_super = mirror_super_create(serve->filename,
connect_to,
connect_from,
max_Bps,
action_at_finish,
client->mirror_state_mbox );
client->
mirror_state_mbox);
serve->mirror = serve->mirror_super->mirror;
server_prevent_mirror_start(serve);
} else {
@@ -415,18 +407,14 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
* sighandler can block the serve thread
*/
if (serve->mirror_super) {
FATAL_IF( 0 != pthread_create(
&serve->mirror_super->thread,
FATAL_IF(0 != pthread_create(&serve->mirror_super->thread,
NULL,
mirror_super_runner,
serve
),
"Failed to create mirror thread"
);
serve),
"Failed to create mirror thread");
debug("Control thread mirror super waiting");
enum mirror_state state =
control_client_mirror_wait( client );
enum mirror_state state = control_client_mirror_wait(client);
debug("Control thread writing response");
control_write_mirror_response(state, client->socket);
}
@@ -436,7 +424,8 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
return 0;
}
int control_mirror_max_bps( struct control_client* client, int linesc, char** lines )
int control_mirror_max_bps(struct control_client *client, int linesc,
char **lines)
{
NULLCHECK(client);
NULLCHECK(client->flexnbd);
@@ -489,8 +478,7 @@ int control_acl(struct control_client* client, int linesc, char** lines)
strlen(lines[new_acl->len]));
write(client->socket, "\n", 1);
acl_destroy(new_acl);
}
else {
} else {
flexnbd_replace_acl(flexnbd, new_acl);
info("ACL set");
write(client->socket, "0: updated\n", 11);
@@ -500,8 +488,7 @@ int control_acl(struct control_client* client, int linesc, char** lines)
}
int control_break(
struct control_client* client,
int control_break(struct control_client *client,
int linesc __attribute__ ((unused)),
char **lines __attribute__ ((unused))
)
@@ -524,13 +511,10 @@ int control_break(
if (server_is_closed(serve)) {
info("Mirror completed while canceling");
write( client->socket,
"1: mirror completed\n", 20 );
}
else {
write(client->socket, "1: mirror completed\n", 20);
} else {
info("Mirror successfully stopped.");
write( client->socket,
"0: mirror stopped\n", 18 );
write(client->socket, "0: mirror stopped\n", 18);
result = 1;
}
@@ -546,8 +530,7 @@ int control_break(
/** FIXME: add some useful statistics */
int control_status(
struct control_client* client,
int control_status(struct control_client *client,
int linesc __attribute__ ((unused)),
char **lines __attribute__ ((unused))
)
@@ -566,10 +549,14 @@ int control_status(
void control_client_cleanup(struct control_client *client,
int fatal __attribute__ ((unused)))
{
if (client->socket) { close(client->socket); }
if (client->socket) {
close(client->socket);
}
/* This is wrongness */
if ( server_acl_locked( client->flexnbd->serve ) ) { server_unlock_acl( client->flexnbd->serve ); }
if (server_acl_locked(client->flexnbd->serve)) {
server_unlock_acl(client->flexnbd->serve);
}
control_client_destroy(client);
}
@@ -584,30 +571,25 @@ void control_respond(struct control_client * client)
int i, linesc;
linesc = read_lines_until_blankline(client->socket, 256, &lines);
if (linesc < 1)
{
if (linesc < 1) {
write(client->socket, "9: missing command\n", 19);
/* ignore failure */
}
else if (strcmp(lines[0], "acl") == 0) {
} 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) {
} 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) {
} 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) {
} else if (strcmp(lines[0], "status") == 0) {
info("status command received");
if (control_status(client, linesc - 1, lines + 1) < 0) {
debug("status command failed");
@@ -617,8 +599,7 @@ void control_respond(struct control_client * client)
if (control_mirror_max_bps(client, linesc - 1, lines + 1) < 0) {
debug("mirror_max_bps command failed");
}
}
else {
} else {
write(client->socket, "10: unknown command\n", 23);
}
@@ -630,4 +611,3 @@ void control_respond(struct control_client * client)
control_client_cleanup(client, 0);
debug("control command handled");
}

View File

@@ -44,16 +44,15 @@ struct control_client{
struct mbox *mirror_state_mbox;
};
struct control * control_create(
struct flexnbd *,
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 accept_control_connection(struct server *params, int client_fd,
union mysockaddr *client_address);
void serve_open_control_socket(struct server *params);
#endif

View File

@@ -61,16 +61,13 @@ int flexnbd_build_signal_fd(void)
}
void flexnbd_create_shared(
struct flexnbd * flexnbd,
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 = control_create(flexnbd, s_ctrl_sock);
} else {
flexnbd->control = NULL;
}
@@ -78,8 +75,7 @@ void flexnbd_create_shared(
}
struct flexnbd * flexnbd_create_serving(
char* s_ip_address,
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -90,17 +86,14 @@ struct flexnbd * flexnbd_create_serving(
int use_killswitch)
{
struct flexnbd *flexnbd = xmalloc(sizeof(struct flexnbd));
flexnbd->serve = server_create(
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);
max_nbd_clients, use_killswitch, 1);
flexnbd_create_shared(flexnbd, s_ctrl_sock);
// Beats installing one handler per client instance
@@ -110,17 +103,14 @@ struct flexnbd * flexnbd_create_serving(
.sa_flags = SA_RESTART | SA_SIGINFO
};
FATAL_UNLESS(
0 == sigaction( CLIENT_KILLSWITCH_SIGNAL, &act, NULL ),
"Installing client killswitch signal failed"
);
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,
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -129,15 +119,12 @@ struct flexnbd * flexnbd_create_listening(
char **s_acl_entries)
{
struct flexnbd *flexnbd = xmalloc(sizeof(struct flexnbd));
flexnbd->serve = server_create(
flexnbd,
flexnbd->serve = server_create(flexnbd,
s_ip_address,
s_port,
s_file,
default_deny,
acl_entries,
s_acl_entries,
1, 0, 0);
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
@@ -153,8 +140,7 @@ void flexnbd_spawn_control(struct flexnbd * flexnbd )
pthread_t *control_thread = &flexnbd->control->thread;
FATAL_UNLESS( 0 == pthread_create(
control_thread,
FATAL_UNLESS(0 == pthread_create(control_thread,
NULL,
control_runner,
flexnbd->control),
@@ -233,9 +219,7 @@ void make_writable( const char * filename )
{
NULLCHECK(filename);
FATAL_IF_NEGATIVE(chmod(filename, S_IWUSR),
"Couldn't chmod %s: %s",
filename,
strerror( errno ) );
"Couldn't chmod %s: %s", filename, strerror(errno));
}
@@ -262,4 +246,3 @@ int flexnbd_serve( struct flexnbd * flexnbd )
return success;
}

View File

@@ -29,8 +29,7 @@ struct flexnbd {
struct flexnbd *flexnbd_create(void);
struct flexnbd * flexnbd_create_serving(
char* s_ip_address,
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -40,8 +39,7 @@ struct flexnbd * flexnbd_create_serving(
int max_nbd_clients,
int use_killswitch);
struct flexnbd * flexnbd_create_listening(
char* s_ip_address,
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -63,4 +61,3 @@ 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

View File

@@ -22,15 +22,14 @@ void flexthread_mutex_destroy( struct flexthread_mutex * ftm )
if (flexthread_mutex_held(ftm)) {
flexthread_mutex_unlock(ftm);
}
else if ( (pthread_t)NULL != ftm->holder ) {
} 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"\
fatal("Attempted to destroy a flexthread_mutex"
" held by another thread!");
}
@@ -72,4 +71,3 @@ int flexthread_mutex_held( struct flexthread_mutex * ftm )
NULLCHECK(ftm);
return pthread_self() == ftm->holder;
}

View File

@@ -90,8 +90,7 @@ struct mirror_ctrl {
};
struct mirror * mirror_alloc(
union mysockaddr * connect_to,
struct mirror *mirror_alloc(union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
enum mirror_finish_action action_at_finish,
@@ -144,19 +143,13 @@ void mirror_init( struct mirror * mirror, const char * filename )
NULLCHECK(mirror);
NULLCHECK(filename);
FATAL_IF_NEGATIVE(
open_and_mmap(
filename,
FATAL_IF_NEGATIVE(open_and_mmap(filename,
&map_fd,
&size,
(void**) &mirror->mapped
),
"Failed to open and mmap %s",
filename
);
(void **) &mirror->mapped),
"Failed to open and mmap %s", filename);
FATAL_IF_NEGATIVE(
madvise( mirror->mapped, size, MADV_SEQUENTIAL ),
FATAL_IF_NEGATIVE(madvise(mirror->mapped, size, MADV_SEQUENTIAL),
SHOW_ERRNO("Failed to madvise() %s", filename)
);
}
@@ -176,8 +169,7 @@ void mirror_reset( struct mirror * mirror )
}
struct mirror * mirror_create(
const char * filename,
struct mirror *mirror_create(const char *filename,
union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
@@ -189,9 +181,7 @@ struct mirror * mirror_create(
mirror = mirror_alloc(connect_to,
connect_from,
max_Bps,
action_at_finish,
commit_signal);
max_Bps, action_at_finish, commit_signal);
mirror_init(mirror, filename);
mirror_reset(mirror);
@@ -281,7 +271,8 @@ int mirror_connect( struct mirror * mirror, uint64_t local_size )
NULLCHECK(mirror->connect_to);
mirror->client = socket_connect(&mirror->connect_to->generic, connect_from);
mirror->client =
socket_connect(&mirror->connect_to->generic, connect_from);
if (0 < mirror->client) {
fd_set fds;
struct timeval tv = { MS_HELLO_TIME_SECS, 0 };
@@ -293,30 +284,30 @@ int mirror_connect( struct mirror * mirror, uint64_t local_size )
if (FD_ISSET(mirror->client, &fds)) {
uint64_t remote_size;
if ( socket_nbd_read_hello( mirror->client, &remote_size ) ) {
uint32_t remote_flags;
if (socket_nbd_read_hello
(mirror->client, &remote_size, &remote_flags)) {
if (remote_size == local_size) {
connected = 1;
mirror_set_state(mirror, MS_GO);
}
else {
} else {
warn("Remote size (%d) doesn't match local (%d)",
remote_size, local_size);
mirror_set_state(mirror, MS_FAIL_SIZE_MISMATCH);
}
}
else {
} else {
warn("Mirror attempt rejected.");
mirror_set_state(mirror, MS_FAIL_REJECTED);
}
}
else {
} else {
warn("No NBD Hello received.");
mirror_set_state(mirror, MS_FAIL_NO_HELLO);
}
if ( !connected ) { close( mirror->client ); }
if (!connected) {
close(mirror->client);
}
else {
} else {
warn("Mirror failed to connect.");
mirror_set_state(mirror, MS_FAIL_CONNECT);
}
@@ -371,12 +362,15 @@ int mirror_setup_next_xfer( struct mirror_ctrl *ctrl )
* full, and stop when it's a quarter full. This stops a busy client from
* stalling a migration forever. FIXME: made-up numbers.
*/
if ( mirror->offset < serve->size && bitset_stream_size( serve->allocation_map ) > BITSET_STREAM_SIZE / 2 ) {
if (mirror->offset < serve->size
&& bitset_stream_size(serve->allocation_map) >
BITSET_STREAM_SIZE / 2) {
ctrl->clear_events = 1;
}
while ( ( mirror->offset == serve->size || ctrl->clear_events ) && e.event != BITSET_STREAM_SET ) {
while ((mirror->offset == serve->size || ctrl->clear_events)
&& e.event != BITSET_STREAM_SET) {
uint64_t events = bitset_stream_size(serve->allocation_map);
if (events == 0) {
@@ -408,11 +402,12 @@ int mirror_setup_next_xfer( struct mirror_ctrl *ctrl )
return 0;
}
debug( "Next transfer: current=%"PRIu64", run=%"PRIu64, current, run );
debug("Next transfer: current=%" PRIu64 ", run=%" PRIu64, current,
run);
struct nbd_request req = {
.magic = REQUEST_MAGIC,
.type = REQUEST_WRITE,
.handle = ".MIRROR.",
.handle.b = ".MIRROR.",
.from = current,
.len = run
};
@@ -460,7 +455,8 @@ static void mirror_write_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
debug( "Mirror write callback invoked with events %d. fd: %i", revents, ctrl->mirror->client );
debug("Mirror write callback invoked with events %d. fd: %i", revents,
ctrl->mirror->client);
/* FIXME: We can end up corking multiple times in unusual circumstances; this
* is annoying, but harmless */
@@ -472,7 +468,8 @@ static void mirror_write_cb( struct ev_loop *loop, ev_io *w, int revents )
data_loc = ((char *) &xfer->hdr.req_raw) + ctrl->xfer.written;
to_write = hdr_size - xfer->written;
} else {
data_loc = ctrl->mirror->mapped + xfer->from + ( xfer->written - hdr_size );
data_loc =
ctrl->mirror->mapped + xfer->from + (xfer->written - hdr_size);
to_write = xfer->len - (ctrl->xfer.written - hdr_size);
}
@@ -485,14 +482,14 @@ static void mirror_write_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
debug("Wrote %" PRIu64 " bytes", count);
debug( "to_write was %"PRIu64", xfer->written was %"PRIu64, to_write, xfer->written );
debug("to_write was %" PRIu64 ", xfer->written was %" PRIu64, to_write,
xfer->written);
// We wrote some bytes, so reset the timer and keep track for the next pass
if (count > 0) {
ctrl->xfer.written += count;
ev_timer_again(ctrl->ev_loop, &ctrl->timeout_watcher);
}
// All bytes written, so now we need to read the NBD reply back.
if (ctrl->xfer.written == ctrl->xfer.len + hdr_size) {
sock_set_tcp_cork(ctrl->mirror->client, 0);
@@ -523,10 +520,13 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
ssize_t count;
uint64_t left = sizeof(struct nbd_reply_raw) - xfer->read;
debug( "Mirror read callback invoked with events %d. fd:%i", revents, m->client );
debug("Mirror read callback invoked with events %d. fd:%i", revents,
m->client);
/* Start / continue reading the NBD response from the mirror. */
if ( ( count = read( m->client, ((void*) &xfer->hdr.rsp_raw) + xfer->read, left ) ) < 0 ) {
if ((count =
read(m->client, ((void *) &xfer->hdr.rsp_raw) + xfer->read,
left)) < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
warn(SHOW_ERRNO("Couldn't read from listener"));
ev_break(loop, EVBREAK_ONE);
@@ -540,12 +540,12 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
ev_break(loop, EVBREAK_ONE);
return;
}
// We read some bytes, so reset the timer
ev_timer_again(ctrl->ev_loop, &ctrl->timeout_watcher);
debug("Read %i bytes", count);
debug( "left was %"PRIu64", xfer->read was %"PRIu64, left, xfer->read );
debug("left was %" PRIu64 ", xfer->read was %" PRIu64, left,
xfer->read);
xfer->read += count;
if (xfer->read < sizeof(struct nbd_reply_raw)) {
@@ -568,7 +568,7 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
if ( memcmp( ".MIRROR.", &rsp.handle[0], 8 ) != 0 ) {
if (memcmp(".MIRROR.", rsp.handle.b, 8) != 0) {
warn("Bad handle returned from listener");
ev_break(loop, EVBREAK_ONE);
return;
@@ -602,7 +602,9 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
debug("next_xfer: %d", next_xfer);
/* Regardless of time estimates, if there's no waiting transfer, we can start closing clients down. */
if ( !ctrl->clients_closed && ( !next_xfer || server_mirror_eta( ctrl->serve ) < MS_CONVERGE_TIME_SECS ) ) {
if (!ctrl->clients_closed
&& (!next_xfer
|| server_mirror_eta(ctrl->serve) < MS_CONVERGE_TIME_SECS)) {
info("Closing clients to allow mirroring to converge");
server_forbid_new_clients(ctrl->serve);
server_close_clients(ctrl->serve);
@@ -643,7 +645,8 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
static void mirror_timeout_cb( struct ev_loop *loop, ev_timer *w __attribute__((unused)), int revents )
static void mirror_timeout_cb(struct ev_loop *loop, ev_timer * w
__attribute__ ((unused)), int revents)
{
if (!(revents & EV_TIMER)) {
warn("Mirror timeout called but no timer event signalled");
@@ -673,7 +676,8 @@ static void mirror_abandon_cb( struct ev_loop *loop, ev_io *w, int revents )
}
static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
static void mirror_limit_cb(struct ev_loop *loop, ev_timer * w,
int revents)
{
struct mirror_ctrl *ctrl = (struct mirror_ctrl *) w->data;
NULLCHECK(ctrl);
@@ -684,7 +688,8 @@ static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
}
if (mirror_should_wait(ctrl)) {
debug( "max_bps exceeded, waiting", ctrl->mirror->max_bytes_per_second );
debug("max_bps exceeded, waiting",
ctrl->mirror->max_bytes_per_second);
ev_timer_again(loop, w);
} else {
/* We're below the limit, so do the next request */
@@ -701,7 +706,8 @@ static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
* if it has, start migrating. If it's not finished, then enabling the bitset
* stream does not go well for us.
*/
static void mirror_begin_cb( struct ev_loop *loop, ev_timer *w, int revents )
static void mirror_begin_cb(struct ev_loop *loop, ev_timer * w,
int revents)
{
struct mirror_ctrl *ctrl = (struct mirror_ctrl *) w->data;
NULLCHECK(ctrl);
@@ -711,7 +717,8 @@ static void mirror_begin_cb( struct ev_loop *loop, ev_timer *w, int revents )
return;
}
if ( ctrl->serve->allocation_map_built || ctrl->serve->allocation_map_not_built ) {
if (ctrl->serve->allocation_map_built
|| ctrl->serve->allocation_map_not_built) {
info("allocation map builder is finished, beginning migration");
ev_timer_stop(loop, w);
/* Start by writing xfer 0 to the listener */
@@ -796,10 +803,8 @@ void mirror_run( struct server *serve )
ctrl.abandon_watcher.data = (void *) &ctrl;
ev_io_start(ctrl.ev_loop, &ctrl.abandon_watcher);
ERROR_UNLESS(
mirror_setup_next_xfer( &ctrl ),
"Couldn't find first transfer for mirror!"
);
ERROR_UNLESS(mirror_setup_next_xfer(&ctrl),
"Couldn't find first transfer for mirror!");
if (serve->allocation_map_built) {
@@ -830,7 +835,8 @@ void mirror_run( struct server *serve )
* it to something sane - they just terminate the event loop with state !=
* MS_DONE. We re-allow new clients here if necessary.
*/
if ( m->action_at_finish == ACTION_NOTHING || m->commit_state != MS_DONE ) {
if (m->action_at_finish == ACTION_NOTHING
|| m->commit_state != MS_DONE) {
server_allow_new_clients(serve);
}
@@ -896,7 +902,9 @@ void* mirror_runner(void* serve_params_uncast)
time_t start_time = time(NULL);
int connected = mirror_connect(mirror, serve->size);
mirror_signal_commit(mirror);
if ( !connected ) { goto abandon_mirror; }
if (!connected) {
goto abandon_mirror;
}
/* After this point, if we see a failure we need to disconnect
* and retry everything from mirror_set_state(_, MS_INIT), but
@@ -922,7 +930,7 @@ void* mirror_runner(void* serve_params_uncast)
* for us ). But if we've failed and are going to retry on the next run, we
* must close this socket here to have any chance of it succeeding.
*/
if ( !mirror->client < 0 ) {
if (!(mirror->client < 0)) {
sock_try_close(mirror->client);
mirror->client = -1;
}
@@ -932,38 +940,33 @@ abandon_mirror:
}
struct mirror_super * mirror_super_create(
const char * filename,
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,
enum mirror_finish_action
action_at_finish,
struct mbox *state_mbox)
{
struct mirror_super *super = xmalloc(sizeof(struct mirror_super));
super->mirror = mirror_create(
filename,
super->mirror = mirror_create(filename,
connect_to,
connect_from,
max_Bps,
action_at_finish,
mbox_create() ) ;
action_at_finish, mbox_create());
super->state_mbox = state_mbox;
return super;
}
/* Post the current state of the mirror into super->state_mbox.*/
void mirror_super_signal_committed(
struct mirror_super * super ,
void mirror_super_signal_committed(struct mirror_super *super,
enum mirror_state commit_state)
{
NULLCHECK(super);
NULLCHECK(super->state_mbox);
mbox_post_mirror_state(
super->state_mbox,
commit_state );
mbox_post_mirror_state(super->state_mbox, commit_state);
}
@@ -997,8 +1000,7 @@ void * mirror_super_runner( void * serve_uncast )
struct mirror_super *super = serve->mirror_super;
do {
FATAL_IF( 0 != pthread_create(
&mirror->thread,
FATAL_IF(0 != pthread_create(&mirror->thread,
NULL,
mirror_runner,
serve),
@@ -1018,9 +1020,7 @@ void * mirror_super_runner( void * serve_uncast )
should_retry = *commit_state == MS_GO;
/* Only send this signal the first time */
mirror_super_signal_committed(
super,
*commit_state);
mirror_super_signal_committed(super, *commit_state);
debug("Mirror supervisor committed");
}
/* We only care about the value of the commit signal on

View File

@@ -127,15 +127,13 @@ struct mirror_super {
struct server;
struct flexnbd;
struct mirror_super * mirror_super_create(
const char * filename,
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
);
enum mirror_finish_action
action_at_finish,
struct mbox *state_mbox);
void *mirror_super_runner(void *serve_uncast);
#endif

View File

@@ -19,6 +19,7 @@ static struct option serve_options[] = {
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"
@@ -28,10 +29,9 @@ static char serve_help_text[] =
"\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;
"\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[] = {
@@ -45,6 +45,7 @@ static struct option listen_options[] = {
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"
@@ -54,9 +55,7 @@ static char listen_help_text[] =
"\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;
SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option read_options[] = {
GETOPT_HELP,
@@ -69,6 +68,7 @@ static struct option read_options[] = {
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"
@@ -78,9 +78,7 @@ static char read_help_text[] =
"\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;
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option *write_options = read_options;
@@ -93,9 +91,7 @@ static char write_help_text[] =
"\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;
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option acl_options[] = {
GETOPT_HELP,
@@ -104,14 +100,12 @@ static struct option acl_options[] = {
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;
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option mirror_speed_options[] = {
GETOPT_HELP,
@@ -121,15 +115,12 @@ static struct option mirror_speed_options[] = {
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;
HELP_LINE SOCK_LINE MAX_SPEED_LINE VERBOSE_LINE QUIET_LINE;
static struct option mirror_options[] = {
GETOPT_HELP,
@@ -142,6 +133,7 @@ static struct option mirror_options[] = {
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"
@@ -151,9 +143,7 @@ static char mirror_help_text[] =
"\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;
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option break_options[] = {
GETOPT_HELP,
@@ -162,14 +152,12 @@ static struct option break_options[] = {
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;
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option status_options[] = {
@@ -179,14 +167,12 @@ static struct option status_options[] = {
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;
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
char help_help_text_arr[] =
"Usage: flexnbd <cmd> [cmd options]\n\n"
@@ -200,8 +186,7 @@ char help_help_text_arr[] =
"\tflexnbd mirror-speed\n"
"\tflexnbd break\n"
"\tflexnbd status\n"
"\tflexnbd help\n\n"
"See flexnbd help <cmd> for further info\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
*/
@@ -214,7 +199,8 @@ 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 )
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':
@@ -254,9 +240,7 @@ void read_serve_param( int c, char **ip_addr, char **ip_port, char **file, char
void read_listen_param(int c,
char **ip_addr,
char **ip_port,
char **file,
char **sock,
int *default_deny )
char **file, char **sock, int *default_deny)
{
switch (c) {
case 'h':
@@ -289,7 +273,9 @@ void read_listen_param( int c,
}
}
void read_readwrite_param( int c, char **ip_addr, char **ip_port, char **bind_addr, char **from, char **size, char *err_text )
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':
@@ -348,11 +334,7 @@ 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
)
void read_mirror_speed_param(int c, char **sock, char **max_speed)
{
switch (c) {
case 'h':
@@ -377,13 +359,10 @@ void read_mirror_speed_param(
}
}
void read_mirror_param(
int c,
void read_mirror_param(int c,
char **sock,
char **ip_addr,
char **ip_port,
int *unlink,
char **bind_addr )
char **ip_port, int *unlink, char **bind_addr)
{
switch (c) {
case 'h':
@@ -459,10 +438,14 @@ int mode_serve( int argc, char *argv[] )
struct flexnbd *flexnbd;
while (1) {
c = getopt_long(argc, argv, serve_short_options, serve_options, NULL);
if ( c == -1 ) { break; }
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 );
read_serve_param(c, &ip_addr, &ip_port, &file, &sock,
&default_deny, &use_killswitch);
}
if (NULL == ip_addr || NULL == ip_port) {
@@ -473,9 +456,14 @@ int mode_serve( int argc, char *argv[] )
err = 1;
fprintf(stderr, "--file is required\n");
}
if ( err ) { exit_err( serve_help_text ); }
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 );
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);
@@ -499,8 +487,11 @@ int mode_listen( int argc, char *argv[] )
struct flexnbd *flexnbd;
while (1) {
c = getopt_long(argc, argv, listen_short_options, listen_options, NULL);
if ( c == -1 ) { break; }
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);
@@ -514,16 +505,16 @@ int mode_listen( int argc, char *argv[] )
err = 1;
fprintf(stderr, "--file is required\n");
}
if ( err ) { exit_err( listen_help_text ); }
if (err) {
exit_err(listen_help_text);
}
flexnbd = flexnbd_create_listening(
ip_addr,
flexnbd = flexnbd_create_listening(ip_addr,
ip_port,
file,
sock,
default_deny,
argc - optind,
argv + optind);
argc - optind, argv + optind);
success = flexnbd_serve(flexnbd);
flexnbd_destroy(flexnbd);
@@ -545,29 +536,25 @@ int mode_listen( int argc, char *argv[] )
* char *s_length,
* char *s_filename )
*/
void params_readwrite(
int write_not_read,
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
)
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
);
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) {
parse_ip_to_sockaddr(&out->connect_from.generic,
s_bind_address) == 0) {
fatal("Couldn't parse bind address '%s'", s_bind_address);
}
@@ -575,30 +562,27 @@ void params_readwrite(
long signed_from = atol(s_from);
FATAL_IF_NEGATIVE(signed_from,
"Can't read from a negative offset %d.", 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);
} 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);
"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
);
FATAL_IF_NEGATIVE(lseek64(out->data_fd, 0, SEEK_SET),
"Couldn't rewind %s", s_length_or_filename);
}
}
else {
} else {
out->len = atol(s_length_or_filename);
out->data_fd = 1;
}
@@ -618,11 +602,15 @@ int mode_read( int argc, char *argv[] )
struct mode_readwrite_params readwrite;
while (1) {
c = getopt_long(argc, argv, read_short_options, read_options, NULL);
c = getopt_long(argc, argv, read_short_options, read_options,
NULL);
if ( c == -1 ) { break; }
if (c == -1) {
break;
}
read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size, read_help_text );
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
&size, read_help_text);
}
if (NULL == ip_addr || NULL == ip_port) {
@@ -633,10 +621,13 @@ int mode_read( int argc, char *argv[] )
err = 1;
fprintf(stderr, "both --from and --size are required.\n");
}
if ( err ) { exit_err( read_help_text ); }
if (err) {
exit_err(read_help_text);
}
memset(&readwrite, 0, sizeof(readwrite));
params_readwrite( 0, &readwrite, ip_addr, ip_port, bind_addr, from, size );
params_readwrite(0, &readwrite, ip_addr, ip_port, bind_addr, from,
size);
do_read(&readwrite);
return 0;
}
@@ -654,10 +645,14 @@ int mode_write( int argc, char *argv[] )
struct mode_readwrite_params readwrite;
while (1) {
c = getopt_long(argc, argv, write_short_options, write_options, NULL);
if ( c == -1 ) { break; }
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 );
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
&size, write_help_text);
}
if (NULL == ip_addr || NULL == ip_port) {
@@ -668,10 +663,13 @@ int mode_write( int argc, char *argv[] )
err = 1;
fprintf(stderr, "both --from and --size are required.\n");
}
if ( err ) { exit_err( write_help_text ); }
if (err) {
exit_err(write_help_text);
}
memset(&readwrite, 0, sizeof(readwrite));
params_readwrite( 1, &readwrite, ip_addr, ip_port, bind_addr, from, size );
params_readwrite(1, &readwrite, ip_addr, ip_port, bind_addr, from,
size);
do_write(&readwrite);
return 0;
}
@@ -683,7 +681,9 @@ int mode_acl( int argc, char *argv[] )
while (1) {
c = getopt_long(argc, argv, acl_short_options, acl_options, NULL);
if ( c == -1 ) { break; }
if (c == -1) {
break;
}
read_acl_param(c, &sock);
}
@@ -708,8 +708,11 @@ int mode_mirror_speed( int argc, char *argv[] )
char *speed = NULL;
while (1) {
c = getopt_long( argc, argv, mirror_speed_short_options, mirror_speed_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, mirror_speed_short_options,
mirror_speed_options, NULL);
if (-1 == c) {
break;
}
read_mirror_speed_param(c, &sock, &speed);
}
@@ -739,14 +742,15 @@ int mode_mirror( int argc, char *argv[] )
remote_argv[2] = "exit";
while (1) {
c = getopt_long( argc, argv, mirror_short_options, mirror_options, NULL);
if ( -1 == c ) { break; }
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] );
&remote_argv[1], &unlink, &remote_argv[3]);
}
if (NULL == sock) {
@@ -757,13 +761,16 @@ int mode_mirror( int argc, char *argv[] )
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 (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 {
} else {
do_remote_command("mirror", sock, 4, remote_argv);
}
@@ -777,8 +784,11 @@ int mode_break( int argc, char *argv[] )
char *sock = NULL;
while (1) {
c = getopt_long( argc, argv, break_short_options, break_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, break_short_options, break_options,
NULL);
if (-1 == c) {
break;
}
read_break_param(c, &sock);
}
@@ -798,8 +808,11 @@ int mode_status( int argc, char *argv[] )
char *sock = NULL;
while (1) {
c = getopt_long( argc, argv, status_short_options, status_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, status_short_options, status_options,
NULL);
if (-1 == c) {
break;
}
read_status_param(c, &sock);
}
@@ -836,7 +849,9 @@ int mode_help( int argc, char *argv[] )
help_text = mirror_help_text;
} else if (IS_CMD(CMD_STATUS, cmd)) {
help_text = status_help_text;
} else { exit_err( help_help_text ); }
} else {
exit_err(help_help_text);
}
}
fprintf(stdout, "%s\n", help_text);
@@ -848,37 +863,27 @@ 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 ) ) {
} else if (IS_CMD(CMD_LISTEN, mode)) {
exit(mode_listen(argc, argv));
}
else if ( IS_CMD( CMD_READ, mode ) ) {
} else if (IS_CMD(CMD_READ, mode)) {
mode_read(argc, argv);
}
else if ( IS_CMD( CMD_WRITE, mode ) ) {
} else if (IS_CMD(CMD_WRITE, mode)) {
mode_write(argc, argv);
}
else if ( IS_CMD( CMD_ACL, mode ) ) {
} 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 ) ) {
} else if (IS_CMD(CMD_MIRROR, mode)) {
mode_mirror(argc, argv);
}
else if ( IS_CMD( CMD_BREAK, mode ) ) {
} else if (IS_CMD(CMD_BREAK, mode)) {
mode_break(argc, argv);
}
else if ( IS_CMD( CMD_STATUS, mode ) ) {
} else if (IS_CMD(CMD_STATUS, mode)) {
mode_status(argc, argv);
}
else if ( IS_CMD( CMD_HELP, mode ) ) {
} else if (IS_CMD(CMD_HELP, mode)) {
mode_help(argc - 1, argv + 1);
}
else {
} else {
mode_help(argc - 1, argv + 1);
exit(1);
}
exit(0);
}

View File

@@ -21,8 +21,7 @@
#include <sys/socket.h>
#include <netinet/tcp.h>
struct server * server_create (
struct flexnbd * flexnbd,
struct server *server_create(struct flexnbd *flexnbd,
char *s_ip_address,
char *s_port,
char *s_file,
@@ -30,8 +29,7 @@ struct server * server_create (
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch,
int success)
int use_killswitch, int success)
{
NULLCHECK(flexnbd);
struct server *out;
@@ -43,19 +41,18 @@ struct server * server_create (
server_allow_new_clients(out);
out->nbd_client = xmalloc( max_nbd_clients * sizeof( struct client_tbl_entry ) );
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),
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
);
"you want to bind to all IPs)", s_ip_address);
out->acl = acl_create(acl_entries, s_acl_entries, default_deny);
@@ -78,6 +75,8 @@ struct server * server_create (
NULLCHECK(out->close_signal);
NULLCHECK(out->acl_updated_signal);
log_context = s_file;
return out;
}
@@ -108,8 +107,7 @@ void server_unlink( struct server * serve )
FATAL_IF_NEGATIVE(unlink(serve->filename),
"Failed to unlink %s: %s",
serve->filename,
strerror( errno ) );
serve->filename, strerror(errno));
}
@@ -153,7 +151,8 @@ void server_unlock_start_mirror( struct server *serve )
{
debug("Mirror start unlocking");
SERVER_UNLOCK( serve, l_start_mirror, "Problem with start mirror unlock" );
SERVER_UNLOCK(serve, l_start_mirror,
"Problem with start mirror unlock");
}
int server_start_mirror_locked(struct server *serve)
@@ -184,8 +183,9 @@ 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);
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");
@@ -197,35 +197,32 @@ void serve_open_server_socket(struct server* params)
* 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"
);
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"
);
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 ),
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"
);
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 **) )
int tryjoin_client_thread(struct client_tbl_entry *entry,
int (*joinfunc) (pthread_t, void **))
{
NULLCHECK(entry);
@@ -237,25 +234,25 @@ int tryjoin_client_thread( struct client_tbl_entry *entry, int (*joinfunc)(pthre
if (entry->thread != 0) {
char s_client_address[128];
sockaddr_address_string( &entry->address.generic, &s_client_address[0], 128 );
sockaddr_address_string(&entry->address.generic,
&s_client_address[0], 128);
debug( "%s(%p,...)", joinfunc == pthread_join ? "joining" : "tryjoining", entry->thread );
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 );
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 ) {
entry->thread, strerror(join_errno));
} else if (join_errno == 0) {
debug("nbd thread %016x exited (%s) with status %ld",
entry->thread,
s_client_address,
(uint64_t)status);
entry->thread, s_client_address, (uintptr_t) status);
client_destroy(entry->client);
entry->client = NULL;
entry->thread = 0;
@@ -285,7 +282,8 @@ 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 )
void cleanup_client_threads(struct client_tbl_entry *entries,
size_t entries_len)
{
size_t i;
for (i = 0; i < entries_len; i++) {
@@ -331,6 +329,8 @@ 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++;
@@ -345,7 +345,8 @@ int server_count_clients( struct server *params )
* 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 )
int server_acl_accepts(struct server *params,
union mysockaddr *client_address)
{
NULLCHECK(params);
NULLCHECK(client_address);
@@ -364,8 +365,7 @@ int server_acl_accepts( struct server *params, union mysockaddr * client_address
}
int server_should_accept_client(
struct server * params,
int server_should_accept_client(struct server *params,
union mysockaddr *client_address,
char *s_client_address,
size_t s_client_address_len)
@@ -374,9 +374,9 @@ int server_should_accept_client(
NULLCHECK(client_address);
NULLCHECK(s_client_address);
const char* result = sockaddr_address_string(
&client_address->generic, s_client_address, s_client_address_len
);
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);
@@ -384,7 +384,8 @@ int server_should_accept_client(
}
if (!server_acl_accepts(params, client_address)) {
warn( "Rejecting client %s: Access control error", s_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"));
@@ -396,11 +397,11 @@ int server_should_accept_client(
int spawn_client_thread(
struct client * client_params,
int spawn_client_thread(struct client *client_params,
pthread_t * out_thread)
{
int result = pthread_create(out_thread, NULL, client_serve, client_params);
int result =
pthread_create(out_thread, NULL, client_serve, client_params);
return result;
}
@@ -410,10 +411,8 @@ int spawn_client_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)
void accept_nbd_client(struct server *params,
int client_fd, union mysockaddr *client_address)
{
NULLCHECK(params);
NULLCHECK(client_address);
@@ -422,8 +421,15 @@ void accept_nbd_client(
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 ) ) {
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);
@@ -457,7 +463,8 @@ void accept_nbd_client(
return;
}
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread, s_client_address);
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread,
s_client_address);
}
@@ -478,8 +485,12 @@ void server_audit_clients( struct server * serve)
*/
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; }
if (0 == entry->thread) {
continue;
}
if (server_acl_accepts(serve, &entry->address)) {
continue;
}
client_signal_stop(entry->client);
}
}
@@ -539,7 +550,9 @@ void server_replace_acl( struct server *serve, struct acl * new_acl )
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 ); }
if (old_acl) {
acl_destroy(old_acl);
}
}
server_unlock_acl(serve);
@@ -607,12 +620,13 @@ int server_accept( struct server * params )
FD_ZERO(&fds);
FD_SET(params->server_fd, &fds);
if( 0 < signal_fd ) { FD_SET(signal_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),
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
SHOW_ERRNO("select() failed")
);
@@ -625,7 +639,8 @@ int server_accept( struct server * params )
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 );
params->success = params->success
&& serve_shutdown_is_graceful(params);
should_continue = 0;
}
@@ -636,7 +651,8 @@ int server_accept( struct server * params )
}
if (FD_ISSET(params->server_fd, &fds)) {
int client_fd = accept( params->server_fd, &client_address.generic, &socklen );
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);
@@ -671,8 +687,7 @@ void* build_allocation_map_thread(void* serve_uncast)
if (build_allocation_map(serve->allocation_map, fd)) {
serve->allocation_map_built = 1;
}
else {
} 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
@@ -705,9 +720,17 @@ void serve_init_allocation_map(struct server* params)
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 );
FATAL_IF_NEGATIVE(size, "Couldn't find size of %s", params->filename);
params->allocation_map =
bitset_alloc(params->size, block_allocation_resolution);
@@ -733,7 +756,8 @@ void server_allow_new_clients( struct server * serve )
return;
}
void server_join_clients( struct server * serve ) {
void server_join_clients(struct server *serve)
{
int i;
void *status;
@@ -746,7 +770,8 @@ void server_join_clients( struct server * serve ) {
if (0 == err) {
serve->nbd_client[i].thread = 0;
} else {
warn( "Error %s (%i) joining thread %p", strerror( err ), err, thread_id );
warn("Error %s (%i) joining thread %p", strerror(err), err,
thread_id);
}
}
}
@@ -798,7 +823,9 @@ void serve_cleanup(struct server* params,
info("cleaning up");
if (params->server_fd){ close(params->server_fd); }
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);
@@ -807,14 +834,18 @@ void serve_cleanup(struct server* params,
int need_mirror_lock;
need_mirror_lock = !server_start_mirror_locked(params);
if ( need_mirror_lock ) { server_lock_start_mirror( 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 ); }
if (need_mirror_lock) {
server_unlock_start_mirror(params);
}
server_join_clients(params);
@@ -859,8 +890,11 @@ 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 );
bitset_stream_queued_bytes(serve->allocation_map,
BITSET_STREAM_SET) + (serve->size -
serve->
mirror->
offset);
return bytes_to_xfer;
}
@@ -906,10 +940,8 @@ void server_abandon_mirror( struct server * serve )
* 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"
);
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);
@@ -943,7 +975,9 @@ int do_serve( struct server* params, struct self_pipe * open_signal )
/* Only signal that we are open for business once the server
socket is open */
if ( NULL != open_signal ) { self_pipe_signal( open_signal ); }
if (NULL != open_signal) {
self_pipe_signal(open_signal);
}
serve_init_allocation_map(params);
serve_accept_loop(params);

View File

@@ -21,6 +21,9 @@ struct client_tbl_entry {
#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;
@@ -100,8 +103,7 @@ struct server {
int success;
};
struct server * server_create(
struct flexnbd * flexnbd,
struct server *server_create(struct flexnbd *flexnbd,
char *s_ip_address,
char *s_port,
char *s_file,
@@ -109,8 +111,7 @@ struct server * server_create(
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch,
int success );
int use_killswitch, int success);
void server_destroy(struct server *);
int server_is_closed(struct server *serve);
void serve_signal_close(struct server *serve);
@@ -164,4 +165,3 @@ struct mode_readwrite_params {
#endif

View File

@@ -21,16 +21,20 @@ struct status * status_create( struct server * serve )
if (status->is_mirroring) {
status->migration_duration = monotonic_time_ms();
if ( ( serve->mirror->migration_started ) < status->migration_duration ) {
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_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);
@@ -60,6 +64,7 @@ int status_write( struct status * status, int fd )
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);
};
@@ -75,4 +80,3 @@ void status_destroy( struct status * status )
NULLCHECK(status);
free(status);
}

View File

@@ -64,6 +64,8 @@
* 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.
*/
@@ -84,6 +86,7 @@ struct status {
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. */
@@ -98,4 +101,3 @@ void status_destroy( struct status * );
#endif

View File

@@ -1,5 +1,3 @@
# encoding: utf-8
require 'flexnbd'
require 'file_writer'
@@ -9,18 +7,23 @@ class Environment
def initialize
@blocksize = 1024
@filename1 = "/tmp/.flexnbd.test.#{$$}.#{Time.now.to_i}.1"
@filename2 = "/tmp/.flexnbd.test.#{$$}.#{Time.now.to_i}.2"
@ip = "127.0.0.1"
@available_ports = [*40000..41000] - listening_ports
@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)
@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
@@ -29,11 +32,11 @@ class Environment
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
@@ -42,7 +45,6 @@ class Environment
@nbd2.serve(@filename2, *acl)
end
def listen1(*acl)
@nbd1.listen(@filename1, *(acl.empty? ? @acl1 : acl))
end
@@ -51,7 +53,6 @@ class Environment
@nbd2.listen(@filename2, *acl)
end
def break1
@nbd1.break
end
@@ -64,7 +65,6 @@ class Environment
@nbd2.acl(*acl)
end
def status1
@nbd1.status.first
end
@@ -73,8 +73,6 @@ class Environment
@nbd2.status.first
end
def mirror12
@nbd1.mirror(@nbd2.ip, @nbd2.port)
end
@@ -87,7 +85,6 @@ class Environment
@nbd1.mirror_unlink(@nbd2.ip, @nbd2.port, 2)
end
def write1(data)
@nbd1.write(0, data)
end
@@ -100,20 +97,17 @@ class Environment
@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 }
`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
@@ -122,41 +116,35 @@ class Environment
end
end
@nbd1.can_die(0)
@nbd1.kill
@nbd2.kill
[@filename1, @filename2].each do |f|
File.unlink(f) if File.exists?(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 { |fn|
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
raise 'no addr' unless addr
raise 'no port' unless port
@fake_pid = fork do
exec [fake, addr, port, @nbd1.pid, sock].map{|x| x.to_s}.join(" ")
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

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# Open a server, accept a client, then cancel the migration by issuing
# a break command.
@@ -13,23 +11,22 @@ client = server.accept
ctrl = UNIXSocket.open(sock)
Process.kill("STOP", src_pid.to_i)
Process.kill('STOP', src_pid.to_i)
ctrl.write("break\n")
ctrl.close_write
client.write_hello
Process.kill("CONT", src_pid.to_i)
Process.kill('CONT', src_pid.to_i)
fail "Unexpected control response" unless
raise 'Unexpected control response' unless
ctrl.read =~ /0: mirror stopped/
client2 = nil
begin
client2 = server.accept( "Expected timeout" )
fail "Unexpected reconnection"
client2 = server.accept('Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
client.close
exit(0)

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# Receive a mirror, and disconnect after sending the entrust reply but
# before it can send the disconnect signal.
#
@@ -15,7 +13,7 @@ server = FakeDest.new( addr, port )
client = server.accept
client.write_hello
while (req = client.read_request; req[:type] == 1)
while req = client.read_request; req[:type] == 1
client.read_data(req[:len])
client.write_reply(req[:handle])
end
@@ -26,11 +24,10 @@ client.close
system "kill -CONT #{src_pid}"
sleep(0.25)
client2 = server.accept( "Timed out waiting for a reconnection" )
client2 = server.accept('Timed out waiting for a reconnection')
client2.close
server.close
$stderr.puts "done"
warn 'done'
exit(0)

View File

@@ -11,11 +11,11 @@ include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept( "Timed out waiting for a connection" )
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 = server.accept('Timed out waiting for a reconnection')
new_client.close
server.close

View File

@@ -12,12 +12,12 @@ include FlexNBD
addr, port = *ARGV
server = FakeDest.new(addr, port)
client = server.accept( "Timed out waiting for a connection" )
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 = server.accept('Timed out waiting for a reconnection')
new_client.close
server.close

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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
@@ -17,9 +15,9 @@ client.write_hello
req = client.read_request
data = client.read_data(req[:len])
Process.kill("STOP", src_pid.to_i)
Process.kill('STOP', src_pid.to_i)
client.write_reply(req[:handle], 0)
client.close
Process.kill("CONT", src_pid.to_i)
Process.kill('CONT', src_pid.to_i)
exit(0)

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
require 'flexnbd/fake_dest'
include FlexNBD
@@ -12,8 +10,7 @@ client.write_hello
handle = client.read_request[:handle]
client.write_error(handle)
client2 = server.accept( "Timed out waiting for a reconnection" )
client2 = server.accept('Timed out waiting for a reconnection')
client.close
client2.close

View File

@@ -26,8 +26,8 @@ client.close
# Invert the sense of the timeout exception, since we *don't* want a
# connection attempt
begin
server.accept( "Expected timeout" )
fail "Unexpected reconnection"
server.accept('Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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.
@@ -15,12 +13,12 @@ client1.write_hello
client1.read_request
t = Thread.start do
client2 = server.accept( "Timed out waiting for a reconnection",
client2 = server.accept('Timed out waiting for a reconnection',
FlexNBD::MS_REQUEST_LIMIT_SECS + 2)
client2.close
end
sleep_time = if ENV.has_key?('FLEXNBD_MS_REQUEST_LIMIT_SECS')
sleep_time = if ENV.key?('FLEXNBD_MS_REQUEST_LIMIT_SECS')
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'].to_f
else
FlexNBD::MS_REQUEST_LIMIT_SECS

View File

@@ -13,15 +13,15 @@ client1 = server.accept
# We don't expect a reconnection attempt.
t = Thread.new do
begin
client2 = server.accept( "Timed out waiting for a reconnection",
client2 = server.accept('Timed out waiting for a reconnection',
FlexNBD::MS_RETRY_DELAY_SECS + 1)
fail "Unexpected reconnection"
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
end
client1.write_hello( :magic => :wrong )
client1.write_hello(magic: :wrong)
t.join

View File

@@ -18,21 +18,21 @@ t = Thread.new do
# 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",
client2 = server.accept('Timed out waiting for a reconnection',
FlexNBD::MS_RETRY_DELAY_SECS + 1)
client2.close
fail "Unexpected reconnection."
raise 'Unexpected reconnection.'
rescue Timeout::Error
end
end
client.write_hello( :size => :wrong )
client.write_hello(size: :wrong)
t.join
# Now check that the source closed the first socket (yes, this was an
# actual bug)
fail "Didn't close socket" unless client.disconnected?
raise "Didn't close socket" unless client.disconnected?
exit 0

View File

@@ -10,15 +10,13 @@ addr, port = *ARGV
server = FakeDest.new(addr, port)
server.accept.close
begin
server.accept
fail "Unexpected reconnection"
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
server.close
exit(0)

View File

@@ -9,7 +9,7 @@ include FlexNBD
addr, port, pid = *ARGV
server = FakeDest.new(addr, port)
client = server.accept( "Timed out waiting for a connection" )
client = server.accept('Timed out waiting for a connection')
client.write_hello
Process.kill(15, pid.to_i)

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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.
@@ -15,7 +13,7 @@ client = server.accept
client.write_hello
req = client.read_request
client.read_data(req[:len])
client.write_reply( req[:handle], 0, :magic => :wrong )
client.write_reply(req[:handle], 0, magic: :wrong)
client2 = server.accept
client.close

View File

@@ -11,13 +11,13 @@ include FlexNBD
addr, port = *ARGV
FakeSource.new( addr, port, "Failed to connect" ).close
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
FakeSource.new(addr, port, 'Failed to reconnect').close
exit 0

View File

@@ -11,10 +11,10 @@ include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
client.write_write_request(0, 8)
client.write_data( "12345678" )
client.write_data('12345678')
# Use system "kill" rather than Process.kill because Process.kill
# doesn't seem to work
@@ -25,12 +25,11 @@ client.close
system "kill -CONT #{srv_pid}"
sleep(0.25)
begin
client2 = FakeSource.new( addr, port, "Expected timeout" )
fail "Unexpected reconnection"
client2 = FakeSource.new(addr, port, 'Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end

View File

@@ -10,10 +10,10 @@ include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
client.write_write_request(0, 8)
client.write_data( "12345678" )
client.write_data('12345678')
client.write_entrust_request
client.read_response
@@ -21,13 +21,11 @@ client.close
sleep(0.25)
begin
client2 = FakeSource.new( addr, port, "Expected timeout" )
fail "Unexpected reconnection"
client2 = FakeSource.new(addr, port, 'Expected timeout')
raise 'Unexpected reconnection'
rescue Timeout::Error
# expected
end
exit(0)

View File

@@ -12,13 +12,12 @@ include FlexNBD
addr, port = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting." )
client = FakeSource.new(addr, port, 'Timed out connecting.')
client.read_hello
client.close
sleep(0.2)
FakeSource.new( addr, port, "Timed out reconnecting." )
FakeSource.new(addr, port, 'Timed out reconnecting.')
exit(0)

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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.
@@ -10,7 +8,7 @@ include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
system "kill -STOP #{srv_pid}"
@@ -24,7 +22,7 @@ system "kill -CONT #{srv_pid}"
sleep(0.25)
# ...and can we reconnect?
client2 = FakeSource.new( addr, port, "Timed out connecting" )
client2 = FakeSource.new(addr, port, 'Timed out connecting')
client2.close
exit(0)

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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.
@@ -10,13 +8,13 @@ include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
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.write_data('12345678')
client.close
system "kill -CONT #{srv_pid}"
@@ -27,7 +25,7 @@ system "kill -CONT #{srv_pid}"
sleep(0.25)
# ...and can we reconnect?
client2 = FakeSource.new( addr, port, "Timed out reconnecting" )
client2 = FakeSource.new(addr, port, 'Timed out reconnecting')
client2.close
exit(0)

View File

@@ -8,10 +8,9 @@ include FlexNBD
addr, port, srv_pid, newaddr, newport = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client = FakeSource.new(addr, port, 'Timed out connecting')
client.write_read_request(0, 8)
client.read_raw(4)
client.close
exit(0)

View File

@@ -10,9 +10,9 @@ include FlexNBD
addr, port = *ARGV
client1 = FakeSource.new( addr, port, "Timed out connecting" )
client1 = FakeSource.new(addr, port, 'Timed out connecting')
sleep(0.25)
client2 = FakeSource.new( addr, port, "Timed out connecting a second time" )
client2 = FakeSource.new(addr, port, 'Timed out connecting a second time')
# This is the expected source crashing after connect
client1.close

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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.
@@ -11,10 +9,9 @@ include FlexNBD
addr, port = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting", "127.0.0.6" )
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

@@ -7,10 +7,10 @@
# listening for an incoming migration.
addr, port = *ARGV
require "flexnbd/fake_source"
require 'flexnbd/fake_source'
include FlexNBD
client = FakeSource.new( addr, port, "Timed out connecting" )
client = FakeSource.new(addr, port, 'Timed out connecting')
client.read_hello
# Now we do two things:
@@ -25,7 +25,7 @@ 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 = FakeSource.new(addr, port, 'Timed out reconnecting.')
new_client.read_hello
exit 0
end

View File

@@ -9,10 +9,10 @@ include FlexNBD
addr, port, pid = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting." )
client = FakeSource.new(addr, port, 'Timed out connecting.')
client.read_hello
Process.kill( "TERM", pid.to_i )
Process.kill('TERM', pid.to_i)
sleep(0.2)
client.close

View File

@@ -9,10 +9,9 @@ include FlexNBD
addr, port, srv_pid, newaddr, newport = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client.send_mirror()
client = FakeSource.new(addr, port, 'Timed out connecting')
client.send_mirror
sleep(1)
exit(0)

View File

@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
# encoding: utf-8
# 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.
@@ -13,20 +11,20 @@ include FlexNBD
addr, port = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
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)
client.write_write_request(hello[:size] + 1, 32, 'myhandle')
client.write_data('1' * 32)
response = client.read_response
fail "Not an error" if response[:error] == 0
fail "Wrong handle" unless "myhandle" == response[:handle]
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 )
client.write_data('2' * 32)
success_response = client.read_response
fail "Second write failed" unless success_response[:error] == 0
raise 'Second write failed' unless success_response[:error] == 0
client.close
exit(0)

View File

@@ -3,13 +3,13 @@
#
class FileWriter
def initialize(filename, blocksize)
@fh = File.open(filename, "w+")
@fh = File.open(filename, 'w+')
@blocksize = blocksize
@pattern = ""
@pattern = ''
end
def size
@blocksize * @pattern.split("").size
@blocksize * @pattern.split('').size
end
# We write in fixed block sizes, given by "blocksize"
@@ -20,8 +20,8 @@ class FileWriter
def write(data)
@pattern += data
data.split("").each do |code|
if code == "_"
data.split('').each do |code|
if code == '_'
@fh.seek(@blocksize, IO::SEEK_CUR)
else
@fh.write(data(code))
@@ -31,15 +31,14 @@ class FileWriter
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 { |blk, blk_off|
patterns = @pattern.split('')
patterns.zip((0...patterns.length).to_a)
.map do |blk, blk_off|
data(blk, blk_off)
}.join[off...(off+len)]
end.join[off...(off + len)]
end
# Read what's actually in the file
@@ -62,14 +61,14 @@ class FileWriter
def data(code, at = @fh.tell)
case code
when "0", "_"
when '0', '_'
"\0" * @blocksize
when "X"
"X" * @blocksize
when "f"
r = ""
when 'X'
'X' * @blocksize
when 'f'
r = ''
(@blocksize / 4).times do
r += [at].pack("I")
r += [at].pack('I')
at += 4
end
r
@@ -77,51 +76,49 @@ class FileWriter
raise "Unknown character '#{block}'"
end
end
end
if __FILE__==$0
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.open('test_read_original_zeros') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4096)
file.write( "0" )
file.write('0')
assert_equal file.read(0, 4096), file.read_original(0, 4096)
assert( file.untouched?(0,4096) , "Untouched file was touched." )
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.open('test_read_original_offsets') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4096)
file.write( "f" )
file.write('f')
assert_equal file.read(0, 4096), file.read_original(0, 4096)
assert( file.untouched?(0,4096) , "Untouched file was touched." )
assert(file.untouched?(0, 4096), 'Untouched file was touched.')
end
end
def test_file_size
Tempfile.open("test_file_size") do |tempfile|
Tempfile.open('test_file_size') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4096)
file.write( "f" )
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.open('test_read_original_offsets') do |tempfile|
tempfile.close
file = FileWriter.new(tempfile.path, 4)
file.write( "f"*4 )
file.write('f' * 4)
assert_equal 4, file.read_original(0, 4).length
end
end
end
end

View File

@@ -4,28 +4,26 @@ 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 do exec cmd end
@pid = fork { exec cmd }
end
end # class Executor
class ValgrindExecutor
attr_reader :pid
def run(cmd)
@pid = fork do exec "valgrind --track-origins=yes --suppressions=custom.supp #{cmd}" end
@pid = fork { exec "valgrind --track-origins=yes --suppressions=custom.supp #{cmd}" }
end
end # class ValgrindExecutor
class ValgrindKillingExecutor
attr_reader :pid
@@ -34,9 +32,9 @@ class ValgrindKillingExecutor
attr_reader :backtrace
def initialize
@backtrace = []
@what = ""
@kind = ""
@pid = ""
@what = ''
@kind = ''
@pid = ''
end
def add_frame
@@ -58,10 +56,8 @@ class ValgrindKillingExecutor
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)
@@ -74,39 +70,36 @@ class ValgrindKillingExecutor
@text = text
end
def tag_start(tag, attrs)
def tag_start(tag, _attrs)
case tag.to_s
when "error"
when 'error'
@found = true
when "frame"
when 'frame'
@error.add_frame
end
end
def tag_end(tag)
case tag.to_s
when "what"
when 'what'
@error.what = @text if @found
@text = ""
when "kind"
@text = ''
when 'kind'
@error.kind = @text if @found
when "file"
when 'file'
@error.add_file(@text) if @found
when "fn"
when 'fn'
@error.add_fn(@text) if @found
when "line"
when 'line'
@error.add_line(@text) if @found
when "error", "stack"
if @found
@killer.call( @error )
end
when "pid"
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
@@ -124,47 +117,41 @@ class ValgrindKillingExecutor
end
end
def initialize
@pid = nil
end
def run(cmd)
@io_r, io_w = IO.pipe
@pid = fork do exec( "valgrind --suppressions=custom.supp --xml=yes --xml-fd=#{io_w.fileno} " + cmd ) end
@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)
$stderr.puts "*"*72
$stderr.puts "* Valgrind error spotted:"
$stderr.puts err.to_s.split("\n").map{|s| " #{s}"}
$stderr.puts "*"*72
Process.kill( "KILL", @pid )
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)
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.
#
@@ -189,12 +176,11 @@ module FlexNBD
end
end
def build_debug_opt
if @do_debug
"--verbose"
'--verbose'
else
"--quiet"
'--quiet'
end
end
@@ -209,20 +195,19 @@ module FlexNBD
@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)
$stderr.puts msg if debug?
warn msg if debug?
end
def serve_cmd(file, acl)
"#{bin} serve "\
"--addr #{ip} "\
@@ -233,7 +218,6 @@ module FlexNBD
"#{acl.join(' ')}"
end
def listen_cmd(file, acl)
"#{bin} listen "\
"--addr #{ip} "\
@@ -250,11 +234,10 @@ module FlexNBD
"--port #{port} "\
"--conn-addr #{connect_ip} "\
"--conn-port #{connect_port} "\
"#{prefetch_proxy ? "--cache " : ""}"\
"#{prefetch_proxy ? '--cache ' : ''}"\
"#{@debug}"
end
def read_cmd(offset, length)
"#{bin} read "\
"--addr #{ip} "\
@@ -264,7 +247,6 @@ module FlexNBD
"--size #{length}"
end
def write_cmd(offset, data)
"#{bin} write "\
"--addr #{ip} "\
@@ -274,7 +256,6 @@ module FlexNBD
"--size #{data.length}"
end
def base_mirror_opts(dest_ip, dest_port)
"--addr #{dest_ip} "\
"--port #{dest_port} "\
@@ -283,7 +264,7 @@ module FlexNBD
def unlink_mirror_opts(dest_ip, dest_port)
"#{base_mirror_opts(dest_ip, dest_port)} "\
"--unlink "
'--unlink '
end
def base_mirror_cmd(opts)
@@ -316,46 +297,52 @@ module FlexNBD
"#{@bin} acl " \
"--sock #{ctrl} "\
"#{@debug} "\
"#{acl.join " "}"
"#{acl.join ' '}"
end
def run_serve_cmd(cmd)
File.unlink(ctrl) if File.exists?(ctrl)
File.unlink(ctrl) if File.exist?(ctrl)
debug(cmd)
@pid = @executor.run(cmd)
while !File.socket?(ctrl)
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.exists?( ctrl )
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 = TCPSocket.new(ip, port) rescue nil
sock = begin
TCPSocket.new(ip, port)
rescue StandardError
nil
end
success = !!sock
( sock.close rescue nil) if sock
if sock
(begin
sock.close
rescue StandardError
nil
end)
end
success
end
@@ -375,27 +362,25 @@ module FlexNBD
at_exit { kill }
end
def start_wait_thread(pid)
@wait_thread = Thread.start do
_, status = Process.waitpid2(pid)
if @kill
if status.signaled?
fail "flexnbd quit with a bad signal: #{status.inspect}" unless
raise "flexnbd quit with a bad signal: #{status.inspect}" unless
@kill.include? status.termsig
else
fail "flexnbd quit with a bad status: #{status.inspect}" unless
raise "flexnbd quit with a bad status: #{status.inspect}" unless
@kill.include? status.exitstatus
end
else
$stderr.puts "flexnbd #{self.pid} quit"
fail "flexnbd #{self.pid} quit early with status #{status.to_i}"
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
@@ -407,7 +392,7 @@ module FlexNBD
can_die(1)
if @pid
begin
Process.kill("INT", @pid)
Process.kill('INT', @pid)
rescue Errno::ESRCH => e
# already dead. Presumably this means it went away after a
# can_die() call.
@@ -423,7 +408,7 @@ module FlexNBD
IO.popen(cmd) do |fh|
return fh.read
end
raise IOError.new "NBD read failed" unless $?.success?
raise IOError, 'NBD read failed' unless $CHILD_STATUS.success?
out
end
@@ -431,27 +416,24 @@ module FlexNBD
cmd = write_cmd(offset, data)
debug(cmd)
IO.popen(cmd, "w") do |fh|
IO.popen(cmd, 'w') do |fh|
fh.write(data)
end
raise IOError.new "NBD write failed" unless $?.success?
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 )
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)
@@ -459,11 +441,11 @@ module FlexNBD
maybe_timeout(cmd, timeout)
end
def maybe_timeout(cmd, timeout = nil)
stdout, stderr = "",""
stdout = ''
stderr = ''
stat = nil
run = Proc.new do
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.
@@ -473,7 +455,7 @@ module FlexNBD
stderr.replace io_err.read
stat = maybe_thr.value if maybe_thr
end
stat ||= $?
stat ||= $CHILD_STATUS
end
if timeout
@@ -485,16 +467,13 @@ module FlexNBD
[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.new( "Migrate command failed\n" + stderr) unless status.success?
raise IOError, "Migrate command failed\n" + stderr unless status.success?
stdout
end
def break(timeout = nil)
cmd = break_cmd
debug(cmd)
@@ -502,7 +481,6 @@ module FlexNBD
maybe_timeout(cmd, timeout)
end
def acl(*acl)
cmd = acl_cmd(*acl)
debug(cmd)
@@ -510,9 +488,8 @@ module FlexNBD
maybe_timeout(cmd, 2)
end
def status(timeout = nil)
cmd = status_cmd()
cmd = status_cmd
debug(cmd)
o, e = maybe_timeout(cmd, timeout)
@@ -520,50 +497,43 @@ module FlexNBD
[parse_status(o), e]
end
def launched?
!!@pid
end
def paused
Process.kill( "STOP", @pid )
Process.kill('STOP', @pid)
yield
ensure
Process.kill( "CONT", @pid )
Process.kill('CONT', @pid)
end
protected
def control_command(*args)
raise "Server not running" unless @pid
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)
code, message = u.readline.split(': ', 2)
return [code, message]
end
end
def parse_status(status)
hsh = {}
status.split(" ").each do |part|
status.split(' ').each do |part|
next if part.strip.empty?
a,b = part.split("=")
a, b = part.split('=')
b.strip!
b = true if b == "true"
b = false if b == "false"
b = true if b == 'true'
b = false if b == 'false'
hsh[a.strip] = b
end
hsh
end
end
end

View File

@@ -1,10 +1,7 @@
# encoding: utf-8
module FlexNBD
def self.binary(str)
if str.respond_to? :force_encoding
str.force_encoding "ASCII-8BIT"
str.force_encoding 'ASCII-8BIT'
else
str
end
@@ -13,36 +10,33 @@ module FlexNBD
# eeevil is his one and only name...
def self.read_constants
parents = []
current = File.expand_path(".")
while current != "/"
current = File.expand_path('.')
while current != '/'
parents << current
current = File.expand_path( File.join( current, ".." ) )
current = File.expand_path(File.join(current, '..'))
end
source_root = parents.find do |dirname|
File.directory?( File.join( dirname, "src" ) )
File.directory?(File.join(dirname, 'src'))
end
fail "No source root!" unless source_root
raise 'No source root!' unless source_root
headers = Dir[File.join( source_root, "src", "{common,proxy,server}","*.h" ) ]
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($1, $2.to_i) unless constants.include?( $1 )
const_set(Regexp.last_match(1), Regexp.last_match(2).to_i) unless const_defined?(Regexp.last_match(1))
end
end
end
end
end
read_constants()
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

@@ -1,5 +1,3 @@
# encoding: utf-8
require 'socket'
require 'timeout'
@@ -7,15 +5,13 @@ require 'flexnbd/constants'
module FlexNBD
class FakeDest
class Client
def initialize(sock)
@sock = sock
end
def write_hello(opts = {})
@sock.write( "NBDMAGIC" )
@sock.write('NBDMAGIC')
if opts[:magic] == :wrong
write_rand(@sock, 8)
@@ -32,13 +28,11 @@ module FlexNBD
@sock.write("\x00" * 128)
end
def write_rand(sock, len)
len.times do sock.write( rand(256).chr ) end
len.times { sock.write(rand(256).chr) }
end
def read_request()
def read_request
req = @sock.read(28)
magic_s = req[0...4]
@@ -48,11 +42,11 @@ module FlexNBD
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
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
@@ -61,14 +55,12 @@ module FlexNBD
end
def disconnected?
begin
Timeout.timeout(2) do
@sock.read(1) == nil
@sock.read(1).nil?
end
rescue Timeout::Error
return false
end
end
def write_reply(handle, err = 0, opts = {})
if opts[:magic] == :wrong
@@ -77,16 +69,14 @@ module FlexNBD
@sock.write(::FlexNBD::REPLY_MAGIC)
end
@sock.write( [err].pack("N") )
@sock.write([err].pack('N'))
@sock.write(handle)
end
def close
@sock.close
end
def read_data(len)
@sock.read(len)
end
@@ -95,25 +85,23 @@ module FlexNBD
@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, bottom = str.unpack('NN')
(top << 32) + bottom
end
def receive_mirror(opts = {})
write_hello()
write_hello
loop do
req = read_request
case req[:type]
when 1
read_data(req[:len])
write_reply(req[:handle])
when 65536
when 65_536
write_reply(req[:handle], opts[:err] == :entrust ? 1 : 0)
break
else
@@ -129,16 +117,13 @@ module FlexNBD
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)
def accept(err_msg = 'Timed out waiting for a connection', timeout = 5)
client_sock = nil
begin
@@ -146,7 +131,7 @@ module FlexNBD
client_sock = @sock.accept
end
rescue Timeout::Error
raise Timeout::Error.new(err_msg)
raise Timeout::Error, err_msg
end
client_sock
@@ -154,13 +139,8 @@ module FlexNBD
Client.new(client_sock)
end
def close
@sock.close
end
end # module FakeDest
end # module FlexNBD

View File

@@ -1,12 +1,9 @@
# encoding: utf-8
require 'socket'
require "timeout"
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
@@ -16,75 +13,76 @@ module FlexNBD
TCPSocket.new(addr, port)
end
rescue Errno::ECONNREFUSED
$stderr.puts "Connection refused, retrying"
warn 'Connection refused, retrying'
sleep(0.2)
retry
end
end
end
def close
@sock.close
end
def read_hello()
def read_hello
timing_out(::FlexNBD::MS_HELLO_TIME_SECS,
"Timed out waiting for hello." ) do
fail "No hello." unless (hello = @sock.read( 152 )) &&
'Timed out waiting for hello.') do
raise 'No hello.' unless (hello = @sock.read(152)) &&
hello.length == 152
magic_s = hello[0..7]
ignore_s= hello[8..15]
size_s = hello[16..23]
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]
size_h, size_l = size_s.unpack("NN")
size = (size_h << 32) + size_l
return { :magic => magic_s, :size => size }
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 )
fail "Bad handle" unless handle.length == 8
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( [type].pack( 'N' ) )
@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" )
def write_write_request(from, len, handle = 'myhandle')
send_request(1, handle, from, len)
end
def write_entrust_request( handle="myhandle" )
send_request( 65536, handle )
def write_write_request_with_fua(from, len, handle = 'myhandle')
send_request(1, handle, from, len, REQUEST_MAGIC, 1)
end
def write_disconnect_request( handle="myhandle" )
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 )
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 )
timing_out(2, 'Timed out reading') do
send_request(0, 'myhandle', from, len)
read_raw(len)
end
end
@@ -94,19 +92,26 @@ module FlexNBD
end
def send_mirror
read_hello()
write( 0, "12345678" )
read_response()
write_disconnect_request()
close()
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)
@@ -114,30 +119,26 @@ module FlexNBD
handle = @sock.read(8)
{
:magic => magic,
:error => error_s.unpack("N").first,
:handle => handle
magic: magic,
error: error_s.unpack('N').first,
handle: handle
}
end
def disconnected?
result = nil
Timeout.timeout( 2 ) { result = ( @sock.read(1) == nil ) }
Timeout.timeout(2) { result = @sock.read(1).nil? }
result
end
def timing_out(time, msg)
begin
Timeout.timeout(time) do
yield
end
rescue Timeout::Error
$stderr.puts msg
warn msg
exit 1
end
end
private
@@ -154,7 +155,5 @@ module FlexNBD
((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;
}

2
tests/acceptance/nbd_scenarios Normal file → Executable file
View File

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

View File

@@ -1,22 +1,33 @@
# encoding: utf-8
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[:magic]
assert_equal 'NBDMAGIC', result[:passwd]
assert_equal override_size || @env.file1.size, result[:size]
yield client
ensure
client.close rescue nil
begin
client.close
rescue StandardError
nil
end
end
end
@@ -28,11 +39,11 @@ module ProxyTests
with_proxied_client do |client|
(0..3).each do |n|
offset = n * 4096
client.write_read_request(offset, 4096, "myhandle")
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 'myhandle', rsp[:handle]
assert_equal 0, rsp[:error]
orig_data = @env.file1.read(offset, 4096)
@@ -51,20 +62,34 @@ module ProxyTests
with_proxied_client do |client|
(0..3).each do |n|
offset = n * 4096
client.write(offset, "\xFF" * 4096)
client.write(offset, b * 4096)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal "myhandle", rsp[:handle]
assert_equal 'myhandle', rsp[:handle]
assert_equal 0, rsp[:error]
data = @env.file1.read(offset, 4096)
assert_equal( ( "\xFF" * 4096 ), data, "Data not written correctly (offset is #{n})" )
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
@@ -107,31 +132,31 @@ module ProxyTests
# The reply should be proxied back to the client.
sc2.write_reply(req2[:handle])
sc2.write_data( "\xFF" * 4096 )
sc2.write_data(b * 4096)
# Check it to make sure it's correct
rsp = timeout(15) { client.read_response }
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( ("\xFF" * 4096), data, "Wrong data returned" )
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, ( "\xFF" * 4096 ) )
client.write(0, (b * 4096))
# ensure we're given the read request
req1 = sc1.read_request
@@ -140,7 +165,11 @@ module ProxyTests
assert_equal 0, req1[:from]
assert_equal 4096, req1[:len]
data1 = sc1.read_data(4096)
assert_equal( ( "\xFF" * 4096 ), data1, "Data not proxied successfully" )
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
@@ -159,32 +188,53 @@ module ProxyTests
sc2.write_reply(req2[:handle])
# Check it to make sure it's correct
rsp = timeout(15) { client.read_response }
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|
with_proxied_client do |_client|
c2 = nil
assert_raises(Timeout::Error) do
timeout(1) do
Timeout.timeout(1) do
c2 = FlexNBD::FakeSource.new(@env.ip, @env.port2, "Couldn't connect to proxy (2)")
c2.read_hello
end
end
c2.close rescue nil if c2
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

@@ -1,13 +1,10 @@
# encoding: utf-8
require 'test/unit'
require 'environment'
class TestDestErrorHandling < Test::Unit::TestCase
def setup
@env = Environment.new
@env.writefile1( "0" * 4 )
@env.writefile1('0' * 4)
@env.listen1
end
@@ -15,89 +12,77 @@ class TestDestErrorHandling < Test::Unit::TestCase
@env.cleanup
end
def test_hello_blocked_by_disconnect_causes_error_not_fatal
run_fake( "source/close_after_connect" )
run_fake('source/close_after_connect')
assert_no_control
end
=begin
# 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
=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" )
run_fake('source/sigterm_after_hello')
end
def test_disconnect_after_hello_causes_error_not_fatal
run_fake( "source/close_after_hello" )
run_fake('source/close_after_hello')
assert_no_control
end
def test_partial_read_causes_error
run_fake( "source/close_mid_read" )
run_fake('source/close_mid_read')
end
def test_double_connect_during_hello
run_fake( "source/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")
@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" )
run_fake('source/write_out_of_range')
end
def test_disconnect_before_write_data_causes_error
run_fake( "source/close_after_write" )
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" )
run_fake('source/close_after_write_data')
end
def test_straight_migration
@env.nbd1.can_die(0)
run_fake( "source/successful_transfer" )
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, = @env.status1
stat
end
def assert_no_control
assert !status['has_control'], "Thought it had 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

@@ -1,5 +1,3 @@
# encoding: utf-8
require 'test/unit'
require 'environment'
require 'flexnbd/constants'
@@ -19,20 +17,18 @@ class TestHappyPath < Test::Unit::TestCase
@env.cleanup
end
def test_read1
@env.writefile1("f"*64)
@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, 10028, 25488].each do |num|
[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
@@ -40,11 +36,11 @@ class TestHappyPath < Test::Unit::TestCase
# Check that we're not
#
def test_writeread1
@env.writefile1("0"*64)
@env.writefile1('0' * 64)
@env.serve1
[0, 12, 63].each do |num|
data = "X"*@env.blocksize
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))
@@ -55,11 +51,11 @@ class TestHappyPath < Test::Unit::TestCase
# up.
#
def test_writeread2
@env.writefile1("0"*1024)
@env.writefile1('0' * 1024)
@env.serve1
d0 = "\0" * @env.blocksize
d1 = "X"*@env.blocksize
d1 = 'X' * @env.blocksize
(0..63).each do |num|
@env.nbd1.write(num * @env.blocksize * 2, d1)
end
@@ -68,20 +64,18 @@ class TestHappyPath < Test::Unit::TestCase
end
end
def setup_to_mirror
@env.writefile1( "f"*4 )
@env.writefile1('f' * 4)
@env.serve1
@env.writefile2( "0"*4 )
@env.writefile2('0' * 4)
@env.listen2
end
def test_mirror
@env.nbd1.can_die
@env.nbd2.can_die(0)
setup_to_mirror()
setup_to_mirror
stdout, stderr = @env.mirror12
@@ -89,16 +83,15 @@ class TestHappyPath < Test::Unit::TestCase
@env.nbd2.join
assert(File.file?(@env.filename1),
"The source file was incorrectly deleted")
'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()
setup_to_mirror
assert File.file?(@env.filename1)
@@ -106,40 +99,40 @@ class TestHappyPath < Test::Unit::TestCase
assert_no_match(/unrecognized/, stderr)
Timeout.timeout(10) do @env.nbd1.join end
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.truncate1 '4G'
@env.serve1
@env.nbd1.write( 2**31+2**29, "12345678" )
@env.nbd1.write(2**31 + 2**29, '12345678')
sleep(1)
assert_equal "12345678", @env.nbd1.read( 2**31+2**29, 8 )
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.writefile1('f' * 4)
@env.serve1
_,stderr = @env.acl1("127.0.0.1")
_, 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") do |f| f.write( "1" * 256 * one_mb ) end
File.open(@env.filename1, 'wb') { |f| f.write('1' * 256 * one_mb) }
@env.serve1
sleep 5
@@ -148,17 +141,15 @@ class TestHappyPath < Test::Unit::TestCase
@env.nbd1.kill
i = 0
File.open(@env.filename1, "rb") do |f|
File.open(@env.filename1, 'rb') do |f|
while mb = f.read(one_mb)
unless "\0" * one_mb == mb
msg = "Read non-zeros after offset %x:\n"%(i * one_mb)
msg = format("Read non-zeros after offset %x:\n", (i * one_mb))
msg += `hexdump #{@env.filename1} | head -n5`
fail msg
raise msg
end
i += 1
end
end
end
end

View File

@@ -2,7 +2,6 @@ require 'test/unit'
require 'environment'
require 'proxy_tests'
class TestPrefetchProxyMode < Test::Unit::TestCase
include ProxyTests
@@ -10,7 +9,7 @@ class TestPrefetchProxyMode < Test::Unit::TestCase
super
@env = Environment.new
@env.prefetch_proxy!
@env.writefile1( "f" * 16 )
@env.writefile1('f' * 16)
end
def teardown
@@ -18,5 +17,3 @@ class TestPrefetchProxyMode < Test::Unit::TestCase
super
end
end

View File

@@ -2,14 +2,13 @@ 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 )
@env.writefile1('f' * 16)
end
def teardown
@@ -17,4 +16,3 @@ class TestProxyMode < Test::Unit::TestCase
super
end
end

View File

@@ -1,14 +1,15 @@
require 'test/unit'
require 'environment'
require 'flexnbd/fake_source'
require 'ld_preload'
class TestServeMode < Test::Unit::TestCase
include LdPreload
def setup
super
@b = "\xFF".b
@env = Environment.new
@env.writefile1( "0" )
@env.serve1
end
def teardown
@@ -17,35 +18,44 @@ class TestServeMode < Test::Unit::TestCase
end
def connect_to_server
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, "Connecting to server failed")
@env.writefile1('0')
@env.serve1
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
begin
result = client.read_hello
assert_equal "NBDMAGIC", result[:magic]
assert_equal 'NBDMAGIC', result[:passwd]
assert_equal 0x00420281861253, result[:magic]
assert_equal @env.file1.size, result[:size]
# See src/common/nbdtypes.h for the various flags. At the moment we
# support HAS_FLAGS (1), SEND_FLUSH (4), SEND_FUA (8)
assert_equal (1 | 4 | 8), result[:flags]
assert_equal "\x0" * 124, result[:reserved]
yield client
ensure
client.close rescue nil
begin
client.close
rescue StandardError
nil
end
end
end
def test_bad_request_magic_receives_error_response
connect_to_server do |client|
# replace REQUEST_MAGIC with all 0s to make it look bad
client.send_request( 0, "myhandle", 0, 0, "\x00\x00\x00\x00" )
client.send_request(0, 'myhandle', 0, 0, "\x00\x00\x00\x00")
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal "myhandle", rsp[:handle]
assert_equal 'myhandle', rsp[:handle]
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
# The client should be disconnected now
assert client.disconnected?, "Server not disconnected"
assert client.disconnected?, 'Server not disconnected'
end
end
def test_long_write_on_top_of_short_write_is_respected
connect_to_server do |client|
# Start with a file of all-zeroes.
client.write(0, "\x00" * @env.file1.size)
@@ -53,29 +63,29 @@ class TestServeMode < Test::Unit::TestCase
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
client.write( 0, "\xFF" )
client.write(0, @b)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
client.write( 0, "\xFF\xFF" )
client.write(0, @b * 2)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
end
assert_equal "\xFF\xFF", @env.file1.read( 0, 2 )
assert_equal @b * 2, @env.file1.read(0, 2)
end
def test_read_request_out_of_bounds_receives_error_response
connect_to_server do |client|
client.write_read_request(@env.file1.size, 4096)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal "myhandle", rsp[:handle]
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
assert_equal 'myhandle', rsp[:handle]
# NBD protocol suggests ENOSPC (28) is returned
assert_equal 28, rsp[:error], 'Server sent incorrect response'
# Ensure we're not disconnected by sending a request. We don't care about
# whether the reply is good or not, here.
@@ -91,8 +101,9 @@ class TestServeMode < Test::Unit::TestCase
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal "myhandle", rsp[:handle]
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
assert_equal 'myhandle', rsp[:handle]
# NBD protocol suggests ENOSPC (28) is returned
assert_equal 28, rsp[:error], 'Server sent incorrect response'
# Ensure we're not disconnected by sending a request. We don't care about
# whether the reply is good or not, here.
@@ -100,10 +111,113 @@ class TestServeMode < Test::Unit::TestCase
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
end
end
def test_unknown_command_receives_error_response
connect_to_server do |client|
client.send_request(123)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 'myhandle', rsp[:handle]
# NBD protocol suggests EINVAL (22) is returned
assert_equal 22, rsp[:error], 'Server sent incorrect response'
# Ensure we're not disconnected by sending a request. We don't care about
# whether the reply is good or not, here.
client.write(0, "\x00" * @env.file1.size)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
end
end
def test_flush_is_accepted
with_ld_preload('msync_logger') do
connect_to_server do |client|
client.flush
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
end
op = parse_ld_preload_logs('msync_logger')
assert_equal 1, op.count, 'Only one msync expected'
assert_equal @env.blocksize, op.first[2], 'msync length wrong'
assert_equal 6, op.first[3], 'msync called with incorrect flags'
end
end
def test_write_with_fua_is_accepted
with_ld_preload('msync_logger') do
page_size = Integer(`getconf PAGESIZE`)
@env.blocksize = page_size * 10
connect_to_server do |client|
# Write somewhere in the third page
pos = page_size * 3 + 100
client.write_with_fua(pos, "\x00" * 33)
rsp = client.read_response
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
assert_equal 0, rsp[:error]
end
op = parse_ld_preload_logs('msync_logger')
assert_equal 1, op.count, 'Only one msync expected'
# Should be 100 + 33, as we've started writing 100 bytes into a page, for
# 33 bytes
assert_equal 133, op.first[2], 'msync length wrong'
assert_equal 6, op.first[3], 'msync called with incorrect flags'
end
end
def test_odd_size_discs_are_truncated_to_nearest_512
# This should get rounded down to 1024
@env.blocksize = 1024 + 511
@env.writefile1('0')
@env.serve1
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
begin
result = client.read_hello
assert_equal 'NBDMAGIC', result[:passwd]
assert_equal 0x00420281861253, result[:magic]
assert_equal 1024, result[:size]
client.close
end
end
def test_server_sets_tcpkeepalive
with_ld_preload('setsockopt_logger') do
connect_to_server(&:close)
op = read_ld_preload_log('setsockopt_logger')
assert_func_call(op,
['setsockopt', '\d+',
Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1, 0],
'TCP Keepalive not successfully set')
assert_func_call(op,
['setsockopt', '\d+',
Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 30, 0],
'TCP Keepalive idle timeout not set to 30s')
assert_func_call(op,
['setsockopt', '\d+',
Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 10, 0],
'TCP keepalive retry time not set to 10s')
assert_func_call(op,
['setsockopt', '\d+',
Socket::SOL_TCP, Socket::TCP_KEEPCNT, 3, 0],
'TCP keepalive count not set to 3')
end
end
def test_status_returns_correct_client_count
@env.writefile1('0')
@env.serve1
assert_equal('0', @env.status1['num_clients'])
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
assert_equal('1', @env.status1['num_clients'])
client2 = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
assert_equal('2', @env.status1['num_clients'])
client2.close
client.close
assert_equal('0', @env.status1['num_clients'])
end
end

View File

@@ -1,126 +1,105 @@
# encoding: utf-8
require 'test/unit'
require 'environment'
class TestSourceErrorHandling < Test::Unit::TestCase
def setup
@old_env = ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS']
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'] = "4.0"
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'] = '4.0'
@env = Environment.new
@env.writefile1( "f" * 4 )
@env.writefile1('f' * 4)
@env.serve1
end
def teardown
@env.nbd1.can_die(0)
@env.cleanup
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'] = @old_env
end
def expect_term_during_migration
@env.nbd1.can_die(1, 9)
end
def test_failure_to_connect_reported_in_mirror_cmd_response
stdout, stderr = @env.mirror12_unchecked
expect_term_during_migration
assert_match(/failed to connect/, stderr)
end
def test_sigterm_after_hello_quits_with_status_of_1
expect_term_during_migration
run_fake( "dest/sigterm_after_hello" )
run_fake('dest/sigterm_after_hello')
end
def test_destination_hangs_after_connect_reports_error_at_source
run_fake( "dest/hang_after_connect",
:err => /Remote server failed to respond/ )
run_fake('dest/hang_after_connect',
err: /Remote server failed to respond/)
end
def test_destination_rejects_connection_reports_error_at_source
run_fake( "dest/reject_acl",
:err => /Mirror was rejected/ )
run_fake('dest/reject_acl',
err: /Mirror was rejected/)
end
def test_wrong_size_causes_disconnect
run_fake( "dest/hello_wrong_size",
:err => /Remote size does not match local size/ )
run_fake('dest/hello_wrong_size',
err: /Remote size does not match local size/)
end
def test_wrong_magic_causes_disconnect
expect_term_during_migration
run_fake( "dest/hello_wrong_magic",
:err => /Mirror was rejected/ )
run_fake('dest/hello_wrong_magic',
err: /Mirror was rejected/)
end
def test_disconnect_after_hello_causes_retry
expect_term_during_migration
run_fake( "dest/close_after_hello",
:out => /Mirror started/ )
run_fake('dest/close_after_hello',
out: /Mirror started/)
end
def test_write_times_out_causes_retry
expect_term_during_migration
run_fake( "dest/hang_after_write" )
run_fake('dest/hang_after_write')
end
def test_rejected_write_causes_retry
expect_term_during_migration
run_fake( "dest/error_on_write" )
run_fake('dest/error_on_write')
end
def test_disconnect_before_write_reply_causes_retry
expect_term_during_migration
run_fake( "dest/close_after_write" )
run_fake('dest/close_after_write')
end
def test_bad_write_reply_causes_retry
expect_term_during_migration
run_fake( "dest/write_wrong_magic" )
run_fake('dest/write_wrong_magic')
end
def test_pre_entrust_disconnect_causes_retry
expect_term_during_migration
run_fake( "dest/close_after_writes" )
run_fake('dest/close_after_writes')
end
def test_cancel_migration
run_fake( "dest/break_after_hello" )
run_fake('dest/break_after_hello')
end
private
def run_fake(name, opts = {})
@env.run_fake(name, @env.ip, @env.port2, @env.nbd1.ctrl)
stdout, stderr = @env.mirror12_unchecked
assert_success
assert_match(opts[:err], stderr) if opts[:err]
assert_match(opts[:out], stdout) if opts[:out]
return stdout, stderr
[stdout, stderr]
end
def assert_success(msg = nil)
assert @env.fake_reports_success, msg || "Fake failed"
assert @env.fake_reports_success, msg || 'Fake failed'
end
end # class TestSourceErrorHandling

97
tests/acceptance/test_write_during_migration.rb Executable file → Normal file
View File

@@ -9,102 +9,98 @@ require 'tmpdir'
Thread.abort_on_exception = true
class TestWriteDuringMigration < Test::Unit::TestCase
def setup
@flexnbd = File.expand_path("../../build/flexnbd")
raise "No binary!" unless File.executable?( @flexnbd )
@flexnbd = File.expand_path('../../build/flexnbd')
raise 'No binary!' unless File.executable?(@flexnbd)
@size = 20 * 1024 * 1024 # 20MB
@write_data = "foo!" * 2048 # 8K write
@write_data = 'foo!' * 2048 # 8K write
@source_port = 9990
@dest_port = 9991
@source_sock = "src.sock"
@dest_sock = "dst.sock"
@source_file = "src.file"
@dest_file = "dst.file"
@source_sock = 'src.sock'
@dest_sock = 'dst.sock'
@source_file = 'src.file'
@dest_file = 'dst.file'
end
def teardown
[@dst_proc, @src_proc].each do |pid|
if pid
Process.kill( "KILL", pid ) rescue nil
next unless pid
begin
Process.kill('KILL', pid)
rescue StandardError
nil
end
end
end
def debug_arg
ENV['DEBUG'] ? "--verbose" : ""
ENV['DEBUG'] ? '--verbose' : ''
end
def launch_servers
@dst_proc = fork() {
@dst_proc = fork do
cmd = "#{@flexnbd} listen -l 127.0.0.1 -p #{@dest_port} -f #{@dest_file} -s #{@dest_sock} #{debug_arg}"
exec cmd
}
end
@src_proc = fork() {
@src_proc = fork do
cmd = "#{@flexnbd} serve -l 127.0.0.1 -p #{@source_port} -f #{@source_file} -s #{@source_sock} #{debug_arg}"
exec cmd
}
end
begin
awaiting = nil
Timeout.timeout(10) do
awaiting = :source
sleep 0.1 while !File.exists?( @source_sock )
sleep 0.1 until File.exist?(@source_sock)
awaiting = :dest
sleep 0.1 while !File.exists?( @dest_sock )
sleep 0.1 until File.exist?(@dest_sock)
end
rescue Timeout::Error
case awaiting
when :source
fail "Couldn't get a source socket."
raise "Couldn't get a source socket."
when :dest
fail "Couldn't get a destination socket."
raise "Couldn't get a destination socket."
else
fail "Something went wrong I don't understand."
raise "Something went wrong I don't understand."
end
end
end
def make_files()
def make_files
FileUtils.touch(@source_file)
File.truncate(@source_file, @size)
FileUtils.touch(@dest_file)
File.truncate(@dest_file, @size)
File.open(@source_file, "wb"){|f| f.write "a"*@size }
File.open(@source_file, 'wb') { |f| f.write 'a' * @size }
end
def start_mirror
UNIXSocket.open(@source_sock) {|sock|
sock.write(["mirror", "127.0.0.1", @dest_port.to_s, "exit"].join("\x0A") + "\x0A\x0A")
UNIXSocket.open(@source_sock) do |sock|
sock.write(['mirror', '127.0.0.1', @dest_port.to_s, 'exit'].join("\x0A") + "\x0A\x0A")
sock.flush
rsp = sock.readline
}
end
end
def wait_for_quit()
def wait_for_quit
Timeout.timeout(10) do
start_time = Time.now
dst_result = Process::waitpid2(@dst_proc)
src_result = Process::waitpid2(@src_proc)
dst_result = Process.waitpid2(@dst_proc)
src_result = Process.waitpid2(@src_proc)
end
end
def source_writer
client = FlexNBD::FakeSource.new( "127.0.0.1", @source_port, "Timed out connecting" )
client = FlexNBD::FakeSource.new('127.0.0.1', @source_port, 'Timed out connecting')
offsets = Range.new(0, (@size - @write_data.size) / 4096).to_a
loop do
begin
client.write(offsets[rand(offsets.size)] * 4096, @write_data)
rescue => err
rescue StandardError => err
# We expect a broken write at some point, so ignore it
break
end
@@ -115,8 +111,8 @@ class TestWriteDuringMigration < Test::Unit::TestCase
# puts `md5sum #{@source_file} #{@dest_file}`
# Ensure each block matches
File.open(@source_file, "r") do |source|
File.open(@dest_file, "r") do |dest|
File.open(@source_file, 'r') do |source|
File.open(@dest_file, 'r') do |dest|
0.upto(@size / 4096) do |block_num|
s_data = source.read(4096)
d_data = dest.read(4096)
@@ -131,16 +127,16 @@ class TestWriteDuringMigration < Test::Unit::TestCase
end
def test_write_during_migration
Dir.mktmpdir() do |tmpdir|
Dir.mktmpdir do |tmpdir|
Dir.chdir(tmpdir) do
make_files()
make_files
launch_servers()
launch_servers
src_writer = Thread.new { source_writer }
start_mirror()
wait_for_quit()
start_mirror
wait_for_quit
src_writer.join
assert_both_sides_identical
end
@@ -148,24 +144,21 @@ class TestWriteDuringMigration < Test::Unit::TestCase
end
def test_many_clients_during_migration
Dir.mktmpdir() do |tmpdir|
Dir.mktmpdir do |tmpdir|
Dir.chdir(tmpdir) do
make_files()
make_files
launch_servers()
launch_servers
src_writers_1 = (1..5).collect { Thread.new { source_writer } }
start_mirror()
start_mirror
src_writers_2 = (1..5).collect { Thread.new { source_writer } }
wait_for_quit()
( src_writers_1 + src_writers_2 ).each {|t| t.join }
wait_for_quit
(src_writers_1 + src_writers_2).each(&:join)
assert_both_sides_identical
end
end end
end

View File

@@ -13,7 +13,6 @@ START_TEST( test_null_acl )
}
END_TEST
START_TEST(test_parses_single_line)
{
char *lines[] = { "127.0.0.1" };
@@ -37,9 +36,11 @@ START_TEST( test_parses_multiple_lines )
struct ip_and_mask *entry;
entry = &(*acl->entries)[0];
fail_unless(entry->ip.family == e0.family, "entry 0 has wrong family!");
fail_unless(entry->ip.family == e0.family,
"entry 0 has wrong family!");
entry = &(*acl->entries)[1];
fail_unless(entry->ip.family == e1.family, "entry 1 has wrong family!");
fail_unless(entry->ip.family == e1.family,
"entry 1 has wrong family!");
}
END_TEST
@@ -52,7 +53,6 @@ START_TEST( test_destroy_doesnt_crash )
}
END_TEST
START_TEST(test_includes_single_address)
{
char *lines[] = { "127.0.0.1" };
@@ -109,12 +109,13 @@ START_TEST( test_includes_single_address_when_multiple_entries_exist )
parse_ip_to_sockaddr(&e0.generic, "127.0.0.1");
parse_ip_to_sockaddr(&e1.generic, "::1");
fail_unless( acl_includes( acl, &e0 ), "Included address 0 wasn't covered" );
fail_unless( acl_includes( acl, &e1 ), "Included address 1 wasn't covered" );
fail_unless(acl_includes(acl, &e0),
"Included address 0 wasn't covered");
fail_unless(acl_includes(acl, &e1),
"Included address 1 wasn't covered");
}
END_TEST
START_TEST(test_doesnt_include_other_address)
{
char *lines[] = { "127.0.0.1" };
@@ -163,7 +164,6 @@ START_TEST( test_default_deny_rejects )
}
END_TEST
START_TEST(test_default_accept_rejects)
{
struct acl *acl = acl_create(0, NULL, 0);
@@ -175,7 +175,6 @@ START_TEST( test_default_accept_rejects )
}
END_TEST
Suite * acl_suite(void)
{
Suite *s = suite_create("acl");
@@ -189,14 +188,19 @@ Suite* acl_suite(void)
tcase_add_test(tc_includes, test_parses_multiple_lines);
tcase_add_test(tc_includes, test_includes_single_address);
tcase_add_test(tc_includes, test_includes_single_address_when_netmask_specified_ipv4);
tcase_add_test(tc_includes, test_includes_single_address_when_netmask_specified_ipv6);
tcase_add_test(tc_includes,
test_includes_single_address_when_netmask_specified_ipv4);
tcase_add_test(tc_includes,
test_includes_single_address_when_netmask_specified_ipv6);
tcase_add_test(tc_includes, test_includes_single_address_when_multiple_entries_exist);
tcase_add_test(tc_includes,
test_includes_single_address_when_multiple_entries_exist);
tcase_add_test(tc_includes, test_doesnt_include_other_address);
tcase_add_test(tc_includes, test_doesnt_include_other_address_when_netmask_specified);
tcase_add_test(tc_includes, test_doesnt_include_other_address_when_multiple_entries_exist);
tcase_add_test(tc_includes,
test_doesnt_include_other_address_when_netmask_specified);
tcase_add_test(tc_includes,
test_doesnt_include_other_address_when_multiple_entries_exist);
tcase_add_test(tc_includes, test_default_deny_rejects);
tcase_add_test(tc_includes, test_default_accept_rejects);
@@ -226,4 +230,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -59,25 +59,25 @@ END_TEST
START_TEST(test_bit_ranges)
{
bitfield_word_t buffer[BIT_WORDS_FOR_SIZE(4160)];
uint64_t *longs = (unsigned long*) buffer;
uint64_t *longs = (uint64_t *) buffer;
uint64_t i;
memset(buffer, 0, 4160);
for (i = 0; i < 64; i++) {
bit_set_range(buffer, i * 64, i);
fail_unless(
longs[i] == (1UL<<i)-1,
fail_unless(longs[i] == (1ULL << i) - 1,
"longs[%ld] = %lx SHOULD BE %lx",
i, longs[i], (1L<<i)-1
);
i, longs[i], (1ULL << i) - 1);
fail_unless(longs[i+1] == 0, "bit_set_range overshot at i=%d", i);
fail_unless(longs[i + 1] == 0, "bit_set_range overshot at i=%d",
i);
}
for (i = 0; i < 64; i++) {
bit_clear_range(buffer, i * 64, i);
fail_unless(longs[i] == 0, "bit_clear_range didn't work at i=%d", i);
fail_unless(longs[i] == 0, "bit_clear_range didn't work at i=%d",
i);
}
}
END_TEST
@@ -86,7 +86,8 @@ START_TEST(test_bit_runs)
{
bitfield_word_t buffer[BIT_WORDS_FOR_SIZE(256)];
int i, ptr = 0, runs[] = {
56,97,22,12,83,1,45,80,85,51,64,40,63,67,75,64,94,81,79,62
56, 97, 22, 12, 83, 1, 45, 80, 85, 51, 64, 40, 63, 67, 75, 64, 94,
81, 79, 62
};
memset(buffer, 0, 256);
@@ -101,11 +102,8 @@ START_TEST(test_bit_runs)
for (i = 0; i < 20; i += 1) {
int run = bit_run_count(buffer, ptr, 2048 - ptr, NULL);
fail_unless(
run == runs[i],
"run %d should have been %d, was %d",
i, runs[i], run
);
fail_unless(run == runs[i],
"run %d should have been %d, was %d", i, runs[i], run);
ptr += runs[i];
}
}
@@ -145,7 +143,6 @@ START_TEST(test_bitset)
}
END_TEST
START_TEST(test_bitset_set)
{
struct bitset *map;
@@ -176,7 +173,6 @@ START_TEST( test_bitset_set )
}
END_TEST
START_TEST(test_bitset_clear)
{
struct bitset *map;
@@ -443,10 +439,14 @@ START_TEST(test_bitset_stream_queued_bytes)
bitset_clear_range(map, 0, 2);
bitset_disable_stream(map);
ck_assert_int_eq( 64, bitset_stream_queued_bytes( map, BITSET_STREAM_ON ) );
ck_assert_int_eq( 80, bitset_stream_queued_bytes( map, BITSET_STREAM_SET ) );
ck_assert_int_eq( 82, bitset_stream_queued_bytes( map, BITSET_STREAM_UNSET ) );
ck_assert_int_eq( 64, bitset_stream_queued_bytes( map, BITSET_STREAM_OFF ) );
ck_assert_int_eq(64,
bitset_stream_queued_bytes(map, BITSET_STREAM_ON));
ck_assert_int_eq(80,
bitset_stream_queued_bytes(map, BITSET_STREAM_SET));
ck_assert_int_eq(82,
bitset_stream_queued_bytes(map, BITSET_STREAM_UNSET));
ck_assert_int_eq(64,
bitset_stream_queued_bytes(map, BITSET_STREAM_OFF));
bitset_free(map);
}
END_TEST
@@ -471,7 +471,8 @@ Suite* bitset_suite(void)
tcase_add_test(tc_bitset, test_bitset_set_range);
tcase_add_test(tc_bitset, test_bitset_clear_range);
tcase_add_test(tc_bitset, test_bitset_set_range_doesnt_push_to_stream);
tcase_add_test(tc_bitset, test_bitset_clear_range_doesnt_push_to_stream);
tcase_add_test(tc_bitset,
test_bitset_clear_range_doesnt_push_to_stream);
suite_add_tcase(s, tc_bitset);
@@ -497,4 +498,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -10,6 +10,7 @@
#include <unistd.h>
struct server fake_server = { 0 };
#define FAKE_SERVER &fake_server
#define FAKE_SOCKET (42)
@@ -23,7 +24,6 @@ START_TEST( test_assigns_socket )
}
END_TEST
START_TEST(test_assigns_server)
{
struct client *c;
@@ -37,7 +37,6 @@ START_TEST( test_assigns_server )
}
END_TEST
START_TEST(test_opens_stop_signal)
{
struct client *c = client_create(FAKE_SERVER, FAKE_SOCKET);
@@ -50,7 +49,6 @@ START_TEST( test_opens_stop_signal )
}
END_TEST
int fd_is_closed(int);
START_TEST(test_closes_stop_signal)
@@ -66,7 +64,6 @@ START_TEST( test_closes_stop_signal )
}
END_TEST
START_TEST(test_read_request_quits_on_stop_signal)
{
int fds[2];
@@ -84,7 +81,6 @@ START_TEST( test_read_request_quits_on_stop_signal )
}
END_TEST
Suite * client_suite(void)
{
Suite *s = suite_create("client");
@@ -119,4 +115,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -15,7 +15,6 @@ START_TEST( test_assigns_sock_name )
}
END_TEST
Suite * control_suite(void)
{
Suite *s = suite_create("control");
@@ -39,4 +38,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

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