mirror of
https://github.com/openvswitch/ovs
synced 2025-10-25 15:07:05 +00:00
ofp-parse: Factor out duplicated code into new functions.
This commit is contained in:
@@ -252,45 +252,9 @@ static void
|
|||||||
send_default_flows(struct lswitch *sw, struct rconn *rconn,
|
send_default_flows(struct lswitch *sw, struct rconn *rconn,
|
||||||
FILE *default_flows)
|
FILE *default_flows)
|
||||||
{
|
{
|
||||||
char line[1024];
|
|
||||||
|
|
||||||
while (fgets(line, sizeof line, default_flows)) {
|
|
||||||
struct ofpbuf *b;
|
struct ofpbuf *b;
|
||||||
struct ofp_flow_mod *ofm;
|
|
||||||
uint16_t priority, idle_timeout, hard_timeout;
|
|
||||||
uint64_t cookie;
|
|
||||||
struct ofp_match match;
|
|
||||||
|
|
||||||
char *comment;
|
while ((b = parse_ofp_add_flow_file(default_flows)) != NULL) {
|
||||||
|
|
||||||
/* Delete comments. */
|
|
||||||
comment = strchr(line, '#');
|
|
||||||
if (comment) {
|
|
||||||
*comment = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Drop empty lines. */
|
|
||||||
if (line[strspn(line, " \t\n")] == '\0') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Parse and send. str_to_flow() will expand and reallocate the data
|
|
||||||
* in 'buffer', so we can't keep pointers to across the str_to_flow()
|
|
||||||
* call. */
|
|
||||||
make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &b);
|
|
||||||
parse_ofp_str(line, &match, b,
|
|
||||||
NULL, NULL, &priority, &idle_timeout, &hard_timeout,
|
|
||||||
&cookie);
|
|
||||||
ofm = b->data;
|
|
||||||
ofm->match = match;
|
|
||||||
ofm->command = htons(OFPFC_ADD);
|
|
||||||
ofm->cookie = htonll(cookie);
|
|
||||||
ofm->idle_timeout = htons(idle_timeout);
|
|
||||||
ofm->hard_timeout = htons(hard_timeout);
|
|
||||||
ofm->buffer_id = htonl(UINT32_MAX);
|
|
||||||
ofm->priority = htons(priority);
|
|
||||||
|
|
||||||
update_openflow_length(b);
|
|
||||||
queue_tx(sw, rconn, b);
|
queue_tx(sw, rconn, b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "dynamic-string.h"
|
||||||
#include "netdev.h"
|
#include "netdev.h"
|
||||||
#include "ofp-util.h"
|
#include "ofp-util.h"
|
||||||
#include "ofpbuf.h"
|
#include "ofpbuf.h"
|
||||||
@@ -29,7 +30,7 @@
|
|||||||
#include "socket-util.h"
|
#include "socket-util.h"
|
||||||
#include "vconn.h"
|
#include "vconn.h"
|
||||||
#include "vlog.h"
|
#include "vlog.h"
|
||||||
|
#include "xtoxll.h"
|
||||||
|
|
||||||
VLOG_DEFINE_THIS_MODULE(ofp_parse)
|
VLOG_DEFINE_THIS_MODULE(ofp_parse)
|
||||||
|
|
||||||
@@ -502,3 +503,65 @@ parse_ofp_str(char *string, struct ofp_match *match, struct ofpbuf *actions,
|
|||||||
free(new);
|
free(new);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Parses 'string' as a OFPT_FLOW_MOD with subtype OFPFC_ADD and returns an
|
||||||
|
* ofpbuf that contains it. */
|
||||||
|
struct ofpbuf *
|
||||||
|
parse_ofp_add_flow_str(char *string)
|
||||||
|
{
|
||||||
|
struct ofpbuf *buffer;
|
||||||
|
struct ofp_flow_mod *ofm;
|
||||||
|
uint16_t priority, idle_timeout, hard_timeout;
|
||||||
|
uint64_t cookie;
|
||||||
|
struct ofp_match match;
|
||||||
|
|
||||||
|
/* parse_ofp_str() will expand and reallocate the data in 'buffer', so we
|
||||||
|
* can't keep pointers to across the parse_ofp_str() call. */
|
||||||
|
make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
|
||||||
|
parse_ofp_str(string, &match, buffer,
|
||||||
|
NULL, NULL, &priority, &idle_timeout, &hard_timeout,
|
||||||
|
&cookie);
|
||||||
|
ofm = buffer->data;
|
||||||
|
ofm->match = match;
|
||||||
|
ofm->command = htons(OFPFC_ADD);
|
||||||
|
ofm->cookie = htonll(cookie);
|
||||||
|
ofm->idle_timeout = htons(idle_timeout);
|
||||||
|
ofm->hard_timeout = htons(hard_timeout);
|
||||||
|
ofm->buffer_id = htonl(UINT32_MAX);
|
||||||
|
ofm->priority = htons(priority);
|
||||||
|
update_openflow_length(buffer);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parses an OFPT_FLOW_MOD with subtype OFPFC_ADD from 'stream' and returns an
|
||||||
|
* ofpbuf that contains it. Returns a null pointer if end-of-file is reached
|
||||||
|
* before reading a flow. */
|
||||||
|
struct ofpbuf *
|
||||||
|
parse_ofp_add_flow_file(FILE *stream)
|
||||||
|
{
|
||||||
|
struct ofpbuf *b = NULL;
|
||||||
|
struct ds s = DS_EMPTY_INITIALIZER;
|
||||||
|
|
||||||
|
while (!ds_get_line(&s, stream)) {
|
||||||
|
char *line = ds_cstr(&s);
|
||||||
|
char *comment;
|
||||||
|
|
||||||
|
/* Delete comments. */
|
||||||
|
comment = strchr(line, '#');
|
||||||
|
if (comment) {
|
||||||
|
*comment = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Drop empty lines. */
|
||||||
|
if (line[strspn(line, " \t\n")] == '\0') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
b = parse_ofp_add_flow_str(line);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ds_destroy(&s);
|
||||||
|
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#define OFP_PARSE_H 1
|
#define OFP_PARSE_H 1
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
struct ofp_match;
|
struct ofp_match;
|
||||||
struct ofpbuf;
|
struct ofpbuf;
|
||||||
@@ -29,5 +30,7 @@ void parse_ofp_str(char *string, struct ofp_match *match,
|
|||||||
uint16_t *out_port, uint16_t *priority,
|
uint16_t *out_port, uint16_t *priority,
|
||||||
uint16_t *idle_timeout, uint16_t *hard_timeout,
|
uint16_t *idle_timeout, uint16_t *hard_timeout,
|
||||||
uint64_t *cookie);
|
uint64_t *cookie);
|
||||||
|
struct ofpbuf *parse_ofp_add_flow_str(char *string);
|
||||||
|
struct ofpbuf *parse_ofp_add_flow_file(FILE *);
|
||||||
|
|
||||||
#endif /* ofp-parse.h */
|
#endif /* ofp-parse.h */
|
||||||
|
|||||||
@@ -524,8 +524,8 @@ static void
|
|||||||
do_add_flows(int argc OVS_UNUSED, char *argv[])
|
do_add_flows(int argc OVS_UNUSED, char *argv[])
|
||||||
{
|
{
|
||||||
struct vconn *vconn;
|
struct vconn *vconn;
|
||||||
|
struct ofpbuf *b;
|
||||||
FILE *file;
|
FILE *file;
|
||||||
char line[1024];
|
|
||||||
|
|
||||||
file = fopen(argv[2], "r");
|
file = fopen(argv[2], "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
@@ -533,43 +533,8 @@ do_add_flows(int argc OVS_UNUSED, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
open_vconn(argv[1], &vconn);
|
open_vconn(argv[1], &vconn);
|
||||||
while (fgets(line, sizeof line, file)) {
|
while ((b = parse_ofp_add_flow_file(file)) != NULL) {
|
||||||
struct ofpbuf *buffer;
|
send_openflow_buffer(vconn, b);
|
||||||
struct ofp_flow_mod *ofm;
|
|
||||||
uint16_t priority, idle_timeout, hard_timeout;
|
|
||||||
uint64_t cookie;
|
|
||||||
struct ofp_match match;
|
|
||||||
|
|
||||||
char *comment;
|
|
||||||
|
|
||||||
/* Delete comments. */
|
|
||||||
comment = strchr(line, '#');
|
|
||||||
if (comment) {
|
|
||||||
*comment = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Drop empty lines. */
|
|
||||||
if (line[strspn(line, " \t\n")] == '\0') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Parse and send. parse_ofp_str() will expand and reallocate
|
|
||||||
* the data in 'buffer', so we can't keep pointers to across the
|
|
||||||
* parse_ofp_str() call. */
|
|
||||||
make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
|
|
||||||
parse_ofp_str(line, &match, buffer,
|
|
||||||
NULL, NULL, &priority, &idle_timeout, &hard_timeout,
|
|
||||||
&cookie);
|
|
||||||
ofm = buffer->data;
|
|
||||||
ofm->match = match;
|
|
||||||
ofm->command = htons(OFPFC_ADD);
|
|
||||||
ofm->cookie = htonll(cookie);
|
|
||||||
ofm->idle_timeout = htons(idle_timeout);
|
|
||||||
ofm->hard_timeout = htons(hard_timeout);
|
|
||||||
ofm->buffer_id = htonl(UINT32_MAX);
|
|
||||||
ofm->priority = htons(priority);
|
|
||||||
|
|
||||||
send_openflow_buffer(vconn, buffer);
|
|
||||||
}
|
}
|
||||||
vconn_close(vconn);
|
vconn_close(vconn);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|||||||
Reference in New Issue
Block a user