1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-01-01 12:19:28 +00:00

hyperv/vmbus: Cosmetic bufring cleanup.

MFC after:	1 week
Sponsored by:	Microsoft OSTC
Differential Revision:	https://reviews.freebsd.org/D7267
This commit is contained in:
Sepherosa Ziehau 2016-07-22 03:30:00 +00:00
parent ea17754c5a
commit a6c54e7eea
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=303178

View File

@ -26,7 +26,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
@ -35,8 +34,14 @@
#include "hv_vmbus_priv.h"
/* Amount of space to write to */
#define HV_BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r))? \
((z) - ((w) - (r))):((r) - (w))
#define HV_BYTES_AVAIL_TO_WRITE(r, w, z) \
((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
static uint32_t copy_to_ring_buffer(hv_vmbus_ring_buffer_info *ring_info,
uint32_t start_write_offset, const uint8_t *src,
uint32_t src_len);
static uint32_t copy_from_ring_buffer(hv_vmbus_ring_buffer_info *ring_info,
char *dest, uint32_t dest_len, uint32_t start_read_offset);
static int
hv_rbi_sysctl_stats(SYSCTL_HANDLER_ARGS)
@ -54,39 +59,29 @@ hv_rbi_sysctl_stats(SYSCTL_HANDLER_ARGS)
write_avail = HV_BYTES_AVAIL_TO_WRITE(read_index,
write_index, sz);
read_avail = sz - write_avail;
snprintf(rbi_stats, sizeof(rbi_stats),
"r_idx:%d "
"w_idx:%d "
"int_mask:%d "
"r_avail:%d "
"w_avail:%d",
read_index, write_index, interrupt_mask,
read_avail, write_avail);
return (sysctl_handle_string(oidp, rbi_stats,
sizeof(rbi_stats), req));
snprintf(rbi_stats, sizeof(rbi_stats),
"r_idx:%d w_idx:%d int_mask:%d r_avail:%d w_avail:%d",
read_index, write_index, interrupt_mask, read_avail, write_avail);
return sysctl_handle_string(oidp, rbi_stats, sizeof(rbi_stats), req);
}
void
hv_ring_buffer_stat(
struct sysctl_ctx_list *ctx,
struct sysctl_oid_list *tree_node,
hv_vmbus_ring_buffer_info *rbi,
hv_ring_buffer_stat(struct sysctl_ctx_list *ctx,
struct sysctl_oid_list *tree_node, hv_vmbus_ring_buffer_info *rbi,
const char *desc)
{
SYSCTL_ADD_PROC(ctx, tree_node, OID_AUTO,
"ring_buffer_stats",
CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE, rbi, 0,
hv_rbi_sysctl_stats, "A", desc);
"ring_buffer_stats", CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
rbi, 0, hv_rbi_sysctl_stats, "A", desc);
}
/**
* @brief Get number of bytes available to read and to write to
* for the specified ring buffer
*/
static inline void
get_ring_buffer_avail_bytes(
hv_vmbus_ring_buffer_info* rbi,
uint32_t* read,
static __inline void
get_ring_buffer_avail_bytes(hv_vmbus_ring_buffer_info *rbi, uint32_t *read,
uint32_t *write)
{
uint32_t read_loc, write_loc;
@ -97,27 +92,25 @@ get_ring_buffer_avail_bytes(
read_loc = rbi->ring_buffer->read_index;
write_loc = rbi->ring_buffer->write_index;
*write = HV_BYTES_AVAIL_TO_WRITE(
read_loc, write_loc, rbi->ring_data_size);
*write = HV_BYTES_AVAIL_TO_WRITE(read_loc, write_loc,
rbi->ring_data_size);
*read = rbi->ring_data_size - *write;
}
/**
* @brief Get the next write location for the specified ring buffer
*/
static inline uint32_t
static __inline uint32_t
get_next_write_location(hv_vmbus_ring_buffer_info *ring_info)
{
uint32_t next = ring_info->ring_buffer->write_index;
return (next);
return ring_info->ring_buffer->write_index;
}
/**
* @brief Set the next write location for the specified ring buffer
*/
static inline void
set_next_write_location(
hv_vmbus_ring_buffer_info* ring_info,
static __inline void
set_next_write_location(hv_vmbus_ring_buffer_info *ring_info,
uint32_t next_write_location)
{
ring_info->ring_buffer->write_index = next_write_location;
@ -126,23 +119,22 @@ set_next_write_location(
/**
* @brief Get the next read location for the specified ring buffer
*/
static inline uint32_t
static __inline uint32_t
get_next_read_location(hv_vmbus_ring_buffer_info *ring_info)
{
uint32_t next = ring_info->ring_buffer->read_index;
return (next);
return ring_info->ring_buffer->read_index;
}
/**
* @brief Get the next read location + offset for the specified ring buffer.
* This allows the caller to skip.
*/
static inline uint32_t
get_next_read_location_with_offset(
hv_vmbus_ring_buffer_info* ring_info,
static __inline uint32_t
get_next_read_location_with_offset(hv_vmbus_ring_buffer_info *ring_info,
uint32_t offset)
{
uint32_t next = ring_info->ring_buffer->read_index;
next += offset;
next %= ring_info->ring_data_size;
return (next);
@ -151,9 +143,8 @@ get_next_read_location_with_offset(
/**
* @brief Set the next read location for the specified ring buffer
*/
static inline void
set_next_read_location(
hv_vmbus_ring_buffer_info* ring_info,
static __inline void
set_next_read_location(hv_vmbus_ring_buffer_info *ring_info,
uint32_t next_read_location)
{
ring_info->ring_buffer->read_index = next_read_location;
@ -162,16 +153,16 @@ set_next_read_location(
/**
* @brief Get the start of the ring buffer
*/
static inline void *
static __inline void *
get_ring_buffer(hv_vmbus_ring_buffer_info *ring_info)
{
return (void *) ring_info->ring_buffer->buffer;
return ring_info->ring_buffer->buffer;
}
/**
* @brief Get the size of the ring buffer.
*/
static inline uint32_t
static __inline uint32_t
get_ring_buffer_size(hv_vmbus_ring_buffer_info *ring_info)
{
return ring_info->ring_data_size;
@ -180,23 +171,21 @@ get_ring_buffer_size(hv_vmbus_ring_buffer_info* ring_info)
/**
* Get the read and write indices as uint64_t of the specified ring buffer.
*/
static inline uint64_t
static __inline uint64_t
get_ring_buffer_indices(hv_vmbus_ring_buffer_info *ring_info)
{
return (uint64_t) ring_info->ring_buffer->write_index << 32;
return ((uint64_t)ring_info->ring_buffer->write_index) << 32;
}
void
hv_ring_buffer_read_begin(
hv_vmbus_ring_buffer_info* ring_info)
hv_ring_buffer_read_begin(hv_vmbus_ring_buffer_info *ring_info)
{
ring_info->ring_buffer->interrupt_mask = 1;
mb();
}
uint32_t
hv_ring_buffer_read_end(
hv_vmbus_ring_buffer_info* ring_info)
hv_ring_buffer_read_end(hv_vmbus_ring_buffer_info *ring_info)
{
uint32_t read, write;
@ -209,7 +198,6 @@ hv_ring_buffer_read_end(
* incoming messages.
*/
get_ring_buffer_avail_bytes(ring_info, &read, &write);
return (read);
}
@ -229,8 +217,7 @@ hv_ring_buffer_read_end(
* arrived.
*/
static boolean_t
hv_ring_buffer_needsig_on_write(
uint32_t old_write_location,
hv_ring_buffer_needsig_on_write(uint32_t old_write_location,
hv_vmbus_ring_buffer_info *rbi)
{
mb();
@ -249,35 +236,20 @@ hv_ring_buffer_needsig_on_write(
return (FALSE);
}
static uint32_t copy_to_ring_buffer(
hv_vmbus_ring_buffer_info* ring_info,
uint32_t start_write_offset,
const uint8_t *src,
uint32_t src_len);
static uint32_t copy_from_ring_buffer(
hv_vmbus_ring_buffer_info* ring_info,
char* dest,
uint32_t dest_len,
uint32_t start_read_offset);
/**
* @brief Initialize the ring buffer.
*/
int
hv_vmbus_ring_buffer_init(
hv_vmbus_ring_buffer_info* ring_info,
void* buffer,
hv_vmbus_ring_buffer_init(hv_vmbus_ring_buffer_info *ring_info, void *buffer,
uint32_t buffer_len)
{
memset(ring_info, 0, sizeof(hv_vmbus_ring_buffer_info));
ring_info->ring_buffer = (hv_vmbus_ring_buffer*) buffer;
ring_info->ring_buffer->read_index =
ring_info->ring_buffer = buffer;
ring_info->ring_buffer->read_index = 0;
ring_info->ring_buffer->write_index = 0;
ring_info->ring_data_size = buffer_len - sizeof(hv_vmbus_ring_buffer);
mtx_init(&ring_info->ring_lock, "vmbus ring buffer", NULL, MTX_SPIN);
return (0);
@ -286,7 +258,8 @@ hv_vmbus_ring_buffer_init(
/**
* @brief Cleanup the ring buffer.
*/
void hv_ring_buffer_cleanup(hv_vmbus_ring_buffer_info* ring_info)
void
hv_ring_buffer_cleanup(hv_vmbus_ring_buffer_info *ring_info)
{
mtx_destroy(&ring_info->ring_lock);
}
@ -295,24 +268,19 @@ void hv_ring_buffer_cleanup(hv_vmbus_ring_buffer_info* ring_info)
* @brief Write to the ring buffer.
*/
int
hv_ring_buffer_write(
hv_vmbus_ring_buffer_info* out_ring_info,
const struct iovec iov[],
uint32_t iovlen,
boolean_t *need_sig)
hv_ring_buffer_write(hv_vmbus_ring_buffer_info *out_ring_info,
const struct iovec iov[], uint32_t iovlen, boolean_t *need_sig)
{
int i = 0;
uint32_t byte_avail_to_write;
uint32_t byte_avail_to_read;
uint32_t old_write_location;
uint32_t total_bytes_to_write = 0;
volatile uint32_t next_write_location;
uint64_t prev_indices = 0;
for (i = 0; i < iovlen; i++) {
for (i = 0; i < iovlen; i++)
total_bytes_to_write += iov[i].iov_len;
}
total_bytes_to_write += sizeof(uint64_t);
@ -326,9 +294,7 @@ hv_ring_buffer_write(
* Otherwise, the next time around, we think the ring buffer
* is empty since the read index == write index
*/
if (byte_avail_to_write <= total_bytes_to_write) {
mtx_unlock_spin(&out_ring_info->ring_lock);
return (EAGAIN);
}
@ -350,9 +316,8 @@ hv_ring_buffer_write(
*/
prev_indices = get_ring_buffer_indices(out_ring_info);
next_write_location = copy_to_ring_buffer(
out_ring_info, next_write_location,
(char *) &prev_indices, sizeof(uint64_t));
next_write_location = copy_to_ring_buffer(out_ring_info,
next_write_location, (char *)&prev_indices, sizeof(uint64_t));
/*
* Full memory barrier before upding the write index.
@ -376,9 +341,7 @@ hv_ring_buffer_write(
* @brief Read without advancing the read index.
*/
int
hv_ring_buffer_peek(
hv_vmbus_ring_buffer_info* in_ring_info,
void* buffer,
hv_ring_buffer_peek(hv_vmbus_ring_buffer_info *in_ring_info, void *buffer,
uint32_t buffer_len)
{
uint32_t bytesAvailToWrite;
@ -403,8 +366,8 @@ hv_ring_buffer_peek(
*/
nextReadLocation = get_next_read_location(in_ring_info);
nextReadLocation = copy_from_ring_buffer(
in_ring_info, (char *)buffer, buffer_len, nextReadLocation);
nextReadLocation = copy_from_ring_buffer(in_ring_info,
(char *)buffer, buffer_len, nextReadLocation);
mtx_unlock_spin(&in_ring_info->ring_lock);
@ -415,11 +378,8 @@ hv_ring_buffer_peek(
* @brief Read and advance the read index.
*/
int
hv_ring_buffer_read(
hv_vmbus_ring_buffer_info* in_ring_info,
void* buffer,
uint32_t buffer_len,
uint32_t offset)
hv_ring_buffer_read(hv_vmbus_ring_buffer_info *in_ring_info, void *buffer,
uint32_t buffer_len, uint32_t offset)
{
uint32_t bytes_avail_to_write;
uint32_t bytes_avail_to_read;
@ -431,8 +391,7 @@ hv_ring_buffer_read(
mtx_lock_spin(&in_ring_info->ring_lock);
get_ring_buffer_avail_bytes(
in_ring_info, &bytes_avail_to_read,
get_ring_buffer_avail_bytes(in_ring_info, &bytes_avail_to_read,
&bytes_avail_to_write);
/*
@ -443,21 +402,14 @@ hv_ring_buffer_read(
return (EAGAIN);
}
next_read_location = get_next_read_location_with_offset(
in_ring_info,
next_read_location = get_next_read_location_with_offset(in_ring_info,
offset);
next_read_location = copy_from_ring_buffer(
in_ring_info,
(char *) buffer,
buffer_len,
next_read_location);
next_read_location = copy_from_ring_buffer(in_ring_info, (char *)buffer,
buffer_len, next_read_location);
next_read_location = copy_from_ring_buffer(
in_ring_info,
(char *) &prev_indices,
sizeof(uint64_t),
next_read_location);
next_read_location = copy_from_ring_buffer(in_ring_info,
(char *)&prev_indices, sizeof(uint64_t), next_read_location);
/*
* Make sure all reads are done before we update the read index since
@ -482,11 +434,8 @@ hv_ring_buffer_read(
* Assume there is enough room. Handles wrap-around in dest case only!
*/
static uint32_t
copy_to_ring_buffer(
hv_vmbus_ring_buffer_info* ring_info,
uint32_t start_write_offset,
const uint8_t *src,
uint32_t src_len)
copy_to_ring_buffer(hv_vmbus_ring_buffer_info *ring_info,
uint32_t start_write_offset, const uint8_t *src, uint32_t src_len)
{
char *ring_buffer = get_ring_buffer(ring_info);
uint32_t ring_buffer_size = get_ring_buffer_size(ring_info);
@ -512,12 +461,9 @@ copy_to_ring_buffer(
*
* Assume there is enough room. Handles wrap-around in src case only!
*/
uint32_t
copy_from_ring_buffer(
hv_vmbus_ring_buffer_info* ring_info,
char* dest,
uint32_t dest_len,
uint32_t start_read_offset)
static uint32_t
copy_from_ring_buffer(hv_vmbus_ring_buffer_info *ring_info, char *dest,
uint32_t dest_len, uint32_t start_read_offset)
{
uint32_t fragLen;
char *ring_buffer = get_ring_buffer(ring_info);
@ -537,4 +483,3 @@ copy_from_ring_buffer(
return (start_read_offset);
}