97b7f6c03ce42d2429f064dacc392aafb5be1e8d
[cascardo/ovs.git] / ovn / controller / pinctrl.c
1
2 /* Copyright (c) 2015, 2016 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,
66                   const struct ofputil_switch_config *config)
67 {
68     enum ofp_version version = rconn_get_version(swconn);
69     struct ofpbuf *request = ofputil_encode_set_config(config, version);
70     queue_msg(request);
71 }
72
73 static void
74 process_packet_in(struct controller_ctx *ctx OVS_UNUSED,
75                   const struct ofp_header *msg)
76 {
77     struct ofputil_packet_in pin;
78     uint32_t buffer_id;
79     size_t total_len;
80
81     if (ofputil_decode_packet_in(msg, &pin, &total_len, &buffer_id) != 0) {
82         return;
83     }
84     if (pin.reason != OFPR_ACTION) {
85         return;
86     }
87
88     /* XXX : process the received packet */
89 }
90
91 static void
92 pinctrl_recv(struct controller_ctx *ctx, const struct ofp_header *oh,
93              enum ofptype type)
94 {
95     if (type == OFPTYPE_ECHO_REQUEST) {
96         queue_msg(make_echo_reply(oh));
97     } else if (type == OFPTYPE_GET_CONFIG_REPLY) {
98         struct ofputil_switch_config config;
99
100         ofputil_decode_get_config_reply(oh, &config);
101         config.miss_send_len = UINT16_MAX;
102         set_switch_config(swconn, &config);
103     } else if (type == OFPTYPE_PACKET_IN) {
104         process_packet_in(ctx, oh);
105     } else if (type != OFPTYPE_ECHO_REPLY && type != OFPTYPE_BARRIER_REPLY) {
106         if (VLOG_IS_DBG_ENABLED()) {
107             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
108
109             char *s = ofp_to_string(oh, ntohs(oh->length), 2);
110
111             VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", s);
112             free(s);
113         }
114     }
115 }
116
117 void
118 pinctrl_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int)
119 {
120     if (br_int) {
121         char *target;
122
123         target = xasprintf("unix:%s/%s.mgmt", ovs_rundir(), br_int->name);
124         if (strcmp(target, rconn_get_target(swconn))) {
125             VLOG_INFO("%s: connecting to switch", target);
126             rconn_connect(swconn, target, target);
127         }
128         free(target);
129     } else {
130         rconn_disconnect(swconn);
131     }
132
133     rconn_run(swconn);
134
135     if (!rconn_is_connected(swconn)) {
136         return;
137     }
138
139     if (conn_seq_no != rconn_get_connection_seqno(swconn)) {
140         get_switch_config(swconn);
141         conn_seq_no = rconn_get_connection_seqno(swconn);
142     }
143
144     struct ofpbuf *msg = rconn_recv(swconn);
145
146     if (!msg) {
147         return;
148     }
149
150     const struct ofp_header *oh = msg->data;
151     enum ofptype type;
152
153     ofptype_decode(&type, oh);
154     pinctrl_recv(ctx, oh, type);
155     ofpbuf_delete(msg);
156 }
157
158 void
159 pinctrl_wait(void)
160 {
161     rconn_run_wait(swconn);
162     rconn_recv_wait(swconn);
163 }
164
165 void
166 pinctrl_destroy(void)
167 {
168     rconn_destroy(swconn);
169 }