Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/param/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <stdint.h>
#include <vmem/vmem.h>
#include <param/param_error.h>


#include "libparam.h"

Expand Down
7 changes: 7 additions & 0 deletions include/param/param_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#define PARAM_ERR_NONE 0 /**< No error */
#define PARAM_ERR_NOMEM -400 /**< Not enough memory */
#define PARAM_ERR_INVAL -401 /**< Invalid argument */
#define PARAM_ERR_NOTSUP -402 /**< Operation not supported */

37 changes: 35 additions & 2 deletions include/vmem/vmem_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,41 @@
#include <stdint.h>
#include <vmem/vmem_server.h>

int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char * dataout, int version, int use_rdp);
int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t length, int version);
/**
* @brief Download data from remote vmem server at vmem address
*
* Use this function for downloading data from remote node.
*
* @param node The remote node to upload to
* @param timeout Timeout for download
* @param address VMEM address to download from on remote
* @param dataout Pointer to data buffer
* @param length The length in bytes to download into dataout buffer
* @param use_rdp Set flag to use RDP connection
* @param version Protocol version to use be used. Use 2 for 64-bit VMEM address otherwise 32-bit is assumed
* @param verbosity Set verbosity level: 0 for silent, 1 for final summary, 2 = for detailed progress
* @return On success, the total number of bytes successfully uploaded
* @return On error, a negative value is returned to indicate the specific error
*/
ssize_t vmem_download(uint16_t node, uint32_t timeout, uint64_t address, char * dataout, uint32_t length, int use_rdp, int version, int verbosity);

/**
* @brief Upload data to remote vmem server at vmem address
*
* Use this function for uploading data to remote node.
*
* @param node The remote node to upload to
* @param timeout Timeout for upload
* @param address VMEM address to upload to on remote
* @param datain Pointer to data to upload
* @param length The length in bytes of the data passed in
* @param use_rdp Set flag to use RDP connection
* @param version Protocol version to use be used. Use 2 for 64-bit VMEM address otherwise 32-bit is assumed
* @param verbosity Set verbosity level: 0 for silent, 1 for final summary, 2 = for detailed progress
* @return On success, the total number of bytes successfully downloaded
* @return On error, a negative value is returned to indicate the specific error
*/
ssize_t vmem_upload(uint16_t node, uint32_t timeout, uint64_t address, const char * datain, uint32_t size, int use_rdp, int version, int verbosity);
void vmem_client_list(int node, int timeout, int version);
int vmem_client_find(int node, int timeout, void * dataout, int version, char * name, int namelen);
int vmem_client_backup(int node, int vmem_id, int timeout, int backup_or_restore);
Expand Down
116 changes: 78 additions & 38 deletions src/vmem/vmem_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@
* Author: johan
*/

#include <stdint.h>
#include <stdio.h>
#include <csp/arch/csp_time.h>
#include <sys/types.h>
#include <unistd.h>

#include <vmem/vmem_client.h>
#include <param/param_error.h>
#include "csp/csp_error.h"

static int abort = 0;

void vmem_client_abort(void) {
abort = 1;
}

int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char * dataout, int version, int use_rdp)
{
ssize_t vmem_download(uint16_t node, uint32_t timeout, uint64_t address, char * dataout, uint32_t length, int use_rdp, int version, int verbosity) {

if((address > UINT32_MAX) && version != 2){
if(verbosity > 0){
printf(" Error: Address out of range 64-bit addresses require version 2\n");
}
return PARAM_ERR_INVAL;
}

uint32_t time_begin = csp_get_ms();
abort = 0;

Expand All @@ -29,12 +39,16 @@ int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char
opts |= CSP_O_RDP;
}
csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, VMEM_PORT_SERVER, timeout, opts);
if (conn == NULL)
return -1;
if (conn == NULL){
if(verbosity > 0) {
printf(" Connection could not be established\n");
}
return CSP_ERR_TIMEDOUT;
}

csp_packet_t * packet = csp_buffer_get(sizeof(vmem_request_t));
if (packet == NULL)
return -1;
return CSP_ERR_NOMEM;

vmem_request_t * request = (void *) packet->data;
request->version = version;
Expand Down Expand Up @@ -78,13 +92,15 @@ int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char
break;
}

if (dotcount % 32 == 0)
printf(" ");
printf(".");
fflush(stdout);
dotcount++;
if (dotcount % 32 == 0)
printf(" - %.0f K\n", (count / 1024.0));
if(verbosity > 1) {
if (dotcount % 32 == 0)
printf(" ");
printf(".");
fflush(stdout);
dotcount++;
if (dotcount % 32 == 0)
printf(" - %.0f K\n", (count / 1024.0));
}

/* Put data */
memcpy((void *) ((intptr_t) dataout + count), packet->data, packet->length);
Expand All @@ -95,33 +111,51 @@ int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char
csp_buffer_free(packet);
}

printf(" - %.0f K\n", (count / 1024.0));
if(verbosity > 1) {
printf(" - %.0f K\n", (count / 1024.0));
}

csp_close(conn);

uint32_t time_total = csp_get_ms() - time_begin;

printf(" Downloaded %u bytes in %.03f s at %u Bps\n", (unsigned int) count, time_total / 1000.0, (unsigned int) (count / ((float)time_total / 1000.0)) );
if(verbosity > 0) {
printf(" Downloaded %u bytes in %.03f s at %u Bps\n", (unsigned int) count, time_total / 1000.0, (unsigned int) (count / ((float)time_total / 1000.0)) );
}

return count;

}

int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t length, int version) {
ssize_t vmem_upload(uint16_t node, uint32_t timeout, uint64_t address, const char * datain, uint32_t length, int use_rdp, int version, int verbosity) {

if((address > UINT32_MAX) && version != 2){
if(verbosity > 0){
printf(" Error: Address out of range 64-bit addresses require version 2\n");
}
return PARAM_ERR_INVAL;
}

uint32_t time_begin = csp_get_ms();
abort = 0;

/* Establish RDP connection */
csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, VMEM_PORT_SERVER, timeout, CSP_O_RDP | CSP_O_CRC32);
uint32_t opts = CSP_O_CRC32;
if (use_rdp) {
opts |= CSP_O_RDP;
}
csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, VMEM_PORT_SERVER, timeout, opts);
if (conn == NULL) {
printf("Connection could not be established\n");
return -1;
if(verbosity > 0) {
printf(" Connection could not be established\n");
}
return CSP_ERR_TIMEDOUT;
}

csp_packet_t * packet = csp_buffer_get(sizeof(vmem_request_t));
if (packet == NULL)
return -1;
csp_packet_t * packet = csp_buffer_get(0);
if (packet == NULL) {
return CSP_ERR_NOMEM;
}

vmem_request_t * request = (void *) packet->data;
request->version = version;
Expand All @@ -144,20 +178,24 @@ int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t
while((count < length) && csp_conn_is_active(conn)) {

if (abort) {
csp_buffer_free(packet);
break;
}

if (dotcount % 32 == 0)
printf(" ");
printf(".");
fflush(stdout);
dotcount++;
if (dotcount % 32 == 0)
printf(" - %.0f K\n", (count / 1024.0));
if(verbosity > 1) {
if (dotcount % 32 == 0)
printf(" ");
printf(".");
fflush(stdout);
dotcount++;
if (dotcount % 32 == 0)
printf(" - %.0f K\n", (count / 1024.0));
}

/* Prepare packet */
csp_packet_t * packet = csp_buffer_get(VMEM_SERVER_MTU);
csp_packet_t * packet = csp_buffer_get(0);
if(packet == NULL) {
break;
}
packet->length = VMEM_MIN(VMEM_SERVER_MTU, length - count);

/* Copy data */
Expand All @@ -170,18 +208,15 @@ int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t

}

printf(" - %.0f K\n", (count / 1024.0));
if(verbosity > 1) {
printf(" - %.0f K\n", (count / 1024.0));
}

csp_close(conn);

uint32_t time_total = csp_get_ms() - time_begin;

if(count != length){
unsigned int window_size = 0;
csp_rdp_get_opt(&window_size, NULL, NULL, NULL, NULL, NULL);
printf("Upload didn't complete, suggested offset to resume: %"PRIu32"\n", count - ((window_size + 1) * VMEM_SERVER_MTU));
return -1;
} else {
if(verbosity > 0) {
printf(" Uploaded %"PRIu32" bytes in %.03f s at %"PRIu32" Bps\n", count, time_total / 1000.0, (uint32_t)(count / ((float)time_total / 1000.0)) );
}

Expand Down Expand Up @@ -350,6 +385,11 @@ int vmem_client_calc_crc32(int node, int timeout, uint64_t address, uint32_t len

int res = -1;

if((address > UINT32_MAX) && version != 2){
printf(" Error: 64 bit address only supported in version 2\n");
return res;
}

uint32_t time_begin = csp_get_ms();

/* Establish connection */
Expand Down Expand Up @@ -395,4 +435,4 @@ int vmem_client_calc_crc32(int node, int timeout, uint64_t address, uint32_t len
csp_close(conn);

return res;
}
}