8c53c195cb20b9f3aa1323ac1dec04c4a1282b51
[cascardo/ovs.git] / ovn / controller / pinctrl.c
1
2 /* Copyright (c) 2015 Red Hat, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "dirs.h"
19 #include "pinctrl.h"
20 #include "ofp-msgs.h"
21 #include "ofp-print.h"
22 #include "ofp-util.h"
23 #include "rconn.h"
24 #include "openvswitch/vlog.h"
25 #include "socket-util.h"
26 #include "vswitch-idl.h"
27
28 VLOG_DEFINE_THIS_MODULE(pinctrl);
29
30 /* OpenFlow connection to the switch. */
31 static struct rconn *swconn;
32
33 /* Last seen sequence number for 'swconn'.  When this differs from
34  * rconn_get_connection_seqno(rconn), 'swconn' has reconnected. */
35 static unsigned int conn_seq_no;
36
37 void
38 pinctrl_init(void)
39 {
40     swconn = rconn_create(5, 0, DSCP_DEFAULT, 1 << OFP13_VERSION);
41     conn_seq_no = 0;
42 }
43
44 static ovs_be32
45 queue_msg(struct ofpbuf *msg)
46 {
47     const struct ofp_header *oh = msg->data;
48     ovs_be32 xid = oh->xid;
49
50     rconn_send(swconn, msg, NULL);
51     return xid;
52 }
53
54 static void
55 get_switch_config(struct rconn *swconn)
56 {
57     struct ofpbuf *request;
58
59     request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
60                            rconn_get_version(swconn), 0);
61     queue_msg(request);
62 }
63
64 static void
65 set_switch_config(struct rconn *swconn, const struct ofp_switch_config *config)
66 {
67     struct ofpbuf *request;
68
69     request =
70         ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, rconn_get_version(swconn), 0);
71     ofpbuf_put(request, config, sizeof *config);
72
73     queue_msg(request);
74 }
75
76 static void
77 process_packet_in(struct controller_ctx *ctx OVS_UNUSED,
78                   const struct ofp_header *msg)
79 {
80     struct ofputil_packet_in pin;
81
82     if (ofputil_decode_packet_in(&pin, msg) != 0) {
83         return;
84     }
85     if (pin.reason != OFPR_ACTION) {
86         return;
87     }
88
89     /* XXX : process the received packet */
90 }
91
92 static void
93 pinctrl_recv(struct controller_ctx *ctx, const struct ofp_header *oh,
94              enum ofptype type)
95 {
96     if (type == OFPTYPE_ECHO_REQUEST) {
97         queue_msg(make_echo_reply(oh));
98     } else if (type == OFPTYPE_GET_CONFIG_REPLY) {
99         struct ofpbuf rq_buf;
100         struct ofp_switch_config *config_, config;
101
102         ofpbuf_use_const(&rq_buf, oh, ntohs(oh->length));
103         config_ = ofpbuf_pull(&rq_buf, sizeof *config_);
104         config = *config_;
105         config.miss_send_len = htons(UINT16_MAX);
106         set_switch_config(swconn, &config);
107     } else if (type == OFPTYPE_PACKET_IN) {
108         process_packet_in(ctx, oh);
109     } else if (type != OFPTYPE_ECHO_REPLY && type != OFPTYPE_BARRIER_REPLY) {
110         if (VLOG_IS_DBG_ENABLED()) {
111             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
112
113             char *s = ofp_to_string(oh, ntohs(oh->length), 2);
114
115             VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", s);
116             free(s);
117         }
118     }
119 }
120
121 void
122 pinctrl_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int)
123 {
124     if (br_int) {
125         char *target;
126
127         target = xasprintf("unix:%s/%s.mgmt", ovs_rundir(), br_int->name);
128         if (strcmp(target, rconn_get_target(swconn))) {
129             VLOG_INFO("%s: connecting to switch", target);
130             rconn_connect(swconn, target, target);
131         }
132         free(target);
133     } else {
134         rconn_disconnect(swconn);
135     }
136
137     rconn_run(swconn);
138
139     if (!rconn_is_connected(swconn)) {
140         return;
141     }
142
143     if (conn_seq_no != rconn_get_connection_seqno(swconn)) {
144         get_switch_config(swconn);
145         conn_seq_no = rconn_get_connection_seqno(swconn);
146     }
147
148     struct ofpbuf *msg = rconn_recv(swconn);
149
150     if (!msg) {
151         return;
152     }
153
154     const struct ofp_header *oh = msg->data;
155     enum ofptype type;
156
157     ofptype_decode(&type, oh);
158     pinctrl_recv(ctx, oh, type);
159     ofpbuf_delete(msg);
160 }
161
162 void
163 pinctrl_wait(void)
164 {
165     rconn_run_wait(swconn);
166     rconn_recv_wait(swconn);
167 }
168
169 void
170 pinctrl_destroy(void)
171 {
172     rconn_destroy(swconn);
173 }