datapath.c 49 KB

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