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