Commit Graph

43 Commits

Author SHA1 Message Date
Viresh Kumar
ad48329bc2
Merge branch 'main' into i2c/hashmap 2021-11-11 06:57:15 +05:30
Viresh Kumar
91778ed009 i2c: Remove unused mem field
Removed the unused `mem` field.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-11-08 09:30:22 +05:30
Viresh Kumar
bd84352b36
Merge branch 'main' into i2c/hashmap 2021-11-02 16:30:30 +05:30
Viresh Kumar
ef52e69bff [i2c] Remove the "func" argument from funcs()
The funcs() implementation is buggy as it doesn't take "func" by
reference and it only defaults to SMBUS and hence the I2C protocol is
never chosen as a function.

Since the value of "func" is never required to be set by the caller, it
shouldn't be passed as argument in the first place. Make the function
return Result<u64> instead.

The test test_funcs() was failing before this change and passes after
it.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-28 11:32:17 +05:30
Viresh Kumar
e28f3af61b [i2c] Add a test to check the working of funcs()
The funcs() implementation is buggy as it doesn't take "func" by
reference and it only defaults to SMBUS and hence the I2C protocol is
never chosen as a function.

This test fails currently.

Suggested-by: Andreea Florescu <fandree@amazon.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-27 14:18:14 +05:30
Viresh Kumar
fedfb0b82e [i2c] Use hashmap for device_map
Use hashmap instead of an array of size 1 << 7, as only a few entries of
the array are used here.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-27 14:14:27 +05:30
Viresh Kumar
16a028f35d [i2c] Avoid using expect()
Be consistent with the use of expect() and unwrap() and use only one of
them, if required. Also add a comment on why usage of unwrap() is safe
in main().

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-27 12:36:57 +05:30
Viresh Kumar
d021564ba3 [i2c] Improve tests by matching error number
Improve tests by matching the expected error number.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-26 16:28:38 +05:30
Viresh Kumar
ca5e7e02dd [i2c] Unify Error types
The crate uses lots of ways to report errors, unify them and use a
separate Error enum type for each file. This will also help us improve
the test code later on.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-26 16:28:38 +05:30
Viresh Kumar
e62e27aead [i2c] Use log crate helpers instead of println!()
Use info! and warn! instead of println!.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-26 15:07:17 +05:30
Andreea Florescu
57b96aa3a3 update I2cAdapter to allow setting a mock device
Before the constructor of this function was opening the device
corresponding to the passed path. This did not allow for properly
mocking the device functionality. Now the device is passed as a
parameter instead, which allows us to tweak what the device returns for
ioctl calls, and we can thus test multiple scenarios.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-10-26 10:27:58 +03:00
Vincent Whitchurch
27a19cfe67 [i2c] Copy from correct result buffer
Copy the read data from the correct result buffer, otherwise
this results in incorrect data and/or memory overwrites in the
guest memory.

Signed-off-by: Vincent Whitchurch <vincent@whitchurch.se>
2021-10-26 08:54:58 +03:00
Viresh Kumar
ed3315afc5 [i2c] Fix build errors
Update version of vhost-user-backend and vmm-sys-util to fix the build
errors.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-11 12:07:01 +05:30
Viresh Kumar
b7b5d3e6e0
Merge branch 'main' into dependabot/cargo/vhost-0.2 2021-10-11 11:38:19 +05:30
Viresh Kumar
5cf0b83820 [i2c] Socket count should be greater than 0
Fail early if socket_count is set to 0.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-08 14:29:15 +03:00
Viresh Kumar
726a056254 [i2c] Cleanup tests around socket_count argument
The socket count argument is optional and defaults to 1, but the tests
written for it were more C like and weren't so clean. Use Option to
improve upon that.

While at it, fix socket names as well. Socket name is name of a socket
and shouldn't be used as test-name. Fix the TODOs as well.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-08 14:29:15 +03:00
Viresh Kumar
219419af2e
Merge branch 'main' into dependabot/cargo/vhost-0.2 2021-10-07 15:28:28 +05:30
Viresh Kumar
88d62dd2ab [i2c] Shuffle code around
This moves the code around to keep structures together with their
implementations, and also keep all configuration stuff to main.c itself.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-06 16:57:06 +05:30
Viresh Kumar
c9e88b69ab [i2c] Remove read_func()
It still doesn't look correct to keep a separate routine for this, which
can be called by others.

Merge it with new() itself.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-10-06 16:57:06 +05:30
dependabot[bot]
c27d308f56
build(deps): update vhost requirement from 0.1 to 0.2
Updates the requirements on [vhost](https://github.com/rust-vmm/vhost) to permit the latest version.
- [Release notes](https://github.com/rust-vmm/vhost/releases)
- [Changelog](https://github.com/rust-vmm/vhost/blob/main/CHANGELOG.md)
- [Commits](https://github.com/rust-vmm/vhost/compare/v0.1.0...v0.2.0)

---
updated-dependencies:
- dependency-name: vhost
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-09-27 04:05:35 +00:00
Andreea Florescu
aab3a25816 [i2c] revert to a simplified main program
Remove the dry run related functionality. The main function now just
having boilerplate code that we should test via integration tests. We
can test the parsing by validating the `TryFrom` implementation.
Everything else is not possible to test with unit tests because they
need I/O (such as working with sockets), and thus are more appropriate
for integration tests.

Added more tests for validating the expected behavior of parsing.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-23 18:30:52 +03:00
Andreea Florescu
a6204b55d7 fix dependency of vmm-sys-util to 0.8.0
Without the dependency fixed, the build is failing. Also, considering
that this crate exports a binary, `Cargo.lock` must be committed to the
repository. Added the `Cargo.lock` file.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-22 22:24:45 +03:00
Andreea Florescu
b49993b636 [i2c] update existing unit tests
Updated the existing unit tests to map to the new implemented
abstraction.

In the process of updating the tests, a few other worth mentioning
changes are implemented:
- when initializing a resource needed for the test, it is a best
practice to keep any updates that change the test result in the same
function. This was not followed as the requests vector was initialized
in the test, and was cleared in a function that was asserting results.
This is an unexpected behavior, and is now removed. The requests are
always initialized in the test before using them (and clear is no longer
used).
- `assert_results` is deleted because it was hard to read which results
where asserted. It is also not necessary to mock functions in that way
because now we can mock only the parts we need from the device
implementation.

The tests are incomplete though because we also need to check the error
returned. This should be implemented in a future commit to keep things
separated.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-22 22:24:45 +03:00
Andreea Florescu
db8545c99a [i2c] disallow invalid configurations of I2cMap
This helps with simplifying the code by keeping all checks at init time.
In practice it means that invalid objects can no longer be created,
which simplifies the testing.

All objects are created through either new, or `TryFrom`, and they can
only be updated (outside of test functions) via functions that are
checking the validity of the resulting object.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-22 22:24:42 +03:00
Andreea Florescu
9c019f4657 [i2c] fix formatting
Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 18:12:17 +03:00
Andreea Florescu
162e1adc4f [i2c] separate parsing of strings from device
This separation is needed so we can easily write unit tests for parsing
without needing to create full blown objects. (as a side note, before
this is possible, we also need to get rid of errno, and replace it with
custom Errors, so that we can also write the much needed negative
tests).

This separation is achieved through creating configuration structures
that can be either programatically initialized, or initialized through
parsing the command line parameters.

This commit is still WIP because we also need to make sure that
configuration objects can only be created valid (to reduce some risks
for future extensions where parameters might be passed some other way
rather than yaml). Also, we need to move the check for uniquness of
device addresses in the DeviceConfig instead of the `I2cMap`.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 18:07:49 +03:00
Andreea Florescu
f1c4742e71 [i2c] reworked the abstractions
The I2cAdapterTrait was introduced because of a need to test the i2c
implementation without having access to a physical device on the host
running the tests. This abstraction though still made the tests pretty
hard to write, so it is now replaced by another abstractions: I2cDevice.

This abstraction is written working backwards from what we DO NOT have
access to on the host where we want to run the tests. This is the i2c
device. So, this abstraction is just abstracting the way all the ioctl
calls that need to be handled by an i2c device. This way, we can test
all the other wrappers that are implemented in this crate.

The abstraction is implemented for a physical device to keep backwards
compatibility with existing code.

This abstraction still needs improvements such as marking the functions
as unsafe. For now the tests are commented out because they need to be
re-written with this abstraction. Since we still have refactoring work
to do (i.e. separate the parsing from the device operation), writing
tests is postponed so that it does not involve duplicated work.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 17:34:00 +03:00
Andreea Florescu
683869411e [i2c] remove get_func from I2cAdapter trait
This function does not need to be in the trait. Having it as a public
function also makes the code hard to use because the return of the
`is_smbus` function is only valid after an initial call to `get_func`.

To get rid of this behavior, we can make the `get_func` a private
function in the `I2cAdapter` implementation, and call this function on
`new`.

The function was renamed to `read_func` to make the name more "mutable"
compatible (the function needs a mutable reference, but it was called
`get`; get functions don't typically need mutable references).

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 16:17:10 +03:00
Andreea Florescu
1a9b9e3fd6 [i2c] separate imports
We've typically separated imports in 3 categories:
- Rust standard library
- external crates
- project internal imports (such as imports from user defined modules)

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 15:21:30 +03:00
Andreea Florescu
850cbce112 [i2c] rename bus to adapter_no
This seems to be the way that it is defined & used in other examples
written for i2c.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 15:18:16 +03:00
Andreea Florescu
77aa88dc77 [i2c] move parsing of bus id outside of trait impl
This is a first change in a series meant for moving all the parsing of
strings in a single place, with the goal of separating parsing from the
device operation. This helps with separating concerns, mocking, and
writing unit tests.

Signed-off-by: Andreea Florescu <fandree@amazon.com>
2021-09-15 15:04:08 +03:00
Viresh Kumar
c5e90cfee9 i2c: Add tests to cover basic backend operations
This still doesn't emulate the descriptors and descriptor chains over
the vrings. That will be added later.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-09-14 11:00:56 +03:00
Viresh Kumar
cd23ce7502 i2c: Separate out start_daemon() and remove dryrun
Separate out daemon's handling and remove the ugly dryrun parameter.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-09-14 11:00:56 +03:00
Viresh Kumar
f7c76ad25a i2c: Upgrade to new version of backend
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-09-14 11:00:56 +03:00
Viresh Kumar
b91f074ae5 i2c: Fix dependency to a working commit of backend
To avoid breaking the crate with a newer version of the
vhost-user-backend crate, fix the dependency to a known good version of
the crate.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-09-03 21:17:05 +05:30
Viresh Kumar
9e786e4032 i2c: Fix breakage introduced by vhost-user-backend
vhost-user-backend repository received few updates recently, which
caused this one to break.

Update the code here to fix the breakage.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-31 15:10:23 +05:30
Viresh Kumar
801802254b i2c: Add unit tests
This adds unit tests for the i2c workspace.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-19 09:52:14 +03:00
Viresh Kumar
cc46cab80d i2c: Update README.md and add one for i2c
This updates the main README and adds a specific one for i2c crate.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-19 09:52:14 +03:00
Viresh Kumar
6c7873ef1c i2c: Process virtio requests
This patch implements process_queue() to process incoming requests.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-19 09:52:14 +03:00
Viresh Kumar
0eedf21f4e i2c: Implement low level I2C helpers
This patch implements the low level I2C helpers responsible for
transferring data and parsing the adapters and their clients.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-19 09:52:14 +03:00
Viresh Kumar
0142902927 i2c: Add boilerplate code
This adds the boilerplate code and its dependencies to get the basic
structure for the i2c crate.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-19 09:52:14 +03:00
Viresh Kumar
cae4278e16 Convert to virtual manifest and add i2c workspace
This converts the Cargo.toml to a virtual manifest, removes the
src/lib.rs file and adds basic workspace for i2c backend device.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2021-08-19 09:52:14 +03:00
Andreea Florescu
72710f3a89 Initial commit 2021-05-26 10:18:40 +03:00