Till now, rxbuf was only used by the worker. An upcoming commit will have another client. So carve out rxbuf from the worker files.
Signed-off-by: Gurucharan Shetty <gshe...@nicira.com> --- lib/automake.mk | 2 ++ lib/rxbuf.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/rxbuf.h | 38 ++++++++++++++++++++++++++ lib/worker.c | 80 +---------------------------------------------------- 4 files changed, 123 insertions(+), 79 deletions(-) create mode 100644 lib/rxbuf.c create mode 100644 lib/rxbuf.h diff --git a/lib/automake.mk b/lib/automake.mk index bcaa1f8..cc230f1 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -146,6 +146,8 @@ lib_libopenvswitch_a_SOURCES = \ lib/rconn.h \ lib/reconnect.c \ lib/reconnect.h \ + lib/rxbuf.h \ + lib/rxbuf.c \ lib/sat-math.h \ lib/sha1.c \ lib/sha1.h \ diff --git a/lib/rxbuf.c b/lib/rxbuf.c new file mode 100644 index 0000000..567f3a1 --- /dev/null +++ b/lib/rxbuf.c @@ -0,0 +1,82 @@ +/* Copyright (c) 2013 Nicira, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <config.h> +#include "rxbuf.h" +#include "ofpbuf.h" + +void +rxbuf_init(struct rxbuf *rx) +{ + ofpbuf_init(&rx->header, 0); + rx->n_fds = 0; + ofpbuf_init(&rx->payload, 0); +} + +void +rxbuf_clear(struct rxbuf *rx) +{ + ofpbuf_clear(&rx->header); + rx->n_fds = 0; + ofpbuf_clear(&rx->payload); +} + +int +rxbuf_run(struct rxbuf *rx, int sock, size_t header_len) +{ + for (;;) { + if (!rx->header.size) { + int retval; + + ofpbuf_clear(&rx->header); + ofpbuf_prealloc_tailroom(&rx->header, header_len); + + retval = recv_data_and_fds(sock, rx->header.data, header_len, + rx->fds, &rx->n_fds); + if (retval <= 0) { + return retval ? -retval : EOF; + } + rx->header.size += retval; + } else if (rx->header.size < header_len) { + size_t bytes_read; + int error; + + error = read_fully(sock, ofpbuf_tail(&rx->header), + header_len - rx->header.size, &bytes_read); + rx->header.size += bytes_read; + if (error) { + return error; + } + } else { + size_t payload_len = *(size_t *) rx->header.data; + + if (rx->payload.size < payload_len) { + size_t left = payload_len - rx->payload.size; + size_t bytes_read; + int error; + + ofpbuf_prealloc_tailroom(&rx->payload, left); + error = read_fully(sock, ofpbuf_tail(&rx->payload), left, + &bytes_read); + rx->payload.size += bytes_read; + if (error) { + return error; + } + } else { + return 0; + } + } + } +} diff --git a/lib/rxbuf.h b/lib/rxbuf.h new file mode 100644 index 0000000..a6af7e0 --- /dev/null +++ b/lib/rxbuf.h @@ -0,0 +1,38 @@ +/* Copyright (c) 2013 Nicira, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RXBUF_H +#define RXBUF_H 1 + +#include <stddef.h> +#include "ofpbuf.h" +#include "socket-util.h" + +/* Receive buffer for a RPC request or reply. */ +struct rxbuf { + /* Header. */ + struct ofpbuf header; /* Header data. */ + int fds[SOUTIL_MAX_FDS]; /* File descriptors. */ + size_t n_fds; + + /* Payload. */ + struct ofpbuf payload; /* Payload data. */ +}; + +void rxbuf_init(struct rxbuf *); +void rxbuf_clear(struct rxbuf *); +int rxbuf_run(struct rxbuf *, int sock, size_t header_len); + +#endif /* rxbuf.h */ diff --git a/lib/worker.c b/lib/worker.c index 4c947a4..6987767 100644 --- a/lib/worker.c +++ b/lib/worker.c @@ -29,8 +29,8 @@ #include "command-line.h" #include "daemon.h" -#include "ofpbuf.h" #include "poll-loop.h" +#include "rxbuf.h" #include "socket-util.h" #include "util.h" #include "vlog.h" @@ -57,24 +57,9 @@ struct worker_reply { void *reply_aux; /* Auxiliary data for 'reply_cb'. */ }; -/* Receive buffer for a RPC request or reply. */ -struct rxbuf { - /* Header. */ - struct ofpbuf header; /* Header data. */ - int fds[SOUTIL_MAX_FDS]; /* File descriptors. */ - size_t n_fds; - - /* Payload. */ - struct ofpbuf payload; /* Payload data. */ -}; - static int client_sock = -1; static struct rxbuf client_rx; -static void rxbuf_init(struct rxbuf *); -static void rxbuf_clear(struct rxbuf *); -static int rxbuf_run(struct rxbuf *, int sock, size_t header_len); - static struct iovec *prefix_iov(void *data, size_t len, const struct iovec *iovs, size_t n_iovs); @@ -394,69 +379,6 @@ worker_main(int fd) exit(0); } -static void -rxbuf_init(struct rxbuf *rx) -{ - ofpbuf_init(&rx->header, 0); - rx->n_fds = 0; - ofpbuf_init(&rx->payload, 0); -} - -static void -rxbuf_clear(struct rxbuf *rx) -{ - ofpbuf_clear(&rx->header); - rx->n_fds = 0; - ofpbuf_clear(&rx->payload); -} - -static int -rxbuf_run(struct rxbuf *rx, int sock, size_t header_len) -{ - for (;;) { - if (!rx->header.size) { - int retval; - - ofpbuf_clear(&rx->header); - ofpbuf_prealloc_tailroom(&rx->header, header_len); - - retval = recv_data_and_fds(sock, rx->header.data, header_len, - rx->fds, &rx->n_fds); - if (retval <= 0) { - return retval ? -retval : EOF; - } - rx->header.size += retval; - } else if (rx->header.size < header_len) { - size_t bytes_read; - int error; - - error = read_fully(sock, ofpbuf_tail(&rx->header), - header_len - rx->header.size, &bytes_read); - rx->header.size += bytes_read; - if (error) { - return error; - } - } else { - size_t payload_len = *(size_t *) rx->header.data; - - if (rx->payload.size < payload_len) { - size_t left = payload_len - rx->payload.size; - size_t bytes_read; - int error; - - ofpbuf_prealloc_tailroom(&rx->payload, left); - error = read_fully(sock, ofpbuf_tail(&rx->payload), left, - &bytes_read); - rx->payload.size += bytes_read; - if (error) { - return error; - } - } else { - return 0; - } - } - } -} static struct iovec * prefix_iov(void *data, size_t len, const struct iovec *iovs, size_t n_iovs) -- 1.7.9.5 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev