e6e7d4ef6ea7eb1458fcc93cb8aeb504f2f92e26
[sliver-openvswitch.git] / vswitchd / mgmt.c
1 /* Copyright (c) 2009 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include <arpa/inet.h>
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25
26 #include "bridge.h"
27 #include "cfg.h"
28 #include "coverage.h"
29 #include "list.h"
30 #include "mgmt.h"
31 #include "openflow/nicira-ext.h"
32 #include "openflow/openflow.h"
33 #include "openflow/openflow-mgmt.h"
34 #include "ofpbuf.h"
35 #include "ovs-vswitchd.h"
36 #include "packets.h"
37 #include "rconn.h"
38 #include "svec.h"
39 #include "vconn.h"
40 #include "vconn-ssl.h"
41 #include "xenserver.h"
42 #include "xtoxll.h"
43
44 #define THIS_MODULE VLM_mgmt
45 #include "vlog.h"
46
47 #define MAX_BACKOFF_DEFAULT 15
48 #define INACTIVITY_PROBE_DEFAULT 15
49
50 static struct svec mgmt_cfg;
51 static uint8_t cfg_cookie[CFG_COOKIE_LEN];
52 static bool need_reconfigure = false;
53 static struct rconn *mgmt_rconn;
54 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
55 static struct svec capabilities;
56 static struct ofpbuf ext_data_buffer;
57 uint64_t mgmt_id;
58
59
60 #define TXQ_LIMIT 128         /* Max number of packets to queue for tx. */
61 struct rconn_packet_counter *txqlen; /* # pkts queued for tx on mgmt_rconn. */
62
63 static uint64_t pick_fallback_mgmt_id(void);
64 static void send_config_update(uint32_t xid, bool use_xid);
65 static void send_resources_update(uint32_t xid, bool use_xid);
66 static int recv_ofmp(uint32_t xid, struct ofmp_header *ofmph, size_t len);
67
68 void
69 mgmt_init(void)
70 {
71     txqlen = rconn_packet_counter_create();
72
73     svec_init(&mgmt_cfg);
74     svec_init(&capabilities);
75     svec_add_nocopy(&capabilities, 
76             xasprintf("com.nicira.mgmt.manager=true\n"));
77
78     mgmt_id = cfg_get_dpid(0, "mgmt.id");
79     if (!mgmt_id) {
80         /* Randomly generate a mgmt id */
81         mgmt_id = pick_fallback_mgmt_id();
82     }
83
84     ofpbuf_init(&ext_data_buffer, 0);
85 }
86
87 #ifdef HAVE_OPENSSL
88 static bool
89 config_string_change(const char *key, char **valuep)
90 {
91     const char *value = cfg_get_string(0, "%s", key);
92     if (value && (!*valuep || strcmp(value, *valuep))) {
93         free(*valuep);
94         *valuep = xstrdup(value);
95         return true;
96     } else {
97         return false;
98     }
99 }
100
101 static void
102 mgmt_configure_ssl(void)
103 {
104     static char *private_key_file;
105     static char *certificate_file;
106     static char *cacert_file;
107     struct stat s;
108
109     /* XXX SSL should be configurable separate from the bridges.
110      * XXX should be possible to de-configure SSL. */
111     if (config_string_change("ssl.private-key", &private_key_file)) {
112         vconn_ssl_set_private_key_file(private_key_file);
113     }
114
115     if (config_string_change("ssl.certificate", &certificate_file)) {
116         vconn_ssl_set_certificate_file(certificate_file);
117     }
118
119     /* We assume that even if the filename hasn't changed, if the CA cert 
120      * file has been removed, that we want to move back into
121      * boot-strapping mode.  This opens a small security hole, because
122      * the old certificate will still be trusted until vSwitch is
123      * restarted.  We may want to address this in vconn's SSL library. */
124     if (config_string_change("ssl.ca-cert", &cacert_file) 
125             || (stat(cacert_file, &s) && errno == ENOENT)) {
126         vconn_ssl_set_ca_cert_file(cacert_file,
127                 cfg_get_bool(0, "ssl.bootstrap-ca-cert"));
128     }
129 }
130 #endif
131
132 void
133 mgmt_reconfigure(void)
134 {
135     struct svec new_cfg;
136     uint8_t new_cookie[CFG_COOKIE_LEN];
137     bool cfg_updated = false;
138     const char *controller_name;
139     int max_backoff;
140     int inactivity_probe;
141     int retval;
142
143     if (!cfg_has_section("mgmt")) {
144         svec_clear(&mgmt_cfg);
145         if (mgmt_rconn) {
146             rconn_destroy(mgmt_rconn);
147             mgmt_rconn = NULL;
148         }
149         return;
150     }
151
152     /* If this is an established connection, send a resources update. */
153     /* xxx This is wasteful if there were no resource changes!!! */
154     if (mgmt_rconn) {
155         send_resources_update(0, false);
156     }
157
158     cfg_get_cookie(new_cookie);
159     if (memcmp(cfg_cookie, new_cookie, sizeof(cfg_cookie))) {
160         memcpy(cfg_cookie, new_cookie, sizeof(cfg_cookie));
161         cfg_updated = true;
162     }
163
164     svec_init(&new_cfg);
165     cfg_get_section(&new_cfg, "mgmt");
166     if (svec_equal(&mgmt_cfg, &new_cfg)) {
167         /* Reconnecting to the controller causes the config file to be
168          * resent automatically.  If we're not reconnecting and the
169          * config file has changed, we need to notify the controller of
170          * changes. */
171         if (cfg_updated && mgmt_rconn) {
172             send_config_update(0, false);
173         }
174         svec_destroy(&new_cfg);
175         return;
176     }
177
178     controller_name = cfg_get_string(0, "mgmt.controller");
179     if (!controller_name) {
180         VLOG_ERR("no controller specified for managment");
181         svec_destroy(&new_cfg);
182         return;
183     }
184
185     max_backoff = cfg_get_int(0, "mgmt.max-backoff");
186     if (max_backoff < 1) {
187         max_backoff = MAX_BACKOFF_DEFAULT;
188     } else if (max_backoff > 3600) {
189         max_backoff = 3600;
190     }
191
192     inactivity_probe = cfg_get_int(0, "mgmt.inactivity-probe");
193     if (inactivity_probe < 5) {
194         inactivity_probe = INACTIVITY_PROBE_DEFAULT;
195     }
196
197     /* xxx If this changes, we need to restart bridges to use new id,
198      * xxx but they need the id before the connect to controller, but we
199      * xxx need their dpids. */
200     /* Check if a different mgmt id has been assigned. */
201     if (cfg_has("mgmt.id")) {
202         uint64_t cfg_mgmt_id = cfg_get_dpid(0, "mgmt.id");
203         if (cfg_mgmt_id != mgmt_id) {
204             mgmt_id = cfg_mgmt_id;
205         }
206     }
207
208     svec_swap(&new_cfg, &mgmt_cfg);
209     svec_destroy(&new_cfg);
210
211 #ifdef HAVE_OPENSSL
212     /* Configure SSL. */
213     mgmt_configure_ssl();
214 #endif
215
216     if (mgmt_rconn) {
217         rconn_destroy(mgmt_rconn);
218         mgmt_rconn = NULL;
219     }
220     mgmt_rconn = rconn_create(inactivity_probe, max_backoff);
221     retval = rconn_connect(mgmt_rconn, controller_name);
222     if (retval == EAFNOSUPPORT) {
223         VLOG_ERR("no support for %s vconn", controller_name);
224     }
225 }
226
227 static void *
228 make_ofmp_xid(size_t ofmp_len, uint16_t type, uint32_t xid,
229         struct ofpbuf **bufferp)
230 {
231     struct ofmp_header *oh;
232
233     oh = make_openflow_xid(ofmp_len, OFPT_VENDOR, xid, bufferp);
234     oh->header.vendor = htonl(NX_VENDOR_ID);
235     oh->header.subtype = htonl(NXT_MGMT);
236     oh->type = htons(type);
237
238     return oh;
239 }
240
241 static void *
242 make_ofmp(size_t ofmp_len, uint16_t type, struct ofpbuf **bufferp)
243 {
244     struct ofmp_header *oh;
245
246     oh = make_openflow(ofmp_len, OFPT_VENDOR, bufferp);
247     oh->header.vendor = htonl(NX_VENDOR_ID);
248     oh->header.subtype = htonl(NXT_MGMT);
249     oh->type = htons(type);
250
251     return oh;
252 }
253
254 static int
255 send_openflow_buffer(struct ofpbuf *buffer)
256 {               
257     int retval;
258
259     if (!mgmt_rconn) {
260         VLOG_ERR("attempt to send openflow packet with no rconn\n");
261         return EINVAL;
262     }
263
264     /* OpenFlow messages use a 16-bit length field, so messages over 64K
265      * must be broken into multiple pieces. 
266      */
267     if (buffer->size <= 65535) {
268         update_openflow_length(buffer);
269         retval = rconn_send_with_limit(mgmt_rconn, buffer, txqlen, TXQ_LIMIT);
270         if (retval) {
271             VLOG_WARN_RL(&rl, "send to %s failed: %s",
272                          rconn_get_name(mgmt_rconn), strerror(retval));
273         }   
274         return retval;
275     } else {
276         struct ofmp_header *header = (struct ofmp_header *)buffer->data;
277         uint32_t xid = header->header.header.xid;
278         size_t remain = buffer->size;
279         uint8_t *ptr = buffer->data;
280         
281         /* Mark the OpenFlow header with a zero length to indicate some
282          * funkiness. 
283          */
284         header->header.header.length = 0;
285
286         while (remain > 0) {
287             struct ofpbuf *new_buffer;
288             struct ofmp_extended_data *oed;
289             size_t new_len = MIN(65535 - sizeof *oed, remain);
290
291             oed = make_ofmp_xid(sizeof *oed, OFMPT_EXTENDED_DATA, xid, 
292                     &new_buffer);
293             oed->type = header->type;
294
295             if (remain > 65535) {
296                 oed->flags |= OFMPEDF_MORE_DATA;
297             }
298
299             printf("xxx SENDING LEN: %d\n", new_len);
300
301             /* Copy the entire original message, including the OpenFlow
302              * header, since management protocol structure definitions
303              * include these headers.
304              */
305             ofpbuf_put(new_buffer, ptr, new_len);
306
307             update_openflow_length(new_buffer);
308             retval = rconn_send_with_limit(mgmt_rconn, new_buffer, txqlen, 
309                     TXQ_LIMIT);
310             if (retval) {
311                 VLOG_WARN_RL(&rl, "send to %s failed: %s",
312                              rconn_get_name(mgmt_rconn), strerror(retval));
313                 ofpbuf_delete(buffer);
314                 return retval;
315             }   
316
317             remain -= new_len;
318             ptr += new_len;
319         }
320
321         ofpbuf_delete(buffer);
322         return 0;
323     }
324 }   
325     
326 static void
327 send_features_reply(uint32_t xid)
328 {
329     struct ofpbuf *buffer;
330     struct ofp_switch_features *ofr;
331
332     ofr = make_openflow_xid(sizeof *ofr, OFPT_FEATURES_REPLY, xid, &buffer);
333     ofr->datapath_id  = 0;
334     ofr->n_tables     = 0;
335     ofr->n_buffers    = 0;
336     ofr->capabilities = 0;
337     ofr->actions      = 0;
338     send_openflow_buffer(buffer);
339 }
340
341 static void 
342 send_capability_reply(uint32_t xid)
343 {
344     int i;
345     struct ofpbuf *buffer;
346     struct ofmp_capability_reply *ofmpcr;
347
348     ofmpcr = make_ofmp_xid(sizeof *ofmpcr, OFMPT_CAPABILITY_REPLY, 
349             xid, &buffer);
350     ofmpcr->format = htonl(OFMPCOF_SIMPLE);
351     ofmpcr->mgmt_id = htonll(mgmt_id);
352     for (i=0; i<capabilities.n; i++) {
353         ofpbuf_put(buffer, capabilities.names[i], 
354                 strlen(capabilities.names[i]));
355     }
356     send_openflow_buffer(buffer);
357 }
358
359 static void 
360 send_resources_update(uint32_t xid, bool use_xid)
361 {
362     struct ofpbuf *buffer;
363     struct ofmp_resources_update *ofmpru;
364     struct ofmp_tlv *tlv;
365     struct svec br_list;
366     struct svec port_list;
367     const char *host_uuid;
368     int i;
369
370     if (use_xid) {
371         ofmpru = make_ofmp_xid(sizeof *ofmpru, OFMPT_RESOURCES_UPDATE, 
372                 xid, &buffer);
373     } else {
374         ofmpru = make_ofmp(sizeof *ofmpru, OFMPT_RESOURCES_UPDATE, &buffer);
375     }
376
377     /* On XenServer systems, each host has its own UUID, which we provide
378      * to the controller. 
379      */ 
380     host_uuid = xenserver_get_host_uuid();
381     if (host_uuid) {
382         struct ofmptsr_mgmt_uuid *mgmt_uuid_tlv;
383
384         mgmt_uuid_tlv = ofpbuf_put_zeros(buffer, sizeof(*mgmt_uuid_tlv));
385         mgmt_uuid_tlv->type = htons(OFMPTSR_MGMT_UUID);
386         mgmt_uuid_tlv->len = htons(sizeof(*mgmt_uuid_tlv));
387         mgmt_uuid_tlv->mgmt_id = htonll(mgmt_id);
388         memcpy(mgmt_uuid_tlv->uuid, host_uuid, OFMP_UUID_LEN);
389     }
390
391     svec_init(&br_list);
392     cfg_get_subsections(&br_list, "bridge");
393     for (i=0; i < br_list.n; i++) {
394         struct ofmptsr_dp *dp_tlv;
395         uint64_t dp_id;
396         int n_uuid;
397
398         dp_id = bridge_get_datapathid(br_list.names[i]);
399         if (!dp_id) {
400             VLOG_WARN_RL(&rl, "bridge %s doesn't seem to exist", 
401                     br_list.names[i]);
402             continue;
403         }
404         dp_tlv = ofpbuf_put_zeros(buffer, sizeof(*dp_tlv));
405         dp_tlv->type = htons(OFMPTSR_DP);
406         dp_tlv->len = htons(sizeof(*dp_tlv));
407
408         dp_tlv->dp_id = htonll(dp_id);
409         memcpy(dp_tlv->name, br_list.names[i], strlen(br_list.names[i])+1);
410
411         /* On XenServer systems, each network has one or more UUIDs
412          * associated with it, which we provide to the controller. 
413          */
414         n_uuid = cfg_count("bridge.%s.xs-network-uuids", br_list.names[i]);
415         if (n_uuid) {
416             struct ofmptsr_dp_uuid *dp_uuid_tlv;
417             size_t tlv_len = sizeof(*dp_uuid_tlv) + n_uuid * OFMP_UUID_LEN;
418             int j;
419
420             dp_uuid_tlv = ofpbuf_put_zeros(buffer, sizeof(*dp_uuid_tlv));
421             dp_uuid_tlv->type = htons(OFMPTSR_DP_UUID);
422             dp_uuid_tlv->len = htons(tlv_len);
423             dp_uuid_tlv->dp_id = htonll(dp_id);
424
425             for (j=0; j<n_uuid; j++) {
426                 const char *dp_uuid = cfg_get_string(j, 
427                         "bridge.%s.xs-network-uuids", br_list.names[i]);
428
429                 /* The UUID list could change underneath us, so just
430                  * fill with zeros in that case.  Another update will be
431                  * initiated shortly, which should contain corrected data.
432                  */
433                 if (dp_uuid) {
434                     ofpbuf_put(buffer, dp_uuid, OFMP_UUID_LEN);
435                 } else {
436                     ofpbuf_put_zeros(buffer, OFMP_UUID_LEN);
437                 }
438             }
439         }
440     }
441     svec_destroy(&br_list);
442
443     /* On XenServer systems, extended information about virtual interfaces 
444      * (VIFs) is available, which is needed by the controller. 
445      */ 
446     svec_init(&port_list);
447     bridge_get_ifaces(&port_list);
448     for (i=0; i < port_list.n; i++) {
449         const char *vif_uuid, *vm_uuid, *net_uuid;
450         uint64_t vif_mac;
451         struct ofmptsr_vif *vif_tlv;
452
453         vif_uuid = cfg_get_string(0, "port.%s.vif-uuid", port_list.names[i]);
454         if (!vif_uuid) {
455             continue;
456         }
457
458         vif_tlv = ofpbuf_put_zeros(buffer, sizeof(*vif_tlv));
459         vif_tlv->type = htons(OFMPTSR_VIF);
460         vif_tlv->len = htons(sizeof(*vif_tlv));
461
462         memcpy(vif_tlv->name, port_list.names[i], strlen(port_list.names[i])+1);
463         memcpy(vif_tlv->vif_uuid, vif_uuid, sizeof(vif_tlv->vif_uuid));
464
465         vm_uuid = cfg_get_string(0, "port.%s.vm-uuid", port_list.names[i]);
466         if (vm_uuid) {
467             memcpy(vif_tlv->vm_uuid, vm_uuid, sizeof(vif_tlv->vm_uuid));
468         } else {
469             /* In case the vif disappeared underneath us. */
470             memset(vif_tlv->vm_uuid, '\0', sizeof(vif_tlv->vm_uuid));
471         }
472
473         net_uuid = cfg_get_string(0, "port.%s.net-uuid", port_list.names[i]);
474         if (net_uuid) {
475             memcpy(vif_tlv->net_uuid, net_uuid, sizeof(vif_tlv->net_uuid));
476         } else {
477             /* In case the vif disappeared underneath us. */
478             memset(vif_tlv->net_uuid, '\0', sizeof(vif_tlv->net_uuid));
479         }
480
481         vif_mac = cfg_get_mac(0, "port.%s.vif-mac", port_list.names[i]);
482         vif_tlv->vif_mac = htonll(vif_mac);
483     }
484     svec_destroy(&port_list);
485
486     /* Put end marker. */
487     tlv = ofpbuf_put_zeros(buffer, sizeof(*tlv));
488     tlv->type = htons(OFMPTSR_END);
489     tlv->len = htons(sizeof(*tlv));
490     send_openflow_buffer(buffer);
491 }
492
493 static void 
494 send_config_update(uint32_t xid, bool use_xid)
495 {
496     struct ofpbuf *buffer;
497     struct ofmp_config_update *ofmpcu;
498
499     if (use_xid) {
500         ofmpcu = make_ofmp_xid(sizeof *ofmpcu, OFMPT_CONFIG_UPDATE, 
501                 xid, &buffer);
502     } else {
503         ofmpcu = make_ofmp(sizeof *ofmpcu, OFMPT_CONFIG_UPDATE, &buffer);
504     }
505
506     ofmpcu->format = htonl(OFMPCOF_SIMPLE);
507     memcpy(ofmpcu->cookie, cfg_cookie, sizeof(ofmpcu->cookie));
508     cfg_buf_put(buffer);
509     send_openflow_buffer(buffer);
510 }
511
512 static void 
513 send_config_update_ack(uint32_t xid, bool success)
514 {
515     struct ofpbuf *buffer;
516     struct ofmp_config_update_ack *ofmpcua;
517
518     ofmpcua = make_ofmp_xid(sizeof *ofmpcua, OFMPT_CONFIG_UPDATE_ACK, 
519             xid, &buffer);
520
521     ofmpcua->format = htonl(OFMPCOF_SIMPLE);
522     if (success) {
523         ofmpcua->flags = htonl(OFMPCUAF_SUCCESS);
524     }
525     cfg_get_cookie(ofmpcua->cookie);
526     send_openflow_buffer(buffer);
527 }
528
529 static void
530 send_error_msg(uint32_t xid, uint16_t type, uint16_t code, 
531             const void *data, size_t len)
532 {
533     struct ofpbuf *buffer;
534     struct ofp_error_msg *oem;
535
536     oem = make_openflow_xid(sizeof(*oem)+len, OFPT_ERROR, xid, &buffer);
537     oem->type = htons(type);
538     oem->code = htons(code);
539     memcpy(oem->data, data, len);
540     send_openflow_buffer(buffer);
541 }
542
543 static int
544 recv_echo_request(uint32_t xid UNUSED, const void *msg)
545 {
546     const struct ofp_header *rq = msg;
547     send_openflow_buffer(make_echo_reply(rq));
548     return 0;
549 }
550
551 static int
552 recv_features_request(uint32_t xid, const void *msg UNUSED)
553 {
554     send_features_reply(xid);
555     return 0;
556 }
557
558 static int
559 recv_set_config(uint32_t xid UNUSED, const void *msg UNUSED)
560 {
561     /* Nothing to configure! */
562     return 0;
563 }
564
565 static int
566 recv_ofmp_capability_request(uint32_t xid, const struct ofmp_header *ofmph,
567         size_t len)
568 {
569     struct ofmp_capability_request *ofmpcr;
570
571     if (len != sizeof(*ofmpcr)) {
572         /* xxx Send error */
573         return -EINVAL;
574     }
575
576     ofmpcr = (struct ofmp_capability_request *)ofmph;
577     if (ofmpcr->format != htonl(OFMPCAF_SIMPLE)) {
578         /* xxx Send error */
579         return -EINVAL;
580     }
581
582     send_capability_reply(xid);
583
584     return 0;
585 }
586
587 static int
588 recv_ofmp_resources_request(uint32_t xid, const void *msg UNUSED, 
589         size_t len UNUSED)
590 {
591     send_resources_update(xid, true);
592     return 0;
593 }
594
595 static int
596 recv_ofmp_config_request(uint32_t xid, const struct ofmp_header *ofmph, 
597         size_t len)
598 {
599     struct ofmp_config_request *ofmpcr;
600
601     if (len != sizeof(*ofmpcr)) {
602         /* xxx Send error */
603         return -EINVAL;
604     }
605
606     ofmpcr = (struct ofmp_config_request *)ofmph;
607     if (ofmpcr->format != htonl(OFMPCOF_SIMPLE)) {
608         /* xxx Send error */
609         return -EINVAL;
610     }
611
612     send_config_update(xid, true);
613
614     return 0;
615 }
616
617 static int
618 recv_ofmp_config_update(uint32_t xid, const struct ofmp_header *ofmph,
619         size_t len)
620 {
621     struct ofmp_config_update *ofmpcu;
622     int data_len;
623
624     data_len = len - sizeof(*ofmpcu);
625     if (data_len <= sizeof(*ofmpcu)) {
626         /* xxx Send error. */
627         return -EINVAL;
628     }
629
630     ofmpcu = (struct ofmp_config_update *)ofmph;
631     if (ofmpcu->format != htonl(OFMPCOF_SIMPLE)) {
632         /* xxx Send error */
633         return -EINVAL;
634     }
635
636     /* Check if the supplied cookie matches our current understanding of
637      * it.  If they don't match, tell the controller and let it sort
638      * things out. */
639     if (cfg_lock(ofmpcu->cookie, 0)) {  
640         /* xxx cfg_lock can fail for other reasons, such as being
641          * xxx locked... */
642         VLOG_WARN_RL(&rl, "config update failed due to bad cookie\n");
643
644         /* Check if our local view matches the controller, in which
645          * case, it is likely that there were local modifications
646          * without our being told to reread the config file. */
647         if (!memcmp(cfg_cookie, ofmpcu->cookie, sizeof cfg_cookie)) {
648             VLOG_WARN_RL(&rl, "config appears to have been locally modified "
649                               "without having told ovs-vswitchd to reload");
650         }
651         send_config_update_ack(xid, false);
652         return 0;
653     }
654
655     /* xxx We should probably do more sanity checking than this. */
656
657     cfg_write_data(ofmpcu->data, data_len);
658     cfg_unlock();
659
660     /* Send the ACK before running reconfigure, since our management
661      * connection settings may have changed. */
662     send_config_update_ack(xid, true);
663
664     need_reconfigure = true;
665
666     return 0;
667 }
668
669 static int
670 recv_ofmp_extended_data(uint32_t xid, const struct ofmp_header *ofmph,
671         size_t len)
672 {
673     size_t data_len;
674     struct ofmp_extended_data *ofmped;
675     uint8_t *ptr;
676
677     data_len = len - sizeof(*ofmped);
678     if (data_len <= sizeof(*ofmped)) {
679         /* xxx Send error. */
680         return -EINVAL;
681     }
682
683     ofmped = (struct ofmp_extended_data *)ofmph;
684
685     ptr = ofpbuf_put(&ext_data_buffer, ofmped->data, data_len);
686
687     if (!ofmped->flags & OFMPEDF_MORE_DATA) {
688         recv_ofmp(xid, ext_data_buffer.data, ext_data_buffer.size);
689         ofpbuf_clear(&ext_data_buffer);
690     }
691
692     return 0;
693 }
694
695 /* Handles receiving a management message.  Generally, this function
696  * will be called 'len' set to zero, and the length will be derived by
697  * the OpenFlow header.  With the extended data message, management
698  * messages are not constrained by OpenFlow's 64K message length limit.  
699  * The extended data handler calls this function with the 'len' set to
700  * the total message length and the OpenFlow header's length field is 
701  * ignored.
702  */
703 static
704 int recv_ofmp(uint32_t xid, struct ofmp_header *ofmph, size_t len)
705 {
706     if (!len) {
707         len = ntohs(ofmph->header.header.length);
708     }
709
710     /* xxx Should sanity-check for min/max length */
711     switch (ntohs(ofmph->type)) 
712     {
713         case OFMPT_CAPABILITY_REQUEST:
714             return recv_ofmp_capability_request(xid, ofmph, len);
715         case OFMPT_RESOURCES_REQUEST:
716             return recv_ofmp_resources_request(xid, ofmph, len);
717         case OFMPT_CONFIG_REQUEST:
718             return recv_ofmp_config_request(xid, ofmph, len);
719         case OFMPT_CONFIG_UPDATE:
720             return recv_ofmp_config_update(xid, ofmph, len);
721         case OFMPT_EXTENDED_DATA:
722             return recv_ofmp_extended_data(xid, ofmph, len);
723         default:
724             VLOG_WARN_RL(&rl, "unknown mgmt message: %d", 
725                     ntohs(ofmph->type));
726             return -EINVAL;
727     }
728 }
729
730 static int 
731 recv_nx_msg(uint32_t xid, const void *oh)
732 {
733     const struct nicira_header *nh = oh;
734
735     switch (ntohl(nh->subtype)) {
736
737     case NXT_MGMT:
738         return recv_ofmp(xid, (struct ofmp_header *)oh, 0);
739
740     default:
741         send_error_msg(xid, OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE, 
742                 oh, ntohs(nh->header.length));
743         return -EINVAL;
744     }
745 }
746
747 static int
748 recv_vendor(uint32_t xid, const void *oh)
749 {
750     const struct ofp_vendor_header *ovh = oh;
751
752     switch (ntohl(ovh->vendor))
753     {
754     case NX_VENDOR_ID:
755         return recv_nx_msg(xid, oh);
756
757     default:
758         VLOG_WARN_RL(&rl, "unknown vendor: 0x%x", ntohl(ovh->vendor));
759         send_error_msg(xid, OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR, 
760                 oh, ntohs(ovh->header.length));
761         return -EINVAL; 
762     }
763 }
764
765 static int
766 handle_msg(uint32_t xid, const void *msg, size_t length)
767 {
768     int (*handler)(uint32_t, const void *);
769     struct ofp_header *oh;
770     size_t min_size;
771
772     COVERAGE_INC(mgmt_received);
773
774     /* Check encapsulated length. */
775     oh = (struct ofp_header *) msg;
776     if (ntohs(oh->length) > length) {
777         return -EINVAL;
778     }
779     assert(oh->version == OFP_VERSION);
780
781     /* Figure out how to handle it. */
782     switch (oh->type) {
783     case OFPT_ECHO_REQUEST:
784         min_size = sizeof(struct ofp_header);
785         handler = recv_echo_request;
786         break;
787     case OFPT_ECHO_REPLY:
788         return 0;
789     case OFPT_FEATURES_REQUEST:
790         min_size = sizeof(struct ofp_header);
791         handler = recv_features_request;
792         break;
793     case OFPT_SET_CONFIG:
794         min_size = sizeof(struct ofp_switch_config);
795         handler = recv_set_config;
796         break;
797     case OFPT_VENDOR:
798         min_size = sizeof(struct ofp_vendor_header);
799         handler = recv_vendor;
800         break;
801     default:
802         VLOG_WARN_RL(&rl, "unknown openflow type: %d", oh->type);
803         send_error_msg(xid, OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE,
804                 msg, length);
805         return -EINVAL;
806     }
807
808     /* Handle it. */
809     if (length < min_size) {
810         return -EFAULT;
811     }
812     return handler(xid, msg);
813 }
814
815 bool 
816 mgmt_run(void)
817 {
818     int i;
819
820     if (!mgmt_rconn) {
821         return false;
822     }
823
824     need_reconfigure = false;
825     rconn_run(mgmt_rconn);
826
827     /* Do some processing, but cap it at a reasonable amount so that
828      * other processing doesn't starve. */
829     for (i=0; i<50; i++) {
830         struct ofpbuf *buffer;
831         struct ofp_header *oh;
832
833         buffer = rconn_recv(mgmt_rconn);
834         if (!buffer) {
835             break;
836         }
837
838         if (buffer->size >= sizeof *oh) {
839             oh = buffer->data;
840             handle_msg(oh->xid, buffer->data, buffer->size);
841             ofpbuf_delete(buffer);
842         } else {
843             VLOG_WARN_RL(&rl, "received too-short OpenFlow message");
844         }
845     }
846
847     return need_reconfigure;
848 }
849
850 void
851 mgmt_wait(void)
852 {
853     if (!mgmt_rconn) {
854         return;
855     }
856
857     rconn_run_wait(mgmt_rconn);
858     rconn_recv_wait(mgmt_rconn);
859 }
860
861 static uint64_t
862 pick_fallback_mgmt_id(void)
863 {
864     uint8_t ea[ETH_ADDR_LEN];
865     eth_addr_random(ea);
866     ea[0] = 0x00;               /* Set Nicira OUI. */
867     ea[1] = 0x23;
868     ea[2] = 0x20;
869     return eth_addr_to_uint64(ea);
870 }