datapath.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /*
  2. * Copyright (c) 2007-2012 Nicira Networks.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/system.h>
  40. #include <asm/div64.h>
  41. #include <linux/highmem.h>
  42. #include <linux/netfilter_bridge.h>
  43. #include <linux/netfilter_ipv4.h>
  44. #include <linux/inetdevice.h>
  45. #include <linux/list.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "vport-internal_dev.h"
  54. /**
  55. * DOC: Locking:
  56. *
  57. * Writes to device state (add/remove datapath, port, set operations on vports,
  58. * etc.) are protected by RTNL.
  59. *
  60. * Writes to other state (flow table modifications, set miscellaneous datapath
  61. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  62. * genl_mutex.
  63. *
  64. * Reads are protected by RCU.
  65. *
  66. * There are a few special cases (mostly stats) that have their own
  67. * synchronization but they nest under all of above and don't interact with
  68. * each other.
  69. */
  70. /* Global list of datapaths to enable dumping them all out.
  71. * Protected by genl_mutex.
  72. */
  73. static LIST_HEAD(dps);
  74. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  75. static void rehash_flow_table(struct work_struct *work);
  76. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  77. static struct vport *new_vport(const struct vport_parms *);
  78. static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
  79. const struct dp_upcall_info *);
  80. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
  81. const struct dp_upcall_info *);
  82. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  83. static struct datapath *get_dp(int dp_ifindex)
  84. {
  85. struct datapath *dp = NULL;
  86. struct net_device *dev;
  87. rcu_read_lock();
  88. dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
  89. if (dev) {
  90. struct vport *vport = ovs_internal_dev_get_vport(dev);
  91. if (vport)
  92. dp = vport->dp;
  93. }
  94. rcu_read_unlock();
  95. return dp;
  96. }
  97. /* Must be called with rcu_read_lock or RTNL lock. */
  98. const char *ovs_dp_name(const struct datapath *dp)
  99. {
  100. struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
  101. return vport->ops->get_name(vport);
  102. }
  103. static int get_dpifindex(struct datapath *dp)
  104. {
  105. struct vport *local;
  106. int ifindex;
  107. rcu_read_lock();
  108. local = rcu_dereference(dp->ports[OVSP_LOCAL]);
  109. if (local)
  110. ifindex = local->ops->get_ifindex(local);
  111. else
  112. ifindex = 0;
  113. rcu_read_unlock();
  114. return ifindex;
  115. }
  116. static void destroy_dp_rcu(struct rcu_head *rcu)
  117. {
  118. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  119. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  120. free_percpu(dp->stats_percpu);
  121. kfree(dp);
  122. }
  123. /* Called with RTNL lock and genl_lock. */
  124. static struct vport *new_vport(const struct vport_parms *parms)
  125. {
  126. struct vport *vport;
  127. vport = ovs_vport_add(parms);
  128. if (!IS_ERR(vport)) {
  129. struct datapath *dp = parms->dp;
  130. rcu_assign_pointer(dp->ports[parms->port_no], vport);
  131. list_add(&vport->node, &dp->port_list);
  132. }
  133. return vport;
  134. }
  135. /* Called with RTNL lock. */
  136. void ovs_dp_detach_port(struct vport *p)
  137. {
  138. ASSERT_RTNL();
  139. /* First drop references to device. */
  140. list_del(&p->node);
  141. rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
  142. /* Then destroy it. */
  143. ovs_vport_del(p);
  144. }
  145. /* Must be called with rcu_read_lock. */
  146. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  147. {
  148. struct datapath *dp = p->dp;
  149. struct sw_flow *flow;
  150. struct dp_stats_percpu *stats;
  151. struct sw_flow_key key;
  152. u64 *stats_counter;
  153. int error;
  154. int key_len;
  155. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  156. /* Extract flow from 'skb' into 'key'. */
  157. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  158. if (unlikely(error)) {
  159. kfree_skb(skb);
  160. return;
  161. }
  162. /* Look up flow. */
  163. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  164. if (unlikely(!flow)) {
  165. struct dp_upcall_info upcall;
  166. upcall.cmd = OVS_PACKET_CMD_MISS;
  167. upcall.key = &key;
  168. upcall.userdata = NULL;
  169. upcall.pid = p->upcall_pid;
  170. ovs_dp_upcall(dp, skb, &upcall);
  171. consume_skb(skb);
  172. stats_counter = &stats->n_missed;
  173. goto out;
  174. }
  175. OVS_CB(skb)->flow = flow;
  176. stats_counter = &stats->n_hit;
  177. ovs_flow_used(OVS_CB(skb)->flow, skb);
  178. ovs_execute_actions(dp, skb);
  179. out:
  180. /* Update datapath statistics. */
  181. u64_stats_update_begin(&stats->sync);
  182. (*stats_counter)++;
  183. u64_stats_update_end(&stats->sync);
  184. }
  185. static struct genl_family dp_packet_genl_family = {
  186. .id = GENL_ID_GENERATE,
  187. .hdrsize = sizeof(struct ovs_header),
  188. .name = OVS_PACKET_FAMILY,
  189. .version = OVS_PACKET_VERSION,
  190. .maxattr = OVS_PACKET_ATTR_MAX
  191. };
  192. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  193. const struct dp_upcall_info *upcall_info)
  194. {
  195. struct dp_stats_percpu *stats;
  196. int dp_ifindex;
  197. int err;
  198. if (upcall_info->pid == 0) {
  199. err = -ENOTCONN;
  200. goto err;
  201. }
  202. dp_ifindex = get_dpifindex(dp);
  203. if (!dp_ifindex) {
  204. err = -ENODEV;
  205. goto err;
  206. }
  207. if (!skb_is_gso(skb))
  208. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  209. else
  210. err = queue_gso_packets(dp_ifindex, skb, upcall_info);
  211. if (err)
  212. goto err;
  213. return 0;
  214. err:
  215. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  216. u64_stats_update_begin(&stats->sync);
  217. stats->n_lost++;
  218. u64_stats_update_end(&stats->sync);
  219. return err;
  220. }
  221. static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
  222. const struct dp_upcall_info *upcall_info)
  223. {
  224. struct dp_upcall_info later_info;
  225. struct sw_flow_key later_key;
  226. struct sk_buff *segs, *nskb;
  227. int err;
  228. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  229. if (IS_ERR(skb))
  230. return PTR_ERR(skb);
  231. /* Queue all of the segments. */
  232. skb = segs;
  233. do {
  234. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  235. if (err)
  236. break;
  237. if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
  238. /* The initial flow key extracted by ovs_flow_extract()
  239. * in this case is for a first fragment, so we need to
  240. * properly mark later fragments.
  241. */
  242. later_key = *upcall_info->key;
  243. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  244. later_info = *upcall_info;
  245. later_info.key = &later_key;
  246. upcall_info = &later_info;
  247. }
  248. } while ((skb = skb->next));
  249. /* Free all of the segments. */
  250. skb = segs;
  251. do {
  252. nskb = skb->next;
  253. if (err)
  254. kfree_skb(skb);
  255. else
  256. consume_skb(skb);
  257. } while ((skb = nskb));
  258. return err;
  259. }
  260. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
  261. const struct dp_upcall_info *upcall_info)
  262. {
  263. struct ovs_header *upcall;
  264. struct sk_buff *nskb = NULL;
  265. struct sk_buff *user_skb; /* to be queued to userspace */
  266. struct nlattr *nla;
  267. unsigned int len;
  268. int err;
  269. if (vlan_tx_tag_present(skb)) {
  270. nskb = skb_clone(skb, GFP_ATOMIC);
  271. if (!nskb)
  272. return -ENOMEM;
  273. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  274. if (!skb)
  275. return -ENOMEM;
  276. nskb->vlan_tci = 0;
  277. skb = nskb;
  278. }
  279. if (nla_attr_size(skb->len) > USHRT_MAX) {
  280. err = -EFBIG;
  281. goto out;
  282. }
  283. len = sizeof(struct ovs_header);
  284. len += nla_total_size(skb->len);
  285. len += nla_total_size(FLOW_BUFSIZE);
  286. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  287. len += nla_total_size(8);
  288. user_skb = genlmsg_new(len, GFP_ATOMIC);
  289. if (!user_skb) {
  290. err = -ENOMEM;
  291. goto out;
  292. }
  293. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  294. 0, upcall_info->cmd);
  295. upcall->dp_ifindex = dp_ifindex;
  296. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  297. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  298. nla_nest_end(user_skb, nla);
  299. if (upcall_info->userdata)
  300. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  301. nla_get_u64(upcall_info->userdata));
  302. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  303. skb_copy_and_csum_dev(skb, nla_data(nla));
  304. err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
  305. out:
  306. kfree_skb(nskb);
  307. return err;
  308. }
  309. /* Called with genl_mutex. */
  310. static int flush_flows(int dp_ifindex)
  311. {
  312. struct flow_table *old_table;
  313. struct flow_table *new_table;
  314. struct datapath *dp;
  315. dp = get_dp(dp_ifindex);
  316. if (!dp)
  317. return -ENODEV;
  318. old_table = genl_dereference(dp->table);
  319. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  320. if (!new_table)
  321. return -ENOMEM;
  322. rcu_assign_pointer(dp->table, new_table);
  323. ovs_flow_tbl_deferred_destroy(old_table);
  324. return 0;
  325. }
  326. static int validate_actions(const struct nlattr *attr,
  327. const struct sw_flow_key *key, int depth);
  328. static int validate_sample(const struct nlattr *attr,
  329. const struct sw_flow_key *key, int depth)
  330. {
  331. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  332. const struct nlattr *probability, *actions;
  333. const struct nlattr *a;
  334. int rem;
  335. memset(attrs, 0, sizeof(attrs));
  336. nla_for_each_nested(a, attr, rem) {
  337. int type = nla_type(a);
  338. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  339. return -EINVAL;
  340. attrs[type] = a;
  341. }
  342. if (rem)
  343. return -EINVAL;
  344. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  345. if (!probability || nla_len(probability) != sizeof(u32))
  346. return -EINVAL;
  347. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  348. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  349. return -EINVAL;
  350. return validate_actions(actions, key, depth + 1);
  351. }
  352. static int validate_set(const struct nlattr *a,
  353. const struct sw_flow_key *flow_key)
  354. {
  355. const struct nlattr *ovs_key = nla_data(a);
  356. int key_type = nla_type(ovs_key);
  357. /* There can be only one key in a action */
  358. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  359. return -EINVAL;
  360. if (key_type > OVS_KEY_ATTR_MAX ||
  361. nla_len(ovs_key) != ovs_key_lens[key_type])
  362. return -EINVAL;
  363. switch (key_type) {
  364. const struct ovs_key_ipv4 *ipv4_key;
  365. case OVS_KEY_ATTR_PRIORITY:
  366. case OVS_KEY_ATTR_ETHERNET:
  367. break;
  368. case OVS_KEY_ATTR_IPV4:
  369. if (flow_key->eth.type != htons(ETH_P_IP))
  370. return -EINVAL;
  371. if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
  372. return -EINVAL;
  373. ipv4_key = nla_data(ovs_key);
  374. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  375. return -EINVAL;
  376. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  377. return -EINVAL;
  378. break;
  379. case OVS_KEY_ATTR_TCP:
  380. if (flow_key->ip.proto != IPPROTO_TCP)
  381. return -EINVAL;
  382. if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
  383. return -EINVAL;
  384. break;
  385. case OVS_KEY_ATTR_UDP:
  386. if (flow_key->ip.proto != IPPROTO_UDP)
  387. return -EINVAL;
  388. if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
  389. return -EINVAL;
  390. break;
  391. default:
  392. return -EINVAL;
  393. }
  394. return 0;
  395. }
  396. static int validate_userspace(const struct nlattr *attr)
  397. {
  398. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  399. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  400. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  401. };
  402. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  403. int error;
  404. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  405. attr, userspace_policy);
  406. if (error)
  407. return error;
  408. if (!a[OVS_USERSPACE_ATTR_PID] ||
  409. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  410. return -EINVAL;
  411. return 0;
  412. }
  413. static int validate_actions(const struct nlattr *attr,
  414. const struct sw_flow_key *key, int depth)
  415. {
  416. const struct nlattr *a;
  417. int rem, err;
  418. if (depth >= SAMPLE_ACTION_DEPTH)
  419. return -EOVERFLOW;
  420. nla_for_each_nested(a, attr, rem) {
  421. /* Expected argument lengths, (u32)-1 for variable length. */
  422. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  423. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  424. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  425. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  426. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  427. [OVS_ACTION_ATTR_SET] = (u32)-1,
  428. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  429. };
  430. const struct ovs_action_push_vlan *vlan;
  431. int type = nla_type(a);
  432. if (type > OVS_ACTION_ATTR_MAX ||
  433. (action_lens[type] != nla_len(a) &&
  434. action_lens[type] != (u32)-1))
  435. return -EINVAL;
  436. switch (type) {
  437. case OVS_ACTION_ATTR_UNSPEC:
  438. return -EINVAL;
  439. case OVS_ACTION_ATTR_USERSPACE:
  440. err = validate_userspace(a);
  441. if (err)
  442. return err;
  443. break;
  444. case OVS_ACTION_ATTR_OUTPUT:
  445. if (nla_get_u32(a) >= DP_MAX_PORTS)
  446. return -EINVAL;
  447. break;
  448. case OVS_ACTION_ATTR_POP_VLAN:
  449. break;
  450. case OVS_ACTION_ATTR_PUSH_VLAN:
  451. vlan = nla_data(a);
  452. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  453. return -EINVAL;
  454. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  455. return -EINVAL;
  456. break;
  457. case OVS_ACTION_ATTR_SET:
  458. err = validate_set(a, key);
  459. if (err)
  460. return err;
  461. break;
  462. case OVS_ACTION_ATTR_SAMPLE:
  463. err = validate_sample(a, key, depth);
  464. if (err)
  465. return err;
  466. break;
  467. default:
  468. return -EINVAL;
  469. }
  470. }
  471. if (rem > 0)
  472. return -EINVAL;
  473. return 0;
  474. }
  475. static void clear_stats(struct sw_flow *flow)
  476. {
  477. flow->used = 0;
  478. flow->tcp_flags = 0;
  479. flow->packet_count = 0;
  480. flow->byte_count = 0;
  481. }
  482. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  483. {
  484. struct ovs_header *ovs_header = info->userhdr;
  485. struct nlattr **a = info->attrs;
  486. struct sw_flow_actions *acts;
  487. struct sk_buff *packet;
  488. struct sw_flow *flow;
  489. struct datapath *dp;
  490. struct ethhdr *eth;
  491. int len;
  492. int err;
  493. int key_len;
  494. err = -EINVAL;
  495. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  496. !a[OVS_PACKET_ATTR_ACTIONS] ||
  497. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  498. goto err;
  499. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  500. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  501. err = -ENOMEM;
  502. if (!packet)
  503. goto err;
  504. skb_reserve(packet, NET_IP_ALIGN);
  505. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  506. skb_reset_mac_header(packet);
  507. eth = eth_hdr(packet);
  508. /* Normally, setting the skb 'protocol' field would be handled by a
  509. * call to eth_type_trans(), but it assumes there's a sending
  510. * device, which we may not have. */
  511. if (ntohs(eth->h_proto) >= 1536)
  512. packet->protocol = eth->h_proto;
  513. else
  514. packet->protocol = htons(ETH_P_802_2);
  515. /* Build an sw_flow for sending this packet. */
  516. flow = ovs_flow_alloc();
  517. err = PTR_ERR(flow);
  518. if (IS_ERR(flow))
  519. goto err_kfree_skb;
  520. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  521. if (err)
  522. goto err_flow_free;
  523. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  524. &flow->key.phy.in_port,
  525. a[OVS_PACKET_ATTR_KEY]);
  526. if (err)
  527. goto err_flow_free;
  528. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  529. if (err)
  530. goto err_flow_free;
  531. flow->hash = ovs_flow_hash(&flow->key, key_len);
  532. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  533. err = PTR_ERR(acts);
  534. if (IS_ERR(acts))
  535. goto err_flow_free;
  536. rcu_assign_pointer(flow->sf_acts, acts);
  537. OVS_CB(packet)->flow = flow;
  538. packet->priority = flow->key.phy.priority;
  539. rcu_read_lock();
  540. dp = get_dp(ovs_header->dp_ifindex);
  541. err = -ENODEV;
  542. if (!dp)
  543. goto err_unlock;
  544. local_bh_disable();
  545. err = ovs_execute_actions(dp, packet);
  546. local_bh_enable();
  547. rcu_read_unlock();
  548. ovs_flow_free(flow);
  549. return err;
  550. err_unlock:
  551. rcu_read_unlock();
  552. err_flow_free:
  553. ovs_flow_free(flow);
  554. err_kfree_skb:
  555. kfree_skb(packet);
  556. err:
  557. return err;
  558. }
  559. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  560. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  561. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  562. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  563. };
  564. static struct genl_ops dp_packet_genl_ops[] = {
  565. { .cmd = OVS_PACKET_CMD_EXECUTE,
  566. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  567. .policy = packet_policy,
  568. .doit = ovs_packet_cmd_execute
  569. }
  570. };
  571. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  572. {
  573. int i;
  574. struct flow_table *table = genl_dereference(dp->table);
  575. stats->n_flows = ovs_flow_tbl_count(table);
  576. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  577. for_each_possible_cpu(i) {
  578. const struct dp_stats_percpu *percpu_stats;
  579. struct dp_stats_percpu local_stats;
  580. unsigned int start;
  581. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  582. do {
  583. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  584. local_stats = *percpu_stats;
  585. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  586. stats->n_hit += local_stats.n_hit;
  587. stats->n_missed += local_stats.n_missed;
  588. stats->n_lost += local_stats.n_lost;
  589. }
  590. }
  591. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  592. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  593. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  594. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  595. };
  596. static struct genl_family dp_flow_genl_family = {
  597. .id = GENL_ID_GENERATE,
  598. .hdrsize = sizeof(struct ovs_header),
  599. .name = OVS_FLOW_FAMILY,
  600. .version = OVS_FLOW_VERSION,
  601. .maxattr = OVS_FLOW_ATTR_MAX
  602. };
  603. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  604. .name = OVS_FLOW_MCGROUP
  605. };
  606. /* Called with genl_lock. */
  607. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  608. struct sk_buff *skb, u32 pid,
  609. u32 seq, u32 flags, u8 cmd)
  610. {
  611. const int skb_orig_len = skb->len;
  612. const struct sw_flow_actions *sf_acts;
  613. struct ovs_flow_stats stats;
  614. struct ovs_header *ovs_header;
  615. struct nlattr *nla;
  616. unsigned long used;
  617. u8 tcp_flags;
  618. int err;
  619. sf_acts = rcu_dereference_protected(flow->sf_acts,
  620. lockdep_genl_is_held());
  621. ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
  622. if (!ovs_header)
  623. return -EMSGSIZE;
  624. ovs_header->dp_ifindex = get_dpifindex(dp);
  625. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  626. if (!nla)
  627. goto nla_put_failure;
  628. err = ovs_flow_to_nlattrs(&flow->key, skb);
  629. if (err)
  630. goto error;
  631. nla_nest_end(skb, nla);
  632. spin_lock_bh(&flow->lock);
  633. used = flow->used;
  634. stats.n_packets = flow->packet_count;
  635. stats.n_bytes = flow->byte_count;
  636. tcp_flags = flow->tcp_flags;
  637. spin_unlock_bh(&flow->lock);
  638. if (used &&
  639. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  640. goto nla_put_failure;
  641. if (stats.n_packets &&
  642. nla_put(skb, OVS_FLOW_ATTR_STATS,
  643. sizeof(struct ovs_flow_stats), &stats))
  644. goto nla_put_failure;
  645. if (tcp_flags &&
  646. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  647. goto nla_put_failure;
  648. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  649. * this is the first flow to be dumped into 'skb'. This is unusual for
  650. * Netlink but individual action lists can be longer than
  651. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  652. * The userspace caller can always fetch the actions separately if it
  653. * really wants them. (Most userspace callers in fact don't care.)
  654. *
  655. * This can only fail for dump operations because the skb is always
  656. * properly sized for single flows.
  657. */
  658. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  659. sf_acts->actions);
  660. if (err < 0 && skb_orig_len)
  661. goto error;
  662. return genlmsg_end(skb, ovs_header);
  663. nla_put_failure:
  664. err = -EMSGSIZE;
  665. error:
  666. genlmsg_cancel(skb, ovs_header);
  667. return err;
  668. }
  669. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  670. {
  671. const struct sw_flow_actions *sf_acts;
  672. int len;
  673. sf_acts = rcu_dereference_protected(flow->sf_acts,
  674. lockdep_genl_is_held());
  675. /* OVS_FLOW_ATTR_KEY */
  676. len = nla_total_size(FLOW_BUFSIZE);
  677. /* OVS_FLOW_ATTR_ACTIONS */
  678. len += nla_total_size(sf_acts->actions_len);
  679. /* OVS_FLOW_ATTR_STATS */
  680. len += nla_total_size(sizeof(struct ovs_flow_stats));
  681. /* OVS_FLOW_ATTR_TCP_FLAGS */
  682. len += nla_total_size(1);
  683. /* OVS_FLOW_ATTR_USED */
  684. len += nla_total_size(8);
  685. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  686. return genlmsg_new(len, GFP_KERNEL);
  687. }
  688. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  689. struct datapath *dp,
  690. u32 pid, u32 seq, u8 cmd)
  691. {
  692. struct sk_buff *skb;
  693. int retval;
  694. skb = ovs_flow_cmd_alloc_info(flow);
  695. if (!skb)
  696. return ERR_PTR(-ENOMEM);
  697. retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
  698. BUG_ON(retval < 0);
  699. return skb;
  700. }
  701. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  702. {
  703. struct nlattr **a = info->attrs;
  704. struct ovs_header *ovs_header = info->userhdr;
  705. struct sw_flow_key key;
  706. struct sw_flow *flow;
  707. struct sk_buff *reply;
  708. struct datapath *dp;
  709. struct flow_table *table;
  710. int error;
  711. int key_len;
  712. /* Extract key. */
  713. error = -EINVAL;
  714. if (!a[OVS_FLOW_ATTR_KEY])
  715. goto error;
  716. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  717. if (error)
  718. goto error;
  719. /* Validate actions. */
  720. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  721. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  722. if (error)
  723. goto error;
  724. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  725. error = -EINVAL;
  726. goto error;
  727. }
  728. dp = get_dp(ovs_header->dp_ifindex);
  729. error = -ENODEV;
  730. if (!dp)
  731. goto error;
  732. table = genl_dereference(dp->table);
  733. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  734. if (!flow) {
  735. struct sw_flow_actions *acts;
  736. /* Bail out if we're not allowed to create a new flow. */
  737. error = -ENOENT;
  738. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  739. goto error;
  740. /* Expand table, if necessary, to make room. */
  741. if (ovs_flow_tbl_need_to_expand(table)) {
  742. struct flow_table *new_table;
  743. new_table = ovs_flow_tbl_expand(table);
  744. if (!IS_ERR(new_table)) {
  745. rcu_assign_pointer(dp->table, new_table);
  746. ovs_flow_tbl_deferred_destroy(table);
  747. table = genl_dereference(dp->table);
  748. }
  749. }
  750. /* Allocate flow. */
  751. flow = ovs_flow_alloc();
  752. if (IS_ERR(flow)) {
  753. error = PTR_ERR(flow);
  754. goto error;
  755. }
  756. flow->key = key;
  757. clear_stats(flow);
  758. /* Obtain actions. */
  759. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  760. error = PTR_ERR(acts);
  761. if (IS_ERR(acts))
  762. goto error_free_flow;
  763. rcu_assign_pointer(flow->sf_acts, acts);
  764. /* Put flow in bucket. */
  765. flow->hash = ovs_flow_hash(&key, key_len);
  766. ovs_flow_tbl_insert(table, flow);
  767. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  768. info->snd_seq,
  769. OVS_FLOW_CMD_NEW);
  770. } else {
  771. /* We found a matching flow. */
  772. struct sw_flow_actions *old_acts;
  773. struct nlattr *acts_attrs;
  774. /* Bail out if we're not allowed to modify an existing flow.
  775. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  776. * because Generic Netlink treats the latter as a dump
  777. * request. We also accept NLM_F_EXCL in case that bug ever
  778. * gets fixed.
  779. */
  780. error = -EEXIST;
  781. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  782. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  783. goto error;
  784. /* Update actions. */
  785. old_acts = rcu_dereference_protected(flow->sf_acts,
  786. lockdep_genl_is_held());
  787. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  788. if (acts_attrs &&
  789. (old_acts->actions_len != nla_len(acts_attrs) ||
  790. memcmp(old_acts->actions, nla_data(acts_attrs),
  791. old_acts->actions_len))) {
  792. struct sw_flow_actions *new_acts;
  793. new_acts = ovs_flow_actions_alloc(acts_attrs);
  794. error = PTR_ERR(new_acts);
  795. if (IS_ERR(new_acts))
  796. goto error;
  797. rcu_assign_pointer(flow->sf_acts, new_acts);
  798. ovs_flow_deferred_free_acts(old_acts);
  799. }
  800. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  801. info->snd_seq, OVS_FLOW_CMD_NEW);
  802. /* Clear stats. */
  803. if (a[OVS_FLOW_ATTR_CLEAR]) {
  804. spin_lock_bh(&flow->lock);
  805. clear_stats(flow);
  806. spin_unlock_bh(&flow->lock);
  807. }
  808. }
  809. if (!IS_ERR(reply))
  810. genl_notify(reply, genl_info_net(info), info->snd_pid,
  811. ovs_dp_flow_multicast_group.id, info->nlhdr,
  812. GFP_KERNEL);
  813. else
  814. netlink_set_err(init_net.genl_sock, 0,
  815. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  816. return 0;
  817. error_free_flow:
  818. ovs_flow_free(flow);
  819. error:
  820. return error;
  821. }
  822. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  823. {
  824. struct nlattr **a = info->attrs;
  825. struct ovs_header *ovs_header = info->userhdr;
  826. struct sw_flow_key key;
  827. struct sk_buff *reply;
  828. struct sw_flow *flow;
  829. struct datapath *dp;
  830. struct flow_table *table;
  831. int err;
  832. int key_len;
  833. if (!a[OVS_FLOW_ATTR_KEY])
  834. return -EINVAL;
  835. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  836. if (err)
  837. return err;
  838. dp = get_dp(ovs_header->dp_ifindex);
  839. if (!dp)
  840. return -ENODEV;
  841. table = genl_dereference(dp->table);
  842. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  843. if (!flow)
  844. return -ENOENT;
  845. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  846. info->snd_seq, OVS_FLOW_CMD_NEW);
  847. if (IS_ERR(reply))
  848. return PTR_ERR(reply);
  849. return genlmsg_reply(reply, info);
  850. }
  851. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  852. {
  853. struct nlattr **a = info->attrs;
  854. struct ovs_header *ovs_header = info->userhdr;
  855. struct sw_flow_key key;
  856. struct sk_buff *reply;
  857. struct sw_flow *flow;
  858. struct datapath *dp;
  859. struct flow_table *table;
  860. int err;
  861. int key_len;
  862. if (!a[OVS_FLOW_ATTR_KEY])
  863. return flush_flows(ovs_header->dp_ifindex);
  864. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  865. if (err)
  866. return err;
  867. dp = get_dp(ovs_header->dp_ifindex);
  868. if (!dp)
  869. return -ENODEV;
  870. table = genl_dereference(dp->table);
  871. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  872. if (!flow)
  873. return -ENOENT;
  874. reply = ovs_flow_cmd_alloc_info(flow);
  875. if (!reply)
  876. return -ENOMEM;
  877. ovs_flow_tbl_remove(table, flow);
  878. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
  879. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  880. BUG_ON(err < 0);
  881. ovs_flow_deferred_free(flow);
  882. genl_notify(reply, genl_info_net(info), info->snd_pid,
  883. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  884. return 0;
  885. }
  886. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  887. {
  888. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  889. struct datapath *dp;
  890. struct flow_table *table;
  891. dp = get_dp(ovs_header->dp_ifindex);
  892. if (!dp)
  893. return -ENODEV;
  894. table = genl_dereference(dp->table);
  895. for (;;) {
  896. struct sw_flow *flow;
  897. u32 bucket, obj;
  898. bucket = cb->args[0];
  899. obj = cb->args[1];
  900. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  901. if (!flow)
  902. break;
  903. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  904. NETLINK_CB(cb->skb).pid,
  905. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  906. OVS_FLOW_CMD_NEW) < 0)
  907. break;
  908. cb->args[0] = bucket;
  909. cb->args[1] = obj;
  910. }
  911. return skb->len;
  912. }
  913. static struct genl_ops dp_flow_genl_ops[] = {
  914. { .cmd = OVS_FLOW_CMD_NEW,
  915. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  916. .policy = flow_policy,
  917. .doit = ovs_flow_cmd_new_or_set
  918. },
  919. { .cmd = OVS_FLOW_CMD_DEL,
  920. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  921. .policy = flow_policy,
  922. .doit = ovs_flow_cmd_del
  923. },
  924. { .cmd = OVS_FLOW_CMD_GET,
  925. .flags = 0, /* OK for unprivileged users. */
  926. .policy = flow_policy,
  927. .doit = ovs_flow_cmd_get,
  928. .dumpit = ovs_flow_cmd_dump
  929. },
  930. { .cmd = OVS_FLOW_CMD_SET,
  931. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  932. .policy = flow_policy,
  933. .doit = ovs_flow_cmd_new_or_set,
  934. },
  935. };
  936. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  937. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  938. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  939. };
  940. static struct genl_family dp_datapath_genl_family = {
  941. .id = GENL_ID_GENERATE,
  942. .hdrsize = sizeof(struct ovs_header),
  943. .name = OVS_DATAPATH_FAMILY,
  944. .version = OVS_DATAPATH_VERSION,
  945. .maxattr = OVS_DP_ATTR_MAX
  946. };
  947. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  948. .name = OVS_DATAPATH_MCGROUP
  949. };
  950. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  951. u32 pid, u32 seq, u32 flags, u8 cmd)
  952. {
  953. struct ovs_header *ovs_header;
  954. struct ovs_dp_stats dp_stats;
  955. int err;
  956. ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
  957. flags, cmd);
  958. if (!ovs_header)
  959. goto error;
  960. ovs_header->dp_ifindex = get_dpifindex(dp);
  961. rcu_read_lock();
  962. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  963. rcu_read_unlock();
  964. if (err)
  965. goto nla_put_failure;
  966. get_dp_stats(dp, &dp_stats);
  967. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  968. goto nla_put_failure;
  969. return genlmsg_end(skb, ovs_header);
  970. nla_put_failure:
  971. genlmsg_cancel(skb, ovs_header);
  972. error:
  973. return -EMSGSIZE;
  974. }
  975. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
  976. u32 seq, u8 cmd)
  977. {
  978. struct sk_buff *skb;
  979. int retval;
  980. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  981. if (!skb)
  982. return ERR_PTR(-ENOMEM);
  983. retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
  984. if (retval < 0) {
  985. kfree_skb(skb);
  986. return ERR_PTR(retval);
  987. }
  988. return skb;
  989. }
  990. /* Called with genl_mutex and optionally with RTNL lock also. */
  991. static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
  992. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  993. {
  994. struct datapath *dp;
  995. if (!a[OVS_DP_ATTR_NAME])
  996. dp = get_dp(ovs_header->dp_ifindex);
  997. else {
  998. struct vport *vport;
  999. rcu_read_lock();
  1000. vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
  1001. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1002. rcu_read_unlock();
  1003. }
  1004. return dp ? dp : ERR_PTR(-ENODEV);
  1005. }
  1006. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1007. {
  1008. struct nlattr **a = info->attrs;
  1009. struct vport_parms parms;
  1010. struct sk_buff *reply;
  1011. struct datapath *dp;
  1012. struct vport *vport;
  1013. int err;
  1014. err = -EINVAL;
  1015. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1016. goto err;
  1017. rtnl_lock();
  1018. err = -ENODEV;
  1019. if (!try_module_get(THIS_MODULE))
  1020. goto err_unlock_rtnl;
  1021. err = -ENOMEM;
  1022. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1023. if (dp == NULL)
  1024. goto err_put_module;
  1025. INIT_LIST_HEAD(&dp->port_list);
  1026. /* Allocate table. */
  1027. err = -ENOMEM;
  1028. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1029. if (!dp->table)
  1030. goto err_free_dp;
  1031. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1032. if (!dp->stats_percpu) {
  1033. err = -ENOMEM;
  1034. goto err_destroy_table;
  1035. }
  1036. /* Set up our datapath device. */
  1037. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1038. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1039. parms.options = NULL;
  1040. parms.dp = dp;
  1041. parms.port_no = OVSP_LOCAL;
  1042. parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1043. vport = new_vport(&parms);
  1044. if (IS_ERR(vport)) {
  1045. err = PTR_ERR(vport);
  1046. if (err == -EBUSY)
  1047. err = -EEXIST;
  1048. goto err_destroy_percpu;
  1049. }
  1050. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1051. info->snd_seq, OVS_DP_CMD_NEW);
  1052. err = PTR_ERR(reply);
  1053. if (IS_ERR(reply))
  1054. goto err_destroy_local_port;
  1055. list_add_tail(&dp->list_node, &dps);
  1056. rtnl_unlock();
  1057. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1058. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1059. GFP_KERNEL);
  1060. return 0;
  1061. err_destroy_local_port:
  1062. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1063. err_destroy_percpu:
  1064. free_percpu(dp->stats_percpu);
  1065. err_destroy_table:
  1066. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1067. err_free_dp:
  1068. kfree(dp);
  1069. err_put_module:
  1070. module_put(THIS_MODULE);
  1071. err_unlock_rtnl:
  1072. rtnl_unlock();
  1073. err:
  1074. return err;
  1075. }
  1076. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1077. {
  1078. struct vport *vport, *next_vport;
  1079. struct sk_buff *reply;
  1080. struct datapath *dp;
  1081. int err;
  1082. rtnl_lock();
  1083. dp = lookup_datapath(info->userhdr, info->attrs);
  1084. err = PTR_ERR(dp);
  1085. if (IS_ERR(dp))
  1086. goto exit_unlock;
  1087. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1088. info->snd_seq, OVS_DP_CMD_DEL);
  1089. err = PTR_ERR(reply);
  1090. if (IS_ERR(reply))
  1091. goto exit_unlock;
  1092. list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
  1093. if (vport->port_no != OVSP_LOCAL)
  1094. ovs_dp_detach_port(vport);
  1095. list_del(&dp->list_node);
  1096. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1097. /* rtnl_unlock() will wait until all the references to devices that
  1098. * are pending unregistration have been dropped. We do it here to
  1099. * ensure that any internal devices (which contain DP pointers) are
  1100. * fully destroyed before freeing the datapath.
  1101. */
  1102. rtnl_unlock();
  1103. call_rcu(&dp->rcu, destroy_dp_rcu);
  1104. module_put(THIS_MODULE);
  1105. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1106. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1107. GFP_KERNEL);
  1108. return 0;
  1109. exit_unlock:
  1110. rtnl_unlock();
  1111. return err;
  1112. }
  1113. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1114. {
  1115. struct sk_buff *reply;
  1116. struct datapath *dp;
  1117. int err;
  1118. dp = lookup_datapath(info->userhdr, info->attrs);
  1119. if (IS_ERR(dp))
  1120. return PTR_ERR(dp);
  1121. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1122. info->snd_seq, OVS_DP_CMD_NEW);
  1123. if (IS_ERR(reply)) {
  1124. err = PTR_ERR(reply);
  1125. netlink_set_err(init_net.genl_sock, 0,
  1126. ovs_dp_datapath_multicast_group.id, err);
  1127. return 0;
  1128. }
  1129. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1130. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1131. GFP_KERNEL);
  1132. return 0;
  1133. }
  1134. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1135. {
  1136. struct sk_buff *reply;
  1137. struct datapath *dp;
  1138. dp = lookup_datapath(info->userhdr, info->attrs);
  1139. if (IS_ERR(dp))
  1140. return PTR_ERR(dp);
  1141. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1142. info->snd_seq, OVS_DP_CMD_NEW);
  1143. if (IS_ERR(reply))
  1144. return PTR_ERR(reply);
  1145. return genlmsg_reply(reply, info);
  1146. }
  1147. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1148. {
  1149. struct datapath *dp;
  1150. int skip = cb->args[0];
  1151. int i = 0;
  1152. list_for_each_entry(dp, &dps, list_node) {
  1153. if (i >= skip &&
  1154. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
  1155. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1156. OVS_DP_CMD_NEW) < 0)
  1157. break;
  1158. i++;
  1159. }
  1160. cb->args[0] = i;
  1161. return skb->len;
  1162. }
  1163. static struct genl_ops dp_datapath_genl_ops[] = {
  1164. { .cmd = OVS_DP_CMD_NEW,
  1165. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1166. .policy = datapath_policy,
  1167. .doit = ovs_dp_cmd_new
  1168. },
  1169. { .cmd = OVS_DP_CMD_DEL,
  1170. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1171. .policy = datapath_policy,
  1172. .doit = ovs_dp_cmd_del
  1173. },
  1174. { .cmd = OVS_DP_CMD_GET,
  1175. .flags = 0, /* OK for unprivileged users. */
  1176. .policy = datapath_policy,
  1177. .doit = ovs_dp_cmd_get,
  1178. .dumpit = ovs_dp_cmd_dump
  1179. },
  1180. { .cmd = OVS_DP_CMD_SET,
  1181. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1182. .policy = datapath_policy,
  1183. .doit = ovs_dp_cmd_set,
  1184. },
  1185. };
  1186. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1187. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1188. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1189. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1190. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1191. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1192. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1193. };
  1194. static struct genl_family dp_vport_genl_family = {
  1195. .id = GENL_ID_GENERATE,
  1196. .hdrsize = sizeof(struct ovs_header),
  1197. .name = OVS_VPORT_FAMILY,
  1198. .version = OVS_VPORT_VERSION,
  1199. .maxattr = OVS_VPORT_ATTR_MAX
  1200. };
  1201. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1202. .name = OVS_VPORT_MCGROUP
  1203. };
  1204. /* Called with RTNL lock or RCU read lock. */
  1205. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1206. u32 pid, u32 seq, u32 flags, u8 cmd)
  1207. {
  1208. struct ovs_header *ovs_header;
  1209. struct ovs_vport_stats vport_stats;
  1210. int err;
  1211. ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
  1212. flags, cmd);
  1213. if (!ovs_header)
  1214. return -EMSGSIZE;
  1215. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1216. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1217. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1218. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1219. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid))
  1220. goto nla_put_failure;
  1221. ovs_vport_get_stats(vport, &vport_stats);
  1222. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1223. &vport_stats))
  1224. goto nla_put_failure;
  1225. err = ovs_vport_get_options(vport, skb);
  1226. if (err == -EMSGSIZE)
  1227. goto error;
  1228. return genlmsg_end(skb, ovs_header);
  1229. nla_put_failure:
  1230. err = -EMSGSIZE;
  1231. error:
  1232. genlmsg_cancel(skb, ovs_header);
  1233. return err;
  1234. }
  1235. /* Called with RTNL lock or RCU read lock. */
  1236. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
  1237. u32 seq, u8 cmd)
  1238. {
  1239. struct sk_buff *skb;
  1240. int retval;
  1241. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1242. if (!skb)
  1243. return ERR_PTR(-ENOMEM);
  1244. retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
  1245. if (retval < 0) {
  1246. kfree_skb(skb);
  1247. return ERR_PTR(retval);
  1248. }
  1249. return skb;
  1250. }
  1251. /* Called with RTNL lock or RCU read lock. */
  1252. static struct vport *lookup_vport(struct ovs_header *ovs_header,
  1253. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1254. {
  1255. struct datapath *dp;
  1256. struct vport *vport;
  1257. if (a[OVS_VPORT_ATTR_NAME]) {
  1258. vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
  1259. if (!vport)
  1260. return ERR_PTR(-ENODEV);
  1261. if (ovs_header->dp_ifindex &&
  1262. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1263. return ERR_PTR(-ENODEV);
  1264. return vport;
  1265. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1266. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1267. if (port_no >= DP_MAX_PORTS)
  1268. return ERR_PTR(-EFBIG);
  1269. dp = get_dp(ovs_header->dp_ifindex);
  1270. if (!dp)
  1271. return ERR_PTR(-ENODEV);
  1272. vport = rcu_dereference_rtnl(dp->ports[port_no]);
  1273. if (!vport)
  1274. return ERR_PTR(-ENOENT);
  1275. return vport;
  1276. } else
  1277. return ERR_PTR(-EINVAL);
  1278. }
  1279. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1280. {
  1281. struct nlattr **a = info->attrs;
  1282. struct ovs_header *ovs_header = info->userhdr;
  1283. struct vport_parms parms;
  1284. struct sk_buff *reply;
  1285. struct vport *vport;
  1286. struct datapath *dp;
  1287. u32 port_no;
  1288. int err;
  1289. err = -EINVAL;
  1290. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1291. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1292. goto exit;
  1293. rtnl_lock();
  1294. dp = get_dp(ovs_header->dp_ifindex);
  1295. err = -ENODEV;
  1296. if (!dp)
  1297. goto exit_unlock;
  1298. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1299. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1300. err = -EFBIG;
  1301. if (port_no >= DP_MAX_PORTS)
  1302. goto exit_unlock;
  1303. vport = rtnl_dereference(dp->ports[port_no]);
  1304. err = -EBUSY;
  1305. if (vport)
  1306. goto exit_unlock;
  1307. } else {
  1308. for (port_no = 1; ; port_no++) {
  1309. if (port_no >= DP_MAX_PORTS) {
  1310. err = -EFBIG;
  1311. goto exit_unlock;
  1312. }
  1313. vport = rtnl_dereference(dp->ports[port_no]);
  1314. if (!vport)
  1315. break;
  1316. }
  1317. }
  1318. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1319. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1320. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1321. parms.dp = dp;
  1322. parms.port_no = port_no;
  1323. parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1324. vport = new_vport(&parms);
  1325. err = PTR_ERR(vport);
  1326. if (IS_ERR(vport))
  1327. goto exit_unlock;
  1328. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1329. OVS_VPORT_CMD_NEW);
  1330. if (IS_ERR(reply)) {
  1331. err = PTR_ERR(reply);
  1332. ovs_dp_detach_port(vport);
  1333. goto exit_unlock;
  1334. }
  1335. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1336. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1337. exit_unlock:
  1338. rtnl_unlock();
  1339. exit:
  1340. return err;
  1341. }
  1342. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1343. {
  1344. struct nlattr **a = info->attrs;
  1345. struct sk_buff *reply;
  1346. struct vport *vport;
  1347. int err;
  1348. rtnl_lock();
  1349. vport = lookup_vport(info->userhdr, a);
  1350. err = PTR_ERR(vport);
  1351. if (IS_ERR(vport))
  1352. goto exit_unlock;
  1353. err = 0;
  1354. if (a[OVS_VPORT_ATTR_TYPE] &&
  1355. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1356. err = -EINVAL;
  1357. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1358. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1359. if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
  1360. vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1361. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1362. OVS_VPORT_CMD_NEW);
  1363. if (IS_ERR(reply)) {
  1364. err = PTR_ERR(reply);
  1365. netlink_set_err(init_net.genl_sock, 0,
  1366. ovs_dp_vport_multicast_group.id, err);
  1367. return 0;
  1368. }
  1369. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1370. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1371. exit_unlock:
  1372. rtnl_unlock();
  1373. return err;
  1374. }
  1375. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1376. {
  1377. struct nlattr **a = info->attrs;
  1378. struct sk_buff *reply;
  1379. struct vport *vport;
  1380. int err;
  1381. rtnl_lock();
  1382. vport = lookup_vport(info->userhdr, a);
  1383. err = PTR_ERR(vport);
  1384. if (IS_ERR(vport))
  1385. goto exit_unlock;
  1386. if (vport->port_no == OVSP_LOCAL) {
  1387. err = -EINVAL;
  1388. goto exit_unlock;
  1389. }
  1390. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1391. OVS_VPORT_CMD_DEL);
  1392. err = PTR_ERR(reply);
  1393. if (IS_ERR(reply))
  1394. goto exit_unlock;
  1395. ovs_dp_detach_port(vport);
  1396. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1397. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1398. exit_unlock:
  1399. rtnl_unlock();
  1400. return err;
  1401. }
  1402. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1403. {
  1404. struct nlattr **a = info->attrs;
  1405. struct ovs_header *ovs_header = info->userhdr;
  1406. struct sk_buff *reply;
  1407. struct vport *vport;
  1408. int err;
  1409. rcu_read_lock();
  1410. vport = lookup_vport(ovs_header, a);
  1411. err = PTR_ERR(vport);
  1412. if (IS_ERR(vport))
  1413. goto exit_unlock;
  1414. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1415. OVS_VPORT_CMD_NEW);
  1416. err = PTR_ERR(reply);
  1417. if (IS_ERR(reply))
  1418. goto exit_unlock;
  1419. rcu_read_unlock();
  1420. return genlmsg_reply(reply, info);
  1421. exit_unlock:
  1422. rcu_read_unlock();
  1423. return err;
  1424. }
  1425. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1426. {
  1427. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1428. struct datapath *dp;
  1429. u32 port_no;
  1430. int retval;
  1431. dp = get_dp(ovs_header->dp_ifindex);
  1432. if (!dp)
  1433. return -ENODEV;
  1434. rcu_read_lock();
  1435. for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
  1436. struct vport *vport;
  1437. vport = rcu_dereference(dp->ports[port_no]);
  1438. if (!vport)
  1439. continue;
  1440. if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
  1441. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1442. OVS_VPORT_CMD_NEW) < 0)
  1443. break;
  1444. }
  1445. rcu_read_unlock();
  1446. cb->args[0] = port_no;
  1447. retval = skb->len;
  1448. return retval;
  1449. }
  1450. static void rehash_flow_table(struct work_struct *work)
  1451. {
  1452. struct datapath *dp;
  1453. genl_lock();
  1454. list_for_each_entry(dp, &dps, list_node) {
  1455. struct flow_table *old_table = genl_dereference(dp->table);
  1456. struct flow_table *new_table;
  1457. new_table = ovs_flow_tbl_rehash(old_table);
  1458. if (!IS_ERR(new_table)) {
  1459. rcu_assign_pointer(dp->table, new_table);
  1460. ovs_flow_tbl_deferred_destroy(old_table);
  1461. }
  1462. }
  1463. genl_unlock();
  1464. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1465. }
  1466. static struct genl_ops dp_vport_genl_ops[] = {
  1467. { .cmd = OVS_VPORT_CMD_NEW,
  1468. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1469. .policy = vport_policy,
  1470. .doit = ovs_vport_cmd_new
  1471. },
  1472. { .cmd = OVS_VPORT_CMD_DEL,
  1473. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1474. .policy = vport_policy,
  1475. .doit = ovs_vport_cmd_del
  1476. },
  1477. { .cmd = OVS_VPORT_CMD_GET,
  1478. .flags = 0, /* OK for unprivileged users. */
  1479. .policy = vport_policy,
  1480. .doit = ovs_vport_cmd_get,
  1481. .dumpit = ovs_vport_cmd_dump
  1482. },
  1483. { .cmd = OVS_VPORT_CMD_SET,
  1484. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1485. .policy = vport_policy,
  1486. .doit = ovs_vport_cmd_set,
  1487. },
  1488. };
  1489. struct genl_family_and_ops {
  1490. struct genl_family *family;
  1491. struct genl_ops *ops;
  1492. int n_ops;
  1493. struct genl_multicast_group *group;
  1494. };
  1495. static const struct genl_family_and_ops dp_genl_families[] = {
  1496. { &dp_datapath_genl_family,
  1497. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1498. &ovs_dp_datapath_multicast_group },
  1499. { &dp_vport_genl_family,
  1500. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1501. &ovs_dp_vport_multicast_group },
  1502. { &dp_flow_genl_family,
  1503. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1504. &ovs_dp_flow_multicast_group },
  1505. { &dp_packet_genl_family,
  1506. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1507. NULL },
  1508. };
  1509. static void dp_unregister_genl(int n_families)
  1510. {
  1511. int i;
  1512. for (i = 0; i < n_families; i++)
  1513. genl_unregister_family(dp_genl_families[i].family);
  1514. }
  1515. static int dp_register_genl(void)
  1516. {
  1517. int n_registered;
  1518. int err;
  1519. int i;
  1520. n_registered = 0;
  1521. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1522. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1523. err = genl_register_family_with_ops(f->family, f->ops,
  1524. f->n_ops);
  1525. if (err)
  1526. goto error;
  1527. n_registered++;
  1528. if (f->group) {
  1529. err = genl_register_mc_group(f->family, f->group);
  1530. if (err)
  1531. goto error;
  1532. }
  1533. }
  1534. return 0;
  1535. error:
  1536. dp_unregister_genl(n_registered);
  1537. return err;
  1538. }
  1539. static int __init dp_init(void)
  1540. {
  1541. struct sk_buff *dummy_skb;
  1542. int err;
  1543. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1544. pr_info("Open vSwitch switching datapath\n");
  1545. err = ovs_flow_init();
  1546. if (err)
  1547. goto error;
  1548. err = ovs_vport_init();
  1549. if (err)
  1550. goto error_flow_exit;
  1551. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1552. if (err)
  1553. goto error_vport_exit;
  1554. err = dp_register_genl();
  1555. if (err < 0)
  1556. goto error_unreg_notifier;
  1557. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1558. return 0;
  1559. error_unreg_notifier:
  1560. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1561. error_vport_exit:
  1562. ovs_vport_exit();
  1563. error_flow_exit:
  1564. ovs_flow_exit();
  1565. error:
  1566. return err;
  1567. }
  1568. static void dp_cleanup(void)
  1569. {
  1570. cancel_delayed_work_sync(&rehash_flow_wq);
  1571. rcu_barrier();
  1572. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1573. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1574. ovs_vport_exit();
  1575. ovs_flow_exit();
  1576. }
  1577. module_init(dp_init);
  1578. module_exit(dp_cleanup);
  1579. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1580. MODULE_LICENSE("GPL");